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

Fix builder-webpack 5 usage in ESM environment #18620

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/angular/src/server/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { StorybookConfig } from '@storybook/core-common';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entries = []) => [
...entries,
require.resolve('../client/preview/config'),
require.resolve('../client/preview/config.js'),
];

export const addons: StorybookConfig['addons'] = [
Expand Down
2 changes: 1 addition & 1 deletion app/ember/src/server/framework-preset-babel-ember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ export function babel(config: TransformOptions, options: Options): TransformOpti
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/html/src/server/framework-preset-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export function webpack(config: Configuration) {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/preact/src/server/framework-preset-preact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export function webpackFinal(config: Configuration): Configuration {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/react/src/server/framework-preset-react-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ export async function webpackFinal(config: Configuration, options: Options) {

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = [], options) => {
if (!hasDocsOrControls(options)) return entry;
return [...entry, findDistEsm(__dirname, 'client/docs/config')];
return [...entry, findDistEsm(__dirname, 'client/docs/config.js')];
};
2 changes: 1 addition & 1 deletion app/react/src/server/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { StorybookConfig } from '@storybook/core-common';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entries = []) => [
...entries,
findDistEsm(__dirname, 'client/preview/config'),
findDistEsm(__dirname, 'client/preview/config.js'),
];

export const addons: StorybookConfig['addons'] = [
Expand Down
2 changes: 1 addition & 1 deletion app/server/src/server/framework-preset-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export function webpack(config: Configuration) {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/svelte/src/server/framework-preset-svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export async function babelDefault(config: TransformOptions): Promise<TransformO
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/vue/src/server/framework-preset-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export async function webpack(config: Configuration, { presets }: Options) {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
2 changes: 1 addition & 1 deletion app/vue3/src/server/framework-preset-vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ export function webpack(config: Configuration): Configuration {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export function webpack(config: Configuration, options: Options) {
}

export const previewAnnotations: StorybookConfig['previewAnnotations'] = (entry = []) => {
return [...entry, findDistEsm(__dirname, 'client/preview/config')];
return [...entry, findDistEsm(__dirname, 'client/preview/config.js')];
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { importFn } from './{{storiesFilename}}';

const { SERVER_CHANNEL_URL } = global;

const getProjectAnnotations = () =>
composeConfigs([{{#each configs}}require('{{this}}'),{{/each}}]);
const getProjectAnnotations = async () => {
const configs = await Promise.all([{{#each configs}}import('{{this}}'),{{/each}}])
return composeConfigs(configs);
}

const channel = createPostMessageChannel({ page: 'preview' });
addons.setChannel(channel);
Expand All @@ -32,7 +34,7 @@ window.__STORYBOOK_CLIENT_API__ = new ClientApi({ storyStore: preview.storyStore

preview.initialize({ importFn, getProjectAnnotations });

if (module.hot) {
if (import.meta.webpackHot) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a backwards compatible change?

Copy link
Contributor Author

@valentinpalkovic valentinpalkovic Jul 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The folder structure indicates, that this template is exclusively used for the webpack 5 builder. As I have understood the docs, it is totally valid to use this syntax also for CommonJS environments (https://webpack.js.org/api/hot-module-replacement/). So if your question is about backwards-compatibility regarding CommonJS environments, then yes. It should work. Especially, because it is already used in the next line.

import.meta.webpackHot.accept('./{{storiesFilename}}', () => {
// importFn has changed so we need to patch the new one in
preview.onStoriesChanged({ importFn });
Expand Down