-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
dev-server.js
163 lines (143 loc) · 5.08 KB
/
dev-server.js
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
import path from 'path';
import { Router } from 'express';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import { logger } from '@storybook/node-logger';
import { getMiddleware } from './utils/middleware';
import { logConfig } from './logConfig';
import loadConfig from './config';
import loadManagerConfig from './manager/manager-config';
const dllPath = path.join(__dirname, '../../dll');
const cache = {};
let previewProcess;
let previewReject;
let resolved = false;
const router = new Router();
export default function(options) {
const configDir = path.resolve(options.configDir);
const outputDir = path.resolve(options.outputDir || path.join(__dirname, '..', 'public'));
const configType = 'DEVELOPMENT';
const startTime = process.hrtime();
let managerTotalTime;
let previewTotalTime;
const managerPromise = loadManagerConfig({
configType,
outputDir,
configDir,
cache,
corePresets: [require.resolve('./manager/manager-preset.js')],
...options,
}).then(config => {
if (options.debugWebpack) {
logConfig('Manager webpack config', config, logger);
}
return new Promise((resolve, reject) => {
webpack(config).watch(
{
aggregateTimeout: 1,
ignored: /node_modules/,
},
(err, stats) => {
managerTotalTime = process.hrtime(startTime);
if (!resolved && (err || stats.hasErrors())) {
const error = new Error('Manager build is broken');
error.error = err;
error.close = true;
error.stats = stats;
stats.toJson(options.errorDetails);
logger.line();
logger.line();
try {
previewReject(error);
previewProcess.close();
logger.warn('force closed preview build');
} catch (e) {
logger.warn('Unable to close preview build!');
}
logger.line();
reject(error);
} else {
resolve(stats);
}
}
);
});
});
const previewPromise = options.ignorePreview
? new Promise(resolve => resolve())
: loadConfig({
configType,
outputDir,
cache,
corePresets: [require.resolve('./preview/preview-preset.js')],
overridePresets: [require.resolve('./preview/custom-webpack-preset.js')],
...options,
}).then(previewConfig => {
if (options.debugWebpack) {
logConfig('Preview webpack config', previewConfig, logger);
}
// remove the leading '/'
let { publicPath } = previewConfig.output;
if (publicPath[0] === '/') {
publicPath = publicPath.slice(1);
}
const previewCompiler = webpack(previewConfig);
const devMiddlewareOptions = {
publicPath: previewConfig.output.publicPath,
watchOptions: {
aggregateTimeout: 1,
ignored: /node_modules/,
...(previewConfig.watchOptions || {}),
},
// this actually causes 0 (regular) output from wdm & webpack
logLevel: 'warn',
clientLogLevel: 'warning',
noInfo: true,
...previewConfig.devServer,
};
const webpackDevMiddlewareInstance = webpackDevMiddleware(
previewCompiler,
devMiddlewareOptions
);
router.use(webpackDevMiddlewareInstance);
router.use(webpackHotMiddleware(previewCompiler));
return new Promise((resolve, reject) => {
previewReject = reject;
webpackDevMiddlewareInstance.waitUntilValid(stats => {
previewTotalTime = process.hrtime(startTime);
if (!stats) {
reject(new Error('no stats after building preview'));
} else if (stats.hasErrors()) {
reject(stats);
} else {
resolve(stats);
}
});
previewProcess = webpackDevMiddlewareInstance;
});
});
// custom middleware
const middlewareFn = getMiddleware(configDir);
middlewareFn(router);
return Promise.all([managerPromise, previewPromise]).then(([managerStats, previewStats]) => {
resolved = true;
router.get('/', (request, response) => {
response.set('Content-Type', 'text/html');
response.sendFile(path.join(`${outputDir}/index.html`));
});
router.get(/\/sb_dll\/(.+\.js)$/, (request, response) => {
response.set('Content-Type', 'text/javascript');
response.sendFile(path.join(`${dllPath}/${request.params[0]}`));
});
router.get(/\/sb_dll\/(.+\.LICENCE)$/, (request, response) => {
response.set('Content-Type', 'text/html');
response.sendFile(path.join(`${dllPath}/${request.params[0]}`));
});
router.get(/(.+\.js)$/, (request, response) => {
response.set('Content-Type', 'text/javascript');
response.sendFile(path.join(`${outputDir}/${request.params[0]}`));
});
return { previewStats, managerStats, managerTotalTime, previewTotalTime, router };
});
}