@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
47 lines • 2.66 kB
JavaScript
;
// based on https://github.com/storybookjs/storybook/blob/v9.1.20/code/builders/builder-vite/src/transform-iframe-html.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateIframeHtml = generateIframeHtml;
const promises_1 = require("node:fs/promises");
const common_1 = require("storybook/internal/common");
const virtual_file_names_js_1 = require("./virtual-file-names.js");
async function generateIframeHtml(options) {
const iframeHtmlTemplate = await (0, promises_1.readFile)(require.resolve('../static/iframe-template.html'), {
encoding: 'utf-8',
});
const { configType, features, presets } = options;
const build = await presets.apply('build');
const frameworkOptions = await presets.apply('frameworkOptions');
const headHtmlSnippet = await presets.apply('previewHead');
const bodyHtmlSnippet = await presets.apply('previewBody');
const logLevel = await presets.apply('logLevel', undefined);
const docsOptions = await presets.apply('docs');
const tagsOptions = await presets.apply('tags');
const coreOptions = await presets.apply('core');
const stories = (0, common_1.normalizeStories)(await options.presets.apply('stories', [], options), {
configDir: options.configDir,
workingDir: process.cwd(),
}).map(specifier => ({
...specifier,
importPathMatcher: specifier.importPathMatcher.source,
}));
const otherGlobals = {
...(build?.test?.disableBlocks ? { __STORYBOOK_BLOCKS_EMPTY_MODULE__: {} } : {}),
};
return iframeHtmlTemplate
.replace('[CONFIG_TYPE HERE]', configType || '')
.replace('[LOGLEVEL HERE]', logLevel || '')
.replace(`'[FRAMEWORK_OPTIONS HERE]'`, JSON.stringify(frameworkOptions))
.replace(`('OTHER_GLOBALS HERE');`, Object.entries(otherGlobals)
.map(([k, v]) => `window["${k}"] = ${JSON.stringify(v)};`)
.join(''))
.replace(`'[CHANNEL_OPTIONS HERE]'`, JSON.stringify(coreOptions && coreOptions.channelOptions ? coreOptions.channelOptions : {}))
.replace(`'[FEATURES HERE]'`, JSON.stringify(features || {}))
.replace(`'[STORIES HERE]'`, JSON.stringify(stories || {}))
.replace(`'[DOCS_OPTIONS HERE]'`, JSON.stringify(docsOptions || {}))
.replace(`'[TAGS_OPTIONS HERE]'`, JSON.stringify(tagsOptions || {}))
.replace('<!-- [HEAD HTML SNIPPET HERE] -->', headHtmlSnippet || '')
.replace('<!-- [BODY HTML SNIPPET HERE] -->', bodyHtmlSnippet || '')
.replace(`[APP MODULE SRC HERE]`, virtual_file_names_js_1.virtualAppFilename);
}
//# sourceMappingURL=generate-iframe-html.js.map