Skip to content

Commit

Permalink
Merge pull request #25098 from storybookjs/valentin/fix-csf-loader-fo…
Browse files Browse the repository at this point in the history
…r-angular-second-attempt

Angular: Fix CSF Plugin
(cherry picked from commit 96bcc58)
  • Loading branch information
valentinpalkovic authored and storybook-bot committed Dec 7, 2023
1 parent 580eb8f commit d223145
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 110 deletions.
2 changes: 1 addition & 1 deletion code/addons/storysource/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"prep": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/addon-bundle.ts"
},
"dependencies": {
"@storybook/source-loader": "workspace:*",
"estraverse": "^5.2.0",
"tiny-invariant": "^1.3.1"
},
Expand All @@ -56,7 +57,6 @@
"@storybook/manager-api": "workspace:*",
"@storybook/preview-api": "workspace:*",
"@storybook/router": "workspace:*",
"@storybook/source-loader": "workspace:*",
"@storybook/theming": "workspace:*",
"@types/react": "^16.14.34",
"@types/react-syntax-highlighter": "11.0.5",
Expand Down
15 changes: 9 additions & 6 deletions code/lib/csf-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ const logger = console;
export const unplugin = createUnplugin<CsfPluginOptions>((options) => {
return {
name: 'unplugin-csf',
enforce: 'pre',
loadInclude(id) {
transformInclude(id) {
return STORIES_REGEX.test(id);
},
async load(fname) {
const code = await fs.readFile(fname, 'utf-8');
async transform(code, id) {
const sourceCode = await fs.readFile(id, 'utf-8');
try {
const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse();
enrichCsf(csf, options);
const makeTitle = (userTitle: string) => userTitle || 'default';
const csf = loadCsf(code, { makeTitle }).parse();
const csfSource = loadCsf(sourceCode, {
makeTitle,
}).parse();
enrichCsf(csf, csfSource, options);
return formatCsf(csf, { sourceMaps: true });
} catch (err: any) {
// This can be called on legacy storiesOf files, so just ignore
Expand Down
Loading

0 comments on commit d223145

Please sign in to comment.