|
1 | 1 | import { normalizeStories } from '@storybook/core-common'; |
2 | | -import type { CoreConfig, Options } from '@storybook/types'; |
3 | | -import { readFile } from 'fs-extra'; |
| 2 | +import type { DocsOptions, Options, TagsOptions } from '@storybook/types'; |
| 3 | +import { readFile } from 'node:fs/promises'; |
4 | 4 | import { virtualAppFilename } from './virtual-file-names.js'; |
5 | 5 |
|
6 | 6 | export type PreviewHtml = string | undefined; |
7 | 7 |
|
8 | 8 | export async function generateIframeHtml(options: Options): Promise<string> { |
9 | | - const iframeHtmlTemplate = await readFile( |
10 | | - require.resolve('../static/iframe-template.html'), |
11 | | - 'utf-8', |
12 | | - ); |
13 | | - const { configType, features, presets, serverChannelUrl } = options; |
| 9 | + const iframeHtmlTemplate = await readFile(require.resolve('../static/iframe-template.html'), { |
| 10 | + encoding: 'utf-8', |
| 11 | + }); |
| 12 | + const { configType, features, presets } = options; |
| 13 | + const build = await presets.apply('build'); |
14 | 14 | const frameworkOptions = await presets.apply<Record<string, any> | null>('frameworkOptions'); |
15 | 15 | const headHtmlSnippet = await presets.apply<PreviewHtml>('previewHead'); |
16 | 16 | const bodyHtmlSnippet = await presets.apply<PreviewHtml>('previewBody'); |
17 | 17 | const logLevel = await presets.apply('logLevel', undefined); |
18 | | - // const docsOptions = await presets.apply<DocsOptions>('docs'); |
19 | | - const coreOptions = await presets.apply<CoreConfig>('core'); |
| 18 | + const docsOptions = await presets.apply<DocsOptions>('docs'); |
| 19 | + const tagsOptions = await presets.apply<TagsOptions>('tags'); |
| 20 | + const coreOptions = await presets.apply('core'); |
20 | 21 | const stories = normalizeStories(await options.presets.apply('stories', [], options), { |
21 | 22 | configDir: options.configDir, |
22 | 23 | workingDir: process.cwd(), |
23 | 24 | }).map(specifier => ({ |
24 | 25 | ...specifier, |
25 | 26 | importPathMatcher: specifier.importPathMatcher.source, |
26 | 27 | })); |
27 | | - return ( |
28 | | - iframeHtmlTemplate |
29 | | - .replace('[CONFIG_TYPE HERE]', configType || '') |
30 | | - .replace('[LOGLEVEL HERE]', logLevel || '') |
31 | | - .replace(`'[FRAMEWORK_OPTIONS HERE]'`, JSON.stringify(frameworkOptions)) |
32 | | - .replace( |
33 | | - `'[CHANNEL_OPTIONS HERE]'`, |
34 | | - JSON.stringify(coreOptions && coreOptions.channelOptions ? coreOptions.channelOptions : {}), |
35 | | - ) |
36 | | - .replace(`'[FEATURES HERE]'`, JSON.stringify(features || {})) |
37 | | - .replace(`'[STORIES HERE]'`, JSON.stringify(stories || {})) |
38 | | - // .replace(`'[DOCS_OPTIONS HERE]'`, JSON.stringify(docsOptions || {})) |
39 | | - .replace(`'[SERVER_CHANNEL_URL HERE]'`, JSON.stringify(serverChannelUrl)) |
40 | | - .replace('<!-- [HEAD HTML SNIPPET HERE] -->', headHtmlSnippet || '') |
41 | | - .replace('<!-- [BODY HTML SNIPPET HERE] -->', bodyHtmlSnippet || '') |
42 | | - .replace(`[APP MODULE SRC HERE]`, virtualAppFilename) |
43 | | - ); |
| 28 | + const otherGlobals = { |
| 29 | + ...(build?.test?.disableBlocks ? { __STORYBOOK_BLOCKS_EMPTY_MODULE__: {} } : {}), |
| 30 | + }; |
| 31 | + return iframeHtmlTemplate |
| 32 | + .replace('[CONFIG_TYPE HERE]', configType || '') |
| 33 | + .replace('[LOGLEVEL HERE]', logLevel || '') |
| 34 | + .replace(`'[FRAMEWORK_OPTIONS HERE]'`, JSON.stringify(frameworkOptions)) |
| 35 | + .replace( |
| 36 | + `('OTHER_GLOBLALS HERE');`, |
| 37 | + Object.entries(otherGlobals) |
| 38 | + .map(([k, v]) => `window["${k}"] = ${JSON.stringify(v)};`) |
| 39 | + .join(''), |
| 40 | + ) |
| 41 | + .replace( |
| 42 | + `'[CHANNEL_OPTIONS HERE]'`, |
| 43 | + JSON.stringify(coreOptions && coreOptions.channelOptions ? coreOptions.channelOptions : {}), |
| 44 | + ) |
| 45 | + .replace(`'[FEATURES HERE]'`, JSON.stringify(features || {})) |
| 46 | + .replace(`'[STORIES HERE]'`, JSON.stringify(stories || {})) |
| 47 | + .replace(`'[DOCS_OPTIONS HERE]'`, JSON.stringify(docsOptions || {})) |
| 48 | + .replace(`'[TAGS_OPTIONS HERE]'`, JSON.stringify(tagsOptions || {})) |
| 49 | + .replace('<!-- [HEAD HTML SNIPPET HERE] -->', headHtmlSnippet || '') |
| 50 | + .replace('<!-- [BODY HTML SNIPPET HERE] -->', bodyHtmlSnippet || '') |
| 51 | + .replace(`[APP MODULE SRC HERE]`, virtualAppFilename); |
44 | 52 | } |
0 commit comments