UNPKG

@web/storybook-builder

Version:
84 lines (83 loc) 3.65 kB
"use strict"; // based on https://github.com/storybookjs/storybook/blob/v9.1.20/code/builders/builder-vite/src/codegen-importfn-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.generateStoriesScript = generateStoriesScript; const node_path_1 = require("node:path"); const list_stories_1 = require("./list-stories"); async function generateStoriesScript(options) { // First we need to get an array of stories and their absolute paths. const stories = await (0, list_stories_1.listStories)(options); // We can then call toImportFn to create a function that can be used to load each story dynamically. return await toImportFn(stories); } /** * This function takes an array of stories and creates a mapping between the stories' relative paths * to the working directory and their dynamic imports. The import is done in an asynchronous * function to delay loading and to allow rollup to split the code into smaller chunks. It then * creates a function, `importFn(path)`, which resolves a path to an import function and this is * called by Storybook to fetch a story dynamically when needed. * * @param stories An array of absolute story paths. */ async function toImportFn(stories) { const slash = (await Promise.resolve().then(() => __importStar(require('slash')))).default; // for CJS compatibility const objectEntries = stories.map(file => { const relativePath = slash((0, node_path_1.relative)(process.cwd(), file)); const importPath = toImportPath(relativePath); let actualPath = file; if (actualPath.endsWith('.mdx')) { actualPath = `${actualPath}.js`; } return ` '${importPath}': () => import('${actualPath}')`; }); return ` const importers = { ${objectEntries.join(',\n')} }; export function importFn(path) { return importers[path](); } `.trim(); } /** * Paths get passed either with no leading './' - e.g. `src/Foo.stories.js`, * or with a leading `../` (etc), e.g. `../src/Foo.stories.js`. * We want to deal in importPaths relative to the working dir, so we normalize */ function toImportPath(relativePath) { return relativePath.startsWith('../') ? relativePath : `./${relativePath}`; } //# sourceMappingURL=generate-stories-script.js.map