@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
77 lines (72 loc) • 3.52 kB
JavaScript
;
// based on https://github.com/storybookjs/storybook/blob/v9.1.20/code/builders/builder-vite/src/codegen-modern-iframe-script.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAppScript = generateAppScript;
const common_1 = require("storybook/internal/common");
const virtual_file_names_js_1 = require("./virtual-file-names.js");
async function generateAppScript(options) {
const slash = (await Promise.resolve().then(() => __importStar(require('slash')))).default; // for CJS compatibility
const { presets, configDir } = options;
const previewOrConfigFile = (0, common_1.loadPreviewOrConfigFile)({ configDir });
const previewAnnotations = await presets.apply('previewAnnotations', [], options);
const previewAnnotationURLs = [...previewAnnotations, previewOrConfigFile]
.filter((path) => !!path)
.map((path) => (typeof path === 'object' ? path.bare : path));
// This is pulled out to a variable because it is reused in both the initial page load
// and the HMR handler. We don't use the hot.accept callback params because only the changed
// modules are provided, the rest are null. We can just re-import everything again in that case.
const getPreviewAnnotationsFunction = `
const getProjectAnnotations = async () => {
const configs = await Promise.all([
${previewAnnotationURLs
.map(previewAnnotation => ` import('${slash(previewAnnotation)}')`)
.join(',\n')}
]);
return composeConfigs(configs);
}
`.trim();
return `
import { setup } from 'storybook/internal/preview/runtime';
import '${virtual_file_names_js_1.virtualSetupAddonsFilename}';
setup();
import { composeConfigs, PreviewWeb, ClientApi } from 'storybook/preview-api';
import { importFn } from '${virtual_file_names_js_1.virtualStoriesFilename}';
${getPreviewAnnotationsFunction}
window.__STORYBOOK_PREVIEW__ = window.__STORYBOOK_PREVIEW__ || new PreviewWeb(importFn, getProjectAnnotations);
window.__STORYBOOK_STORY_STORE__ = window.__STORYBOOK_STORY_STORE__ || window.__STORYBOOK_PREVIEW__.storyStore;
`.trim();
}
//# sourceMappingURL=generate-app-script.js.map