-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathvirtual-module-mapping.ts
More file actions
83 lines (72 loc) · 2.6 KB
/
virtual-module-mapping.ts
File metadata and controls
83 lines (72 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
getBuilderOptions,
loadPreviewOrConfigFile,
normalizeStories,
readTemplate,
} from 'storybook/internal/common';
import type { Options, PreviewAnnotation } from 'storybook/internal/types';
import { toImportFn } from '@storybook/core-webpack';
import slash from 'slash';
import type { BuilderOptions } from '../types';
export const getVirtualModules = async (options: Options) => {
const virtualModules: Record<string, string> = {};
const builderOptions = await getBuilderOptions<BuilderOptions>(options);
const workingDir = process.cwd();
const isProd = options.configType === 'PRODUCTION';
const nonNormalizedStories = await options.presets.apply('stories', []);
const entries = [];
const stories = normalizeStories(nonNormalizedStories, {
configDir: options.configDir,
workingDir,
});
const previewAnnotations = [
...(await options.presets.apply<PreviewAnnotation[]>('previewAnnotations', [], options)).map(
(entry) => {
// If entry is an object, use the absolute import specifier.
// This is to maintain back-compat with community addons that bundle other addons
// and package managers that "hide" sub dependencies (e.g. pnpm / yarn pnp)
if (typeof entry === 'object') {
return entry.absolute;
}
return slash(entry);
}
),
loadPreviewOrConfigFile(options),
].filter(Boolean);
const storiesFilename = 'storybook-stories.js';
const storiesPath = resolve(join(workingDir, storiesFilename));
const needPipelinedImport = !!builderOptions.lazyCompilation && !isProd;
virtualModules[storiesPath] = toImportFn(stories, { needPipelinedImport });
const configEntryPath = resolve(join(workingDir, 'storybook-config-entry.js'));
virtualModules[configEntryPath] = (
await readTemplate(
fileURLToPath(
import.meta.resolve('@storybook/builder-webpack5/templates/virtualModuleModernEntry.js')
)
)
)
.replaceAll(`'{{storiesFilename}}'`, `'./${storiesFilename}'`)
.replaceAll(
`'{{previewAnnotations}}'`,
previewAnnotations
.filter(Boolean)
.map((entry) => `'${entry}'`)
.join(',')
)
.replaceAll(
`'{{previewAnnotations_requires}}'`,
previewAnnotations
.filter(Boolean)
.map((entry) => `require('${entry}')`)
.join(',')
)
// We need to double escape `\` for webpack. We may have some in windows paths
.replace(/\\/g, '\\\\');
entries.push(configEntryPath);
return {
virtualModules,
entries,
};
};