UNPKG

1.75 kBJavaScriptView Raw
1const open = require('opn');
2const merge = require('deepmerge');
3
4const isLocal = host => host === 'localhost' || host === '127.0.0.1';
5const getHost = publicHost => (isLocal(publicHost) ? 'localhost' : '0.0.0.0');
6const getPublic = (neutrino, options) => {
7 if (options.public) {
8 return options.public;
9 }
10
11 if (neutrino.options.host) {
12 return isLocal(neutrino.options.host) ? 'localhost' : neutrino.options.host;
13 }
14
15 return !options.host || isLocal(options.host) ?
16 'localhost' :
17 options.host;
18};
19
20
21module.exports = (neutrino, opts = {}) => {
22 const port = opts.port || 5000;
23 const publicHost = getPublic(neutrino, opts);
24 const host = getHost(publicHost);
25
26 const options = merge.all([
27 {
28 port,
29 https: false,
30 contentBase: neutrino.options.source,
31 open: false,
32 hot: true,
33 historyApiFallback: true,
34 publicPath: '/',
35 headers: {
36 host: `${publicHost}:${port}`
37 },
38 stats: {
39 assets: false,
40 children: false,
41 chunks: false,
42 colors: true,
43 errors: true,
44 errorDetails: true,
45 hash: false,
46 modules: false,
47 publicPath: false,
48 timings: false,
49 version: false,
50 warnings: true
51 }
52 },
53 opts,
54 { host, public: `${publicHost}:${port}` },
55 neutrino.options.port ? { port: neutrino.options.port } : {},
56 neutrino.options.https ? { https: neutrino.options.https } : {}
57 ]);
58 const protocol = options.https ? 'https' : 'http';
59 const url = `${protocol}://${publicHost}:${options.port}`;
60
61 neutrino.config
62 .devServer
63 .merge(options)
64 .when(options.open, () => {
65 neutrino.on('start', () => open(url, { wait: false }));
66 });
67};