@storybook/addon-console
Version:
Show console output like logs, errors, and warnings in the Storybook
178 lines (175 loc) • 5 kB
JavaScript
// src/index.tsx
import { global } from "@storybook/global";
import { action, configureActions } from "@storybook/addon-actions";
// src/react-decorator.tsx
import React from "react";
var ReactDecorator = class extends React.Component {
constructor(props) {
super(props);
this.props.onMount();
}
componentWillUnmount() {
this.props.onUnMount();
}
render() {
return this.props.story;
}
};
function reactStory(story, onMount, onUnMount) {
return /* @__PURE__ */ React.createElement(ReactDecorator, { story, onMount, onUnMount });
}
// src/index.tsx
if (configureActions) {
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: action(options.log),
warn: action(options.warn),
error: action(options.error),
dir: 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);
};
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);
}
export {
setConsoleOptions,
withConsole
};