Skip to content

Commit 91ba424

Browse files
committed
Merge branch 'next' into valentin/move-addon-actions-into-core
2 parents 42e178f + e99b027 commit 91ba424

File tree

8 files changed

+119
-321
lines changed

8 files changed

+119
-321
lines changed

code/.storybook/preview.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ const ThemedSetRoot = () => {
131131
return null;
132132
};
133133

134+
// eslint-disable-next-line no-underscore-dangle
135+
const preview = (window as any).__STORYBOOK_PREVIEW__ as PreviewWeb<ReactRenderer> | undefined;
136+
const channel = (window as any).__STORYBOOK_ADDONS_CHANNEL__ as Channel | undefined;
134137
const loaders = [
135138
/**
136139
* This loader adds a DocsContext to the story, which is required for the most Blocks to work. A
@@ -145,9 +148,6 @@ const loaders = [
145148
* The DocsContext will then be added via the decorator below.
146149
*/
147150
async ({ parameters: { relativeCsfPaths, attached = true } }) => {
148-
// eslint-disable-next-line no-underscore-dangle
149-
const preview = (window as any).__STORYBOOK_PREVIEW__ as PreviewWeb<ReactRenderer> | undefined;
150-
const channel = (window as any).__STORYBOOK_ADDONS_CHANNEL__ as Channel | undefined;
151151
// __STORYBOOK_PREVIEW__ and __STORYBOOK_ADDONS_CHANNEL__ is set in the PreviewWeb constructor
152152
// which isn't loaded in portable stories/vitest
153153
if (!relativeCsfPaths || !preview || !channel) {

code/builders/builder-vite/src/codegen-modern-iframe-script.test.ts

-181
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { getFrameworkName, loadPreviewOrConfigFile } from 'storybook/internal/common';
2-
import { isCsfFactoryPreview, readConfig } from 'storybook/internal/csf-tools';
32
import type { Options, PreviewAnnotation } from 'storybook/internal/types';
43

5-
import { genArrayFromRaw, genImport, genSafeVariableName } from 'knitwork';
6-
import { filename } from 'pathe/utils';
74
import { dedent } from 'ts-dedent';
85

96
import { processPreviewAnnotation } from './utils/process-preview-annotation';
@@ -14,72 +11,38 @@ export async function generateModernIframeScriptCode(options: Options, projectRo
1411
const frameworkName = await getFrameworkName(options);
1512

1613
const previewOrConfigFile = loadPreviewOrConfigFile({ configDir });
17-
const previewConfig = await readConfig(previewOrConfigFile!);
18-
const isCsf4 = isCsfFactoryPreview(previewConfig);
19-
2014
const previewAnnotations = await presets.apply<PreviewAnnotation[]>(
2115
'previewAnnotations',
2216
[],
2317
options
2418
);
25-
return generateModernIframeScriptCodeFromPreviews({
26-
previewAnnotations: [...previewAnnotations, previewOrConfigFile],
27-
projectRoot,
28-
frameworkName,
29-
isCsf4,
30-
});
31-
}
32-
33-
export async function generateModernIframeScriptCodeFromPreviews(options: {
34-
previewAnnotations: (PreviewAnnotation | undefined)[];
35-
projectRoot: string;
36-
frameworkName: string;
37-
isCsf4: boolean;
38-
}) {
39-
const { projectRoot, frameworkName } = options;
40-
const previewAnnotationURLs = options.previewAnnotations
41-
.filter((path) => path !== undefined)
19+
const [previewFileUrl, ...previewAnnotationURLs] = [previewOrConfigFile, ...previewAnnotations]
20+
.filter(Boolean)
4221
.map((path) => processPreviewAnnotation(path, projectRoot));
4322

44-
const variables: string[] = [];
45-
const imports: string[] = [];
46-
for (const previewAnnotation of previewAnnotationURLs) {
47-
const variable =
48-
genSafeVariableName(filename(previewAnnotation)).replace(/_(45|46|47)/g, '_') +
49-
'_' +
50-
hash(previewAnnotation);
51-
variables.push(variable);
52-
imports.push(genImport(previewAnnotation, { name: '*', as: variable }));
53-
}
54-
55-
const previewFileURL = previewAnnotationURLs[previewAnnotationURLs.length - 1];
56-
const previewFileVariable = variables[variables.length - 1];
57-
const previewFileImport = imports[imports.length - 1];
58-
5923
// This is pulled out to a variable because it is reused in both the initial page load
60-
// and the HMR handler.
61-
// The `hmrPreviewAnnotationModules` parameter is used to pass the updated modules from HMR.
62-
// However, only the changed modules are provided, the rest are null.
63-
const getPreviewAnnotationsFunction = options.isCsf4
64-
? dedent`
65-
const getProjectAnnotations = (hmrPreviewAnnotationModules = []) => {
66-
const preview = hmrPreviewAnnotationModules[0] ?? ${previewFileVariable};
67-
return preview.default.composed;
68-
}`
69-
: dedent`
70-
const getProjectAnnotations = (hmrPreviewAnnotationModules = []) => {
71-
const configs = ${genArrayFromRaw(
72-
variables.map(
24+
// and the HMR handler. We don't use the hot.accept callback params because only the changed
25+
// modules are provided, the rest are null. We can just re-import everything again in that case.
26+
const getPreviewAnnotationsFunction = dedent`
27+
const getProjectAnnotations = async (hmrPreviewAnnotationModules = []) => {
28+
const preview = await import('${previewFileUrl}');
29+
30+
if (isPreview(preview.default)) {
31+
return preview.default.composed;
32+
}
33+
34+
const configs = await Promise.all([${previewAnnotationURLs
35+
.map(
7336
(previewAnnotation, index) =>
74-
// Prefer the updated module from an HMR update, otherwise the original module
75-
`hmrPreviewAnnotationModules[${index}] ?? ${previewAnnotation}`
76-
),
77-
' '
78-
)}
79-
return composeConfigs(configs);
37+
// Prefer the updated module from an HMR update, otherwise import the original module
38+
`hmrPreviewAnnotationModules[${index}] ?? import('${previewAnnotation}')`
39+
)
40+
.join(',\n')}])
41+
return composeConfigs([...configs, preview]);
8042
}`;
8143

82-
const generateHMRHandler = (): string => {
44+
// eslint-disable-next-line @typescript-eslint/no-shadow
45+
const generateHMRHandler = (frameworkName: string): string => {
8346
// Web components are not compatible with HMR, so disable HMR, reload page instead.
8447
if (frameworkName === '@storybook/web-components-vite') {
8548
return dedent`
@@ -95,7 +58,8 @@ export async function generateModernIframeScriptCodeFromPreviews(options: {
9558
window.__STORYBOOK_PREVIEW__.onStoriesChanged({ importFn: newModule.importFn });
9659
});
9760
98-
import.meta.hot.accept(${JSON.stringify(options.isCsf4 ? [previewFileURL] : previewAnnotationURLs)}, (previewAnnotationModules) => {
61+
import.meta.hot.accept(${JSON.stringify(previewAnnotationURLs)}, (previewAnnotationModules) => {
62+
${getPreviewAnnotationsFunction}
9963
// getProjectAnnotations has changed so we need to patch the new one in
10064
window.__STORYBOOK_PREVIEW__.onGetProjectAnnotationsChanged({ getProjectAnnotations: () => getProjectAnnotations(previewAnnotationModules) });
10165
});
@@ -112,7 +76,6 @@ export async function generateModernIframeScriptCodeFromPreviews(options: {
11276
*/
11377
const code = dedent`
11478
import { setup } from 'storybook/internal/preview/runtime';
115-
11679
import '${SB_VIRTUAL_FILES.VIRTUAL_ADDON_SETUP_FILE}';
11780
11881
setup();
@@ -121,17 +84,12 @@ export async function generateModernIframeScriptCodeFromPreviews(options: {
12184
import { isPreview } from 'storybook/internal/csf';
12285
import { importFn } from '${SB_VIRTUAL_FILES.VIRTUAL_STORIES_FILE}';
12386
124-
${options.isCsf4 ? previewFileImport : imports.join('\n')}
12587
${getPreviewAnnotationsFunction}
12688
12789
window.__STORYBOOK_PREVIEW__ = window.__STORYBOOK_PREVIEW__ || new PreviewWeb(importFn, getProjectAnnotations);
12890
12991
window.__STORYBOOK_STORY_STORE__ = window.__STORYBOOK_STORY_STORE__ || window.__STORYBOOK_PREVIEW__.storyStore;
130-
131-
${generateHMRHandler()};
132-
`.trim();
92+
93+
${generateHMRHandler(frameworkName)};`.trim();
13394
return code;
13495
}
135-
function hash(value: string) {
136-
return value.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
137-
}

code/builders/builder-vite/src/optimizeDeps.ts

-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ const INCLUDE_CANDIDATES = [
127127
'qs',
128128
'react-dom',
129129
'react-dom/client',
130-
'react-dom/test-utils',
131130
'react-fast-compare',
132131
'react-is',
133132
'react-textarea-autosize',
@@ -147,7 +146,6 @@ const INCLUDE_CANDIDATES = [
147146
'refractor/lang/typescript.js',
148147
'refractor/lang/yaml.js',
149148
'regenerator-runtime/runtime.js',
150-
'semver', // TODO: Remove once https://github.com/npm/node-semver/issues/712 is fixed
151149
'sb-original/default-loader',
152150
'sb-original/image-context',
153151
'slash',

0 commit comments

Comments
 (0)