UNPKG

@storybook/addon-console

Version:

Show console output like logs, errors, and warnings in the Storybook

209 lines (205 loc) 6.73 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.tsx var src_exports = {}; __export(src_exports, { setConsoleOptions: () => setConsoleOptions, withConsole: () => withConsole }); module.exports = __toCommonJS(src_exports); var import_global = require("@storybook/global"); var import_addon_actions = require("@storybook/addon-actions"); // src/react-decorator.tsx var import_react = __toESM(require("react")); var ReactDecorator = class extends import_react.default.Component { constructor(props) { super(props); this.props.onMount(); } componentWillUnmount() { this.props.onUnMount(); } render() { return this.props.story; } }; function reactStory(story, onMount, onUnMount) { return /* @__PURE__ */ import_react.default.createElement(ReactDecorator, { story, onMount, onUnMount }); } // src/index.tsx if (import_addon_actions.configureActions) { (0, import_addon_actions.configureActions)({ clearOnStoryChange: false }); } var logger = console; var cLogger = { log: logger.log.bind(logger), warn: logger.warn.bind(logger), error: logger.error.bind(logger), dir: logger.dir.bind(logger) }; var addonOptions = { panelExclude: [/\[HMR\]/], panelInclude: [], consoleExclude: [], consoleInclude: [], log: "console", warn: "warn", error: "error", dir: "dir" }; var currentOptions = addonOptions; var createLogger = (options) => ({ log: (0, import_addon_actions.action)(options.log), warn: (0, import_addon_actions.action)(options.warn), error: (0, import_addon_actions.action)(options.error), dir: (0, import_addon_actions.action)(options.dir) }); var shouldDisplay = (messages, exclude, include) => { if (include.length) { return messages.filter( (mess) => typeof mess === "string" ? include.find((regExp) => mess.match(regExp)) : false ); } if (exclude.length) { return messages.filter( (mess) => typeof mess === "string" ? !exclude.find((regExp) => mess.match(regExp)) : true ); } return messages; }; function setScope(options) { const { panelExclude, panelInclude, consoleExclude, consoleInclude } = options; const aLogger = createLogger(options); logger.log = (...args) => { const toPanel = shouldDisplay(args, panelExclude, panelInclude); const toConsole = shouldDisplay(args, consoleExclude, consoleInclude); if (toPanel.length) aLogger.log(...toPanel); if (toConsole.length) cLogger.log(...toConsole); }; logger.warn = (...args) => { const toPanel = shouldDisplay(args, panelExclude, panelInclude); const toConsole = shouldDisplay(args, consoleExclude, consoleInclude); if (toPanel.length) aLogger.warn(...toPanel); if (toConsole.length) cLogger.warn(...toConsole); }; logger.error = (...args) => { const toPanel = shouldDisplay(args, panelExclude, panelInclude); const toConsole = shouldDisplay(args, consoleExclude, consoleInclude); if (toPanel.length) aLogger.error(...toPanel); if (toConsole.length) cLogger.error(...toConsole); }; logger.dir = (...args) => { const toPanel = shouldDisplay(args, panelExclude, panelInclude); const toConsole = shouldDisplay(args, consoleExclude, consoleInclude); if (toPanel.length) aLogger.dir(...toPanel); if (toConsole.length) cLogger.dir(...toConsole); }; import_global.global.onerror = (...args) => { const toPanel = shouldDisplay([args[0]], panelExclude, panelInclude); const toConsole = shouldDisplay([args[0]], consoleExclude, consoleInclude); if (toPanel.length) aLogger.error(...args); if (toConsole.length) return false; return true; }; } setScope(addonOptions); var detectOptions = (prop) => { if (!prop) return {}; if (typeof prop === "object") { const newOptions2 = { // ...addonOptions, // remove ...prop }; return newOptions2; } const newOptions = { ...prop(currentOptions) }; return newOptions; }; function setConsoleOptions(optionsOrFn) { const newOptions = detectOptions(optionsOrFn); currentOptions = { ...currentOptions, ...newOptions }; setScope(currentOptions); return currentOptions; } function handleStoryLogs(renderer) { switch (renderer) { case "react": return reactStory; default: logger.warn( `Warning! withConsole doesn't support @storybook/${renderer}. Use setConsoleOptions instead` ); return (story) => story; } } function addConsole(storyFn, context, consoleOptions) { const prevOptions = { ...currentOptions }; const logNames = context ? { log: `${context.kind}/${context.story}`, warn: `${context.kind}/${context.story}/warn`, error: `${context.kind}/${context.story}/error`, dir: `${context.kind}/${context.story}/dir` } : {}; const options = { ...currentOptions, ...logNames, ...consoleOptions }; setScope(options); const story = storyFn(); const wrapStory = handleStoryLogs(context.parameters.renderer); const wrappedStory = wrapStory( story, () => setScope(options), () => setScope(currentOptions) ); currentOptions = prevOptions; setScope(currentOptions); return wrappedStory; } function withConsole(optionsOrFn) { const newOptions = detectOptions(optionsOrFn); return (storyFn) => (context) => addConsole(storyFn, context, newOptions); }