-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
index.ts
200 lines (176 loc) · 6.67 KB
/
index.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import rollupPluginNodeResolve from '@rollup/plugin-node-resolve';
import { getBuilderOptions } from '@storybook/core-common';
import { logger } from '@storybook/node-logger';
// Import both globals and globalsNameReferenceMap to prevent retrocompatibility.
// @ts-ignore
import { globals, globalsNameReferenceMap } from '@storybook/preview/globals';
import type { Builder, Options, StorybookConfig as StorybookConfigBase } from '@storybook/types';
import { DevServerConfig, mergeConfigs, startDevServer } from '@web/dev-server';
import type { DevServer } from '@web/dev-server-core';
import { fromRollup } from '@web/dev-server-rollup';
import { rollupPluginHTML } from '@web/rollup-plugin-html';
import express from 'express';
import * as fs from 'fs-extra';
import { join, parse, resolve } from 'path';
import { OutputOptions, RollupBuild, RollupOptions, rollup } from 'rollup';
import rollupPluginExternalGlobals from 'rollup-plugin-external-globals';
import { generateIframeHtml } from './generate-iframe-html.js';
import { getNodeModuleDir } from './get-node-module-dir.js';
import { readFileConfig } from './read-file-config.js';
import { rollupPluginMdx } from './rollup-plugin-mdx.js';
import {
PREBUNDLED_MODULES_DIR,
rollupPluginPrebundleModules,
} from './rollup-plugin-prebundle-modules.js';
import { rollupPluginStorybookBuilder } from './rollup-plugin-storybook-builder.js';
const wdsPluginExternalGlobals = fromRollup(rollupPluginExternalGlobals);
const wdsPluginMdx = fromRollup(rollupPluginMdx);
const wdsPluginPrebundleModules = fromRollup(rollupPluginPrebundleModules);
const wdsPluginStorybookBuilder = fromRollup(rollupPluginStorybookBuilder);
export type StorybookConfigWds = StorybookConfigBase & {
wdsFinal?: (
config: DevServerConfig,
options: Options,
) => DevServerConfig | Promise<DevServerConfig>;
rollupFinal?: (config: RollupOptions, options: Options) => RollupOptions | Promise<RollupOptions>;
};
export type BuilderOptions = {
/**
* Path to @web/dev-server config file, relative to CWD.
*/
wdsConfigPath?: string;
};
// Storybook's Stats are optional Webpack related property
type WdsStats = {
toJson: () => any;
};
export type WdsBuilder = Builder<DevServerConfig, WdsStats>;
let wdsServer: DevServer;
export const bail: WdsBuilder['bail'] = async () => {
await wdsServer?.stop();
};
export const start: WdsBuilder['start'] = async ({ startTime, options, router, server }) => {
const previewDirOrigin = join(getNodeModuleDir('@storybook/preview'), 'dist');
router.use('/sb-preview', express.static(previewDirOrigin, { immutable: true, maxAge: '5m' }));
router.use(`/${PREBUNDLED_MODULES_DIR}`, express.static(resolve(`./${PREBUNDLED_MODULES_DIR}`)));
const env = await options.presets.apply<Record<string, string>>('env');
const wdsStorybookConfig: DevServerConfig = {
nodeResolve: true,
plugins: [
{
name: 'storybook-iframe-html',
async serve(context) {
if (context.path === '/iframe.html') {
const iframeHtml = await generateIframeHtml(options);
return { type: 'html', body: iframeHtml };
}
},
},
wdsPluginPrebundleModules(env),
wdsPluginStorybookBuilder(options),
wdsPluginMdx(options),
wdsPluginExternalGlobals(globalsNameReferenceMap || globals),
],
};
const { wdsConfigPath } = await getBuilderOptions<BuilderOptions>(options);
const wdsUserConfig = await readFileConfig(wdsConfigPath);
const wdsFinalConfig = await options.presets.apply<DevServerConfig>(
'wdsFinal',
mergeConfigs(wdsUserConfig, wdsStorybookConfig, {
// reset local config "open" as it should not be used for storybook specific configuration
open: false,
}),
options,
);
// if "wdsFinal" added "open" then rewrite it to open on storybook host (unless it's a full URL)
if (
wdsFinalConfig.open &&
typeof wdsFinalConfig.open === 'string' &&
!wdsFinalConfig.open.match(/^https?:\/\//)
) {
const protocol = options.https ? 'https' : 'http';
const host = options.host || 'localhost';
const port = options.port;
wdsFinalConfig.open = `${protocol}://${host}:${port}${wdsFinalConfig.open}`;
}
// setup middleware mode
wdsFinalConfig.middlewareMode = {
server,
};
wdsFinalConfig.port = undefined;
wdsFinalConfig.hostname = undefined;
wdsServer = await startDevServer({
// we load and merge configs manually
readFileConfig: false,
readCliArgs: false,
autoExitProcess: false,
logStartMessage: false,
config: wdsFinalConfig,
});
router.use(wdsServer.koaApp.callback());
return {
bail,
stats: { toJson: () => null },
totalTime: process.hrtime(startTime),
};
};
export const build: WdsBuilder['build'] = async ({ startTime, options }) => {
const env = await options.presets.apply<Record<string, string>>('env');
const rollupDefaultOutputOptions: OutputOptions = {
dir: options.outputDir,
};
const rollupStorybookConfig: RollupOptions = {
output: rollupDefaultOutputOptions,
external: ['./sb-preview/runtime.js'],
plugins: [
rollupPluginHTML({
input: { html: await generateIframeHtml(options), name: 'iframe.html' },
extractAssets: true,
bundleAssetsFromCss: true,
externalAssets: 'sb-common-assets/**',
}),
rollupPluginNodeResolve(),
rollupPluginPrebundleModules(env),
rollupPluginStorybookBuilder(options),
rollupPluginMdx(options),
rollupPluginExternalGlobals(globalsNameReferenceMap || globals),
],
};
const rollupFinalConfig = await options.presets.apply<RollupOptions>(
'rollupFinal',
rollupStorybookConfig,
options,
);
const rollupBuild = (async () => {
logger.info('=> Building preview..');
let bundle: RollupBuild | undefined;
try {
bundle = await rollup(rollupFinalConfig);
if (rollupFinalConfig.output) {
const outputOptionsArray = Array.isArray(rollupFinalConfig.output)
? rollupFinalConfig.output
: [rollupFinalConfig.output];
for (const outputOptions of outputOptionsArray) {
await bundle.write(outputOptions);
}
}
} finally {
if (bundle) {
bundle.close();
}
}
logger.trace({ message: '=> Preview built', time: process.hrtime(startTime) });
})();
const previewDirOrigin = join(getNodeModuleDir('@storybook/preview'), 'dist');
const previewDirTarget = join(options.outputDir || '', `sb-preview`);
const previewFiles = fs.copy(previewDirOrigin, previewDirTarget, {
filter: src => {
const { ext } = parse(src);
if (ext) {
return ext === '.js';
}
return true;
},
});
await Promise.all([rollupBuild, previewFiles]);
};