fix: Remove Storybook parameters if story is present

This commit is contained in:
Jiri Vyhnalek 2023-01-06 15:11:46 +01:00
Родитель d812fa02c1
Коммит b9d68b2cfd
2 изменённых файлов: 35 добавлений и 1 удалений

Просмотреть файл

@ -1,5 +1,6 @@
import * as Babel from '@babel/core';
import modifyImportsPlugin from './modifyImports';
import removeStorybookParameters from './removeStorybookParameters';
export const PLUGIN_NAME = 'storybook-stories-fullsource';
@ -44,7 +45,7 @@ export default function (babel: typeof Babel, options: BabelPluginOptions): Babe
const transformedCode = babel.transformSync(path.node.init.value, {
...state.file.opts,
comments: false,
plugins: [[modifyImportsPlugin, options]],
plugins: [[modifyImportsPlugin, options], removeStorybookParameters],
}).code;
path.get('init').replaceWith(t.stringLiteral(transformedCode));

Просмотреть файл

@ -0,0 +1,33 @@
import * as Babel from '@babel/core';
export const PLUGIN_NAME = 'babel-plugin-remove-storybook-parameters';
/**
* This plugin finds Storybook "parameters" assignment and removes it, if it contains "story" key.
* The reason for this is that sometimes "story" is not a hardcoded string, but it is imported from
* a markdown file, which results in story being undefined and CodeSandbox example not working.
*
* Since we dont actually need Storybook parameters in the CodeSandbox anyway,
* the easiest thing to do is to remove it altogether.
*/
export default function removeStorybookParameters(babel: typeof Babel): Babel.PluginObj {
const { types: t } = babel;
return {
name: PLUGIN_NAME,
visitor: {
Identifier(path) {
if (path.node.name === 'story') {
const parentPath = path.findParent(
path =>
path.isAssignmentExpression() &&
t.isMemberExpression(path.node.left) &&
t.isIdentifier(path.node.left.property) &&
path.node.left.property.name === 'parameters',
);
parentPath?.remove();
}
},
},
};
}