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

Core: Rename generated-stories-entry to cjs extension so require works #16727

Merged
merged 9 commits into from Jan 24, 2022
7 changes: 6 additions & 1 deletion lib/builder-webpack5/src/preview/iframe-webpack.config.ts
Expand Up @@ -124,6 +124,8 @@ export default async (options: Options & Record<string, any>): Promise<Configura
const clientApi = storybookPaths['@storybook/client-api'];
const clientLogger = storybookPaths['@storybook/client-logger'];

// NOTE: although this file is also from the `dist/cjs` directory, it is actually a ESM
// file, see https://github.com/storybookjs/storybook/pull/16727#issuecomment-986485173
virtualModuleMapping[`${configFilename}-generated-config-entry.js`] = interpolate(
entryTemplate,
{
Expand All @@ -138,7 +140,10 @@ export default async (options: Options & Record<string, any>): Promise<Configura
const storyTemplate = await readTemplate(
path.join(__dirname, 'virtualModuleStory.template.js')
);
const storiesFilename = path.resolve(path.join(workingDir, `generated-stories-entry.js`));
// NOTE: this file has a `.cjs` extension as it is a CJS file (from `dist/cjs`) and runs
// in the user's webpack mode, which may be strict about the use of require/import.
// See https://github.com/storybookjs/storybook/issues/14877
const storiesFilename = path.resolve(path.join(workingDir, `generated-stories-entry.cjs`));
virtualModuleMapping[storiesFilename] = interpolate(storyTemplate, { frameworkImportPath })
// Make sure we also replace quotes for this one
.replace("'{{stories}}'", stories.map(toRequireContextString).join(','));
Expand Down