@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
52 lines (51 loc) • 2.29 kB
JavaScript
;
// based on https://github.com/storybookjs/storybook/blob/v8.5.0/code/builders/builder-vite/src/codegen-importfn-script.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateStoriesScript = void 0;
const pluginutils_1 = require("@rollup/pluginutils");
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);
}
exports.generateStoriesScript = generateStoriesScript;
/**
* 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 objectEntries = stories.map(file => {
const relativePath = (0, pluginutils_1.normalizePath)((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