/
dev-server.ts
174 lines (148 loc) · 5.5 KB
/
dev-server.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
import express, { Router } from 'express';
import compression from 'compression';
import {
CoreConfig,
DocsOptions,
Options,
StorybookConfig,
normalizeStories,
logConfig,
} from '@storybook/core-common';
import { telemetry } from '@storybook/telemetry';
import { getMiddleware } from './utils/middleware';
import { getServerAddresses } from './utils/server-address';
import { getServer } from './utils/server-init';
import { useStatics } from './utils/server-statics';
import { useStoriesJson } from './utils/stories-json';
import { useStorybookMetadata } from './utils/metadata';
import { getServerChannel } from './utils/get-server-channel';
import { openInBrowser } from './utils/open-in-browser';
import { getBuilders } from './utils/get-builders';
import { StoryIndexGenerator } from './utils/StoryIndexGenerator';
// @ts-expect-error (Converted from ts-ignore)
export const router: Router = new Router();
export const DEBOUNCE = 100;
export async function storybookDevServer(options: Options) {
const startTime = process.hrtime();
const app = express();
const server = await getServer(app, options);
const serverChannel = getServerChannel(server);
const features = await options.presets.apply<StorybookConfig['features']>('features');
const core = await options.presets.apply<CoreConfig>('core');
// try get index generator, if failed, send telemetry without storyCount, then rethrow the error
let initializedStoryIndexGenerator: Promise<StoryIndexGenerator> = Promise.resolve(undefined);
if (features?.buildStoriesJson || features?.storyStoreV7) {
try {
const workingDir = process.cwd();
const directories = {
configDir: options.configDir,
workingDir,
};
const normalizedStories = normalizeStories(
await options.presets.apply('stories'),
directories
);
const storyIndexers = await options.presets.apply('storyIndexers', []);
const docsOptions = await options.presets.apply<DocsOptions>('docs', {});
const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers,
docs: docsOptions,
workingDir,
storiesV2Compatibility: !features?.breakingChangesV7 && !features?.storyStoreV7,
storyStoreV7: features?.storyStoreV7,
});
initializedStoryIndexGenerator = generator.initialize().then(() => generator);
useStoriesJson({
router,
initializedStoryIndexGenerator,
normalizedStories,
serverChannel,
workingDir,
});
} catch (err) {
if (!core?.disableTelemetry) {
telemetry('start');
}
throw err;
}
}
if (!core?.disableTelemetry) {
initializedStoryIndexGenerator.then(async (generator) => {
const storyIndex = await generator?.getIndex();
const payload = storyIndex
? {
storyIndex: {
storyCount: Object.keys(storyIndex.entries).length,
version: storyIndex.v,
},
}
: undefined;
telemetry('start', payload, { configDir: options.configDir });
});
}
if (!core?.disableProjectJson) {
useStorybookMetadata(router, options.configDir);
}
app.use(compression({ level: 1 }));
if (typeof options.extendServer === 'function') {
options.extendServer(server);
}
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
// These headers are required to enable SharedArrayBuffer
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
next();
});
if (core?.crossOriginIsolated) {
app.use((req, res, next) => {
// These headers are required to enable SharedArrayBuffer
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
res.header('Cross-Origin-Opener-Policy', 'same-origin');
res.header('Cross-Origin-Embedder-Policy', 'require-corp');
next();
});
}
// User's own static files
await useStatics(router, options);
getMiddleware(options.configDir)(router);
app.use(router);
const { port, host } = options;
const proto = options.https ? 'https' : 'http';
const { address, networkAddress } = getServerAddresses(port, host, proto);
await new Promise<void>((resolve, reject) => {
// FIXME: Following line doesn't match TypeScript signature at all 🤔
// @ts-expect-error (Converted from ts-ignore)
server.listen({ port, host }, (error: Error) => (error ? reject(error) : resolve()));
});
const [previewBuilder, managerBuilder] = await getBuilders(options);
if (options.debugWebpack) {
logConfig('Preview webpack config', await previewBuilder.getConfig(options));
}
const managerResult = await managerBuilder.start({
startTime,
options,
router,
server,
});
let previewResult;
if (!options.ignorePreview) {
try {
previewResult = await previewBuilder.start({
startTime,
options,
router,
server,
});
} catch (error) {
await managerBuilder?.bail();
throw error;
}
}
// TODO #13083 Move this to before starting the previewBuilder - when compiling the preview is so fast that it will be done before the browser is done opening
if (!options.ci && !options.smokeTest && options.open) {
openInBrowser(host ? networkAddress : address);
}
return { previewResult, managerResult, address, networkAddress };
}