|
1 | 1 | // based on https://github.com/storybookjs/storybook/blob/v8.5.0/code/builders/builder-vite/src/codegen-importfn-script.ts
|
2 | 2 |
|
3 | 3 | import { normalizePath } from '@rollup/pluginutils';
|
4 |
| -import { logger } from '@storybook/node-logger'; |
5 | 4 | import type { Options } from '@storybook/types';
|
6 |
| -import { extname, relative } from 'node:path'; |
7 |
| -import { listStories } from './list-stories.js'; |
| 5 | +import { relative } from 'node:path'; |
| 6 | +import { listStories } from './list-stories'; |
8 | 7 |
|
9 |
| -/** |
10 |
| - * This file is largely based on https://github.com/storybookjs/storybook/blob/d1195cbd0c61687f1720fefdb772e2f490a46584/lib/core-common/src/utils/to-importFn.ts |
11 |
| - */ |
12 |
| - |
13 |
| -export async function generateStoriesScript(options: Options) { |
| 8 | +export async function generateStoriesScript(options: Options): Promise<string> { |
14 | 9 | // First we need to get an array of stories and their absolute paths.
|
15 | 10 | const stories = await listStories(options);
|
16 | 11 |
|
17 | 12 | // We can then call toImportFn to create a function that can be used to load each story dynamically.
|
18 |
| - return (await toImportFn(stories)).trim(); |
| 13 | + return await toImportFn(stories); |
19 | 14 | }
|
20 | 15 |
|
21 | 16 | /**
|
22 | 17 | * This function takes an array of stories and creates a mapping between the stories' relative paths
|
23 |
| - * to the working directory and their dynamic imports. The import is done in an asynchronous function |
24 |
| - * to delay loading. It then creates a function, `importFn(path)`, which resolves a path to an import |
25 |
| - * function and this is called by Storybook to fetch a story dynamically when needed. |
| 18 | + * to the working directory and their dynamic imports. The import is done in an asynchronous |
| 19 | + * function to delay loading and to allow rollup to split the code into smaller chunks. It then |
| 20 | + * creates a function, `importFn(path)`, which resolves a path to an import function and this is |
| 21 | + * called by Storybook to fetch a story dynamically when needed. |
| 22 | + * |
26 | 23 | * @param stories An array of absolute story paths.
|
27 | 24 | */
|
28 | 25 | async function toImportFn(stories: string[]) {
|
29 | 26 | const objectEntries = stories.map(file => {
|
30 |
| - const ext = extname(file); |
31 | 27 | const relativePath = normalizePath(relative(process.cwd(), file));
|
32 |
| - if (!['.js', '.jsx', '.ts', '.tsx', '.mdx', '.svelte', '.vue'].includes(ext)) { |
33 |
| - logger.warn(`Cannot process ${ext} file with storyStoreV7: ${relativePath}`); |
34 |
| - } |
35 |
| - |
36 | 28 | const importPath = toImportPath(relativePath);
|
37 | 29 | let actualPath = file;
|
38 | 30 | if (actualPath.endsWith('.mdx')) {
|
|
0 commit comments