diff --git a/code/addons/storysource/package.json b/code/addons/storysource/package.json index a36f6440594d..1f479b45d33a 100644 --- a/code/addons/storysource/package.json +++ b/code/addons/storysource/package.json @@ -47,7 +47,6 @@ "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" }, @@ -57,6 +56,7 @@ "@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", diff --git a/code/lib/csf-plugin/src/index.ts b/code/lib/csf-plugin/src/index.ts index ec0aaa1d52a9..aed7a531ee32 100644 --- a/code/lib/csf-plugin/src/index.ts +++ b/code/lib/csf-plugin/src/index.ts @@ -12,18 +12,15 @@ const logger = console; export const unplugin = createUnplugin((options) => { return { name: 'unplugin-csf', - transformInclude(id) { + enforce: 'pre', + loadInclude(id) { return STORIES_REGEX.test(id); }, - async transform(code, id) { - const sourceCode = await fs.readFile(id, 'utf-8'); + async load(fname) { + const code = await fs.readFile(fname, 'utf-8'); try { - const makeTitle = (userTitle: string) => userTitle || 'default'; - const csf = loadCsf(code, { makeTitle }).parse(); - const csfSource = loadCsf(sourceCode, { - makeTitle, - }).parse(); - enrichCsf(csf, csfSource, options); + const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse(); + enrichCsf(csf, options); return formatCsf(csf, { sourceMaps: true }); } catch (err: any) { // This can be called on legacy storiesOf files, so just ignore diff --git a/code/lib/csf-tools/src/enrichCsf.test.ts b/code/lib/csf-tools/src/enrichCsf.test.ts index d793f730cecc..a8f5f3aaa09c 100644 --- a/code/lib/csf-tools/src/enrichCsf.test.ts +++ b/code/lib/csf-tools/src/enrichCsf.test.ts @@ -11,16 +11,11 @@ expect.addSnapshotSerializer({ test: (val) => true, }); -const enrich = (code: string, originalCode: string, options?: EnrichCsfOptions) => { +const enrich = (code: string, options?: EnrichCsfOptions) => { // we don't actually care about the title - const csf = loadCsf(code, { - makeTitle: (userTitle) => userTitle || 'default', - }).parse(); - const csfSource = loadCsf(originalCode, { - makeTitle: (userTitle) => userTitle || 'default', - }).parse(); - enrichCsf(csf, csfSource, options); + const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse(); + enrichCsf(csf, options); return formatCsf(csf); }; @@ -28,28 +23,17 @@ describe('enrichCsf', () => { describe('source', () => { it('csf1', () => { expect( - enrich( - dedent` - // compiled code + enrich(dedent` export default { title: 'Button', } - export const Basic = () => React.createElement(Button) - `, - dedent` - // original code - export default { - title: 'Button', - } - export const Basic = () =>