Skip to content

Commit d8f53d5

Browse files
committed
Vite: Don't prefix story import with @fs
1 parent be2e4c8 commit d8f53d5

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
import { toImportFn } from './codegen-importfn-script';
4+
5+
describe('toImportFn', () => {
6+
it('should correctly map story paths to import functions for absolute paths on Linux', async () => {
7+
const root = '/absolute/path';
8+
const stories = ['/absolute/path/to/story1.js', '/absolute/path/to/story2.js'];
9+
10+
const result = await toImportFn(root, stories);
11+
12+
expect(result).toMatchInlineSnapshot(`
13+
"const importers = {
14+
"./to/story1.js": () => import("/absolute/path/to/story1.js"),
15+
"./to/story2.js": () => import("/absolute/path/to/story2.js")
16+
};
17+
18+
export async function importFn(path) {
19+
return await importers[path]();
20+
}"
21+
`);
22+
});
23+
24+
it('should correctly map story paths to import functions for absolute paths on Windows', async () => {
25+
const root = 'C:\\absolute\\path';
26+
const stories = ['C:\\absolute\\path\\to\\story1.js', 'C:\\absolute\\path\\to\\story2.js'];
27+
28+
const result = await toImportFn(root, stories);
29+
30+
expect(result).toMatchInlineSnapshot(`
31+
"const importers = {
32+
"./to/story1.js": () => import("C:/absolute/path/to/story1.js"),
33+
"./to/story2.js": () => import("C:/absolute/path/to/story2.js")
34+
};
35+
36+
export async function importFn(path) {
37+
return await importers[path]();
38+
}"
39+
`);
40+
});
41+
42+
it('should handle an empty array of stories', async () => {
43+
const root = '/absolute/path';
44+
const stories: string[] = [];
45+
46+
const result = await toImportFn(root, stories);
47+
48+
expect(result).toMatchInlineSnapshot(`
49+
"const importers = {};
50+
51+
export async function importFn(path) {
52+
return await importers[path]();
53+
}"
54+
`);
55+
});
56+
});
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { relative } from 'node:path';
2-
31
import type { Options } from 'storybook/internal/types';
42

3+
import { genDynamicImport, genImport, genObjectFromRawEntries } from 'knitwork';
4+
import { normalize, relative } from 'pathe';
5+
import { dedent } from 'ts-dedent';
6+
57
import { listStories } from './list-stories';
68

79
/**
@@ -21,34 +23,33 @@ function toImportPath(relativePath: string) {
2123
/**
2224
* This function takes an array of stories and creates a mapping between the stories' relative paths
2325
* to the working directory and their dynamic imports. The import is done in an asynchronous
24-
* function to delay loading. It then creates a function, `importFn(path)`, which resolves a path to
25-
* an import function and this is called by Storybook to fetch a story dynamically when needed.
26+
* function to delay loading and to allow Vite to split the code into smaller chunks. It then
27+
* creates a function, `importFn(path)`, which resolves a path to an import function and this is
28+
* called by Storybook to fetch a story dynamically when needed.
2629
*
2730
* @param stories An array of absolute story paths.
2831
*/
29-
async function toImportFn(stories: string[]) {
30-
const { normalizePath } = await import('vite');
32+
export async function toImportFn(root: string, stories: string[]) {
3133
const objectEntries = stories.map((file) => {
32-
const relativePath = normalizePath(relative(process.cwd(), file));
34+
const relativePath = relative(root, file);
3335

34-
return ` '${toImportPath(relativePath)}': async () => import('/@fs/${file}')`;
36+
return [toImportPath(relativePath), genDynamicImport(normalize(file))] as [string, string];
3537
});
3638

37-
return `
38-
const importers = {
39-
${objectEntries.join(',\n')}
40-
};
39+
return dedent`
40+
const importers = ${genObjectFromRawEntries(objectEntries)};
4141
4242
export async function importFn(path) {
43-
return importers[path]();
43+
return await importers[path]();
4444
}
4545
`;
4646
}
4747

48-
export async function generateImportFnScriptCode(options: Options) {
48+
export async function generateImportFnScriptCode(options: Options): Promise<string> {
4949
// First we need to get an array of stories and their absolute paths.
5050
const stories = await listStories(options);
5151

5252
// We can then call toImportFn to create a function that can be used to load each story dynamically.
53-
return (await toImportFn(stories)).trim();
53+
// eslint-disable-next-line @typescript-eslint/return-await
54+
return await toImportFn(options.root || process.cwd(), stories);
5455
}

0 commit comments

Comments
 (0)