1 | const open = require('opn');
|
2 | const merge = require('deepmerge');
|
3 |
|
4 | const isLocal = host => host === 'localhost' || host === '127.0.0.1';
|
5 | const getHost = publicHost => (isLocal(publicHost) ? 'localhost' : '0.0.0.0');
|
6 | const 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 |
|
21 | module.exports = (neutrino, opts = {}) => {
|
22 | const publicHost = getPublic(neutrino, opts);
|
23 | const host = getHost(publicHost);
|
24 | const options = merge.all([
|
25 | {
|
26 | port: 5000,
|
27 | https: false,
|
28 | contentBase: neutrino.options.source,
|
29 | open: false,
|
30 | hot: true,
|
31 | historyApiFallback: true,
|
32 | publicPath: '/',
|
33 | headers: {
|
34 | host: publicHost
|
35 | },
|
36 | stats: {
|
37 | assets: false,
|
38 | children: false,
|
39 | chunks: false,
|
40 | colors: true,
|
41 | errors: true,
|
42 | errorDetails: true,
|
43 | hash: false,
|
44 | modules: false,
|
45 | publicPath: false,
|
46 | timings: false,
|
47 | version: false,
|
48 | warnings: true
|
49 | }
|
50 | },
|
51 | opts,
|
52 | { host, public: publicHost },
|
53 | neutrino.options.port ? { port: neutrino.options.port } : {},
|
54 | neutrino.options.https ? { https: neutrino.options.https } : {}
|
55 | ]);
|
56 | const protocol = options.https ? 'https' : 'http';
|
57 | const url = `${protocol}://${publicHost}:${options.port}`;
|
58 |
|
59 | neutrino.config
|
60 | .devServer
|
61 | .merge(options)
|
62 | .end()
|
63 | .when(options.open, () => neutrino.on('start', () => open(url, { wait: false })))
|
64 | .entry('index')
|
65 | .when(options.hot, entry => entry.prepend(require.resolve('webpack/hot/dev-server')))
|
66 | .prepend(`${require.resolve('webpack-dev-server/client')}?${url}`);
|
67 | };
|