Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF-tools/Codemods: Pre-bundle all babel related packages #21301

Merged
merged 6 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions code/lib/codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,35 @@
"prep": "../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/preset-env": "^7.20.2",
"@babel/types": "^7.20.7",
"@storybook/csf": "next",
"@storybook/csf-tools": "7.0.0-beta.56",
"@storybook/node-logger": "7.0.0-beta.56",
"@storybook/types": "7.0.0-beta.56",
"cross-spawn": "^7.0.3",
"globby": "^11.0.2",
"jscodeshift": "^0.14.0",
"lodash": "^4.17.21",
"prettier": "^2.8.0",
"recast": "^0.23.1",
"util": "^0.12.4"
"recast": "^0.23.1"
},
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/types": "^7.21.2",
"@types/jscodeshift": "^0.11.6",
"ansi-regex": "^5.0.1",
"cross-spawn": "^7.0.3",
"globby": "^11.0.2",
"jest": "^29.3.1",
"jest-specific-snapshot": "^7.0.0",
"jscodeshift": "^0.14.0",
"lodash": "^4.17.21",
"mdast-util-mdx-jsx": "^2.1.2",
"mdast-util-mdxjs-esm": "^1.3.1",
"prettier": "^2.8.0",
"remark": "^14.0.2",
"remark-mdx": "^2.2.1",
"ts-dedent": "^2.2.0",
"typescript": "~4.9.3",
"unist-util-is": "^5.2.0",
"unist-util-select": "^4.0.3",
"unist-util-visit": "^4.1.2",
"util": "^0.12.4",
"vfile": "^5.3.7"
},
"publishConfig": {
Expand Down
12 changes: 8 additions & 4 deletions code/lib/codemod/src/transforms/csf-2-to-3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function transform(info: FileInfo, api: API, options: { parser?:
// Remove the render function when we can hoist the template
// const Template = (args) => <Cat {...args} />;
// export const A = Template.bind({});
let storyFn = template && csf._templates[template];
let storyFn: t.Expression = template && (csf._templates[template] as any as t.Expression);
if (!storyFn) {
storyFn = init;
}
Expand All @@ -163,20 +163,24 @@ export default function transform(info: FileInfo, api: API, options: { parser?:
importHelper.removeDeprecatedStoryImport();

csf._ast.program.body = csf._ast.program.body.reduce((acc, stmt) => {
const statement = stmt as t.Statement;
// remove story annotations & template declarations
if (isStoryAnnotation(stmt, objectExports) || isTemplateDeclaration(stmt, csf._templates)) {
if (
isStoryAnnotation(statement, objectExports) ||
isTemplateDeclaration(statement, csf._templates)
) {
return acc;
}

// replace story exports with new object exports
const newExport = getNewExport(stmt, objectExports);
const newExport = getNewExport(statement, objectExports);
if (newExport) {
acc.push(newExport);
return acc;
}

// include unknown statements
acc.push(stmt);
acc.push(statement);
return acc;
}, []);

Expand Down
11 changes: 7 additions & 4 deletions code/lib/codemod/src/transforms/mdx-to-csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ export function transform(source: string, baseName: string): [mdx: string, csf:
return [t.objectProperty(t.identifier(attribute.name), t.stringLiteral(attribute.value))];
}
return [
t.objectProperty(t.identifier(attribute.name), babelParseExpression(attribute.value.value)),
t.objectProperty(
t.identifier(attribute.name),
babelParseExpression(attribute.value.value) as any as t.Expression
),
];
}
return [];
Expand Down Expand Up @@ -216,7 +219,7 @@ export function transform(source: string, baseName: string): [mdx: string, csf:
return t.arrowFunctionExpression([], t.stringLiteral(child.value));
}
if (child.type === 'mdxFlowExpression' || child.type === 'mdxTextExpression') {
const expression = babelParseExpression(child.value);
const expression = babelParseExpression(child.value) as any as t.Expression;

// Recreating those lines: https://github.com/storybookjs/mdx1-csf/blob/f408fc97e9a63097ca1ee577df9315a3cccca975/src/sb-mdx-plugin.ts#L185-L198
const BIND_REGEX = /\.bind\(.*\)/;
Expand All @@ -234,7 +237,7 @@ export function transform(source: string, baseName: string): [mdx: string, csf:

const expression = babelParseExpression(
mdxProcessor.stringify({ type: 'root', children: [child] })
);
) as any as t.Expression;
return t.arrowFunctionExpression([], expression);
}

Expand Down Expand Up @@ -272,7 +275,7 @@ export function transform(source: string, baseName: string): [mdx: string, csf:
return [
t.objectProperty(
t.identifier(attribute.name),
babelParseExpression(attribute.value.value)
babelParseExpression(attribute.value.value) as any as t.Expression
),
];
}
Expand Down
8 changes: 4 additions & 4 deletions code/lib/csf-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
"prep": "../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@babel/types": "^7.20.2",
"@storybook/csf": "next",
"@storybook/types": "7.0.0-beta.56",
"fs-extra": "^11.1.0",
"recast": "^0.23.1",
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"@babel/generator": "^7.20.4",
"@babel/parser": "^7.20.3",
"@babel/traverse": "^7.20.1",
"@babel/generator": "^7.21.1",
"@babel/parser": "^7.21.2",
"@babel/traverse": "^7.21.2",
"@babel/types": "^7.21.2",
"@types/fs-extra": "^11.0.1",
"@types/js-yaml": "^3.12.6",
"js-yaml": "^3.14.1",
Expand Down
Loading