/
create-server.ts
51 lines (45 loc) · 1.31 KB
/
create-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
import webpack from 'webpack';
import WebpackDevServer, { Configuration } from 'webpack-dev-server';
import makeWebpackConfig from './make-webpack-config';
import * as Rsg from '../typings';
export default function createServer(
config: Rsg.SanitizedStyleguidistConfig,
env: 'development' | 'production' | 'none'
): { app: WebpackDevServer; compiler: webpack.Compiler } {
const webpackConfig = makeWebpackConfig(config, env);
const baseConfig: Partial<Configuration> = {
host: config.serverHost,
port: config.serverPort,
compress: true,
hot: true,
client: {
logging: 'none',
},
static: Array.isArray(config.assetsDir)
? config.assetsDir.map((assetsDir) => ({
directory: assetsDir,
watch: true,
publicPath: '/',
}))
: {
directory: config.assetsDir,
watch: true,
publicPath: '/',
},
devMiddleware: {
stats: webpackConfig.stats || {},
},
};
// Allow custom devServer options to override base config.
webpackConfig.devServer = {
...baseConfig,
...webpackConfig.devServer,
};
const compiler = webpack(webpackConfig);
const devServer = new WebpackDevServer(webpackConfig.devServer, compiler);
// User defined customizations
if (config.configureServer) {
config.configureServer((devServer as any).app, env);
}
return { app: devServer, compiler };
}