UNPKG

1.91 kBJavaScriptView Raw
1'use strict';
2
3/* eslint-disable
4 no-shadow,
5 no-param-reassign,
6 array-bracket-spacing,
7 space-before-function-paren
8*/
9const webpack = require('webpack');
10const createDomain = require('./createDomain');
11
12function addEntries(config, options, server) {
13 if (options.inline !== false) {
14 // we're stubbing the app in this method as it's static and doesn't require
15 // a server to be supplied. createDomain requires an app with the
16 // address() signature.
17 const app = server || {
18 address() {
19 return { port: options.port };
20 },
21 };
22
23 const domain = createDomain(options, app);
24 const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
25 const entries = [
26 `${require.resolve('../../client/')}?${domain}${sockPath}`,
27 ];
28
29 if (options.hotOnly) {
30 entries.push(require.resolve('webpack/hot/only-dev-server'));
31 } else if (options.hot) {
32 entries.push(require.resolve('webpack/hot/dev-server'));
33 }
34
35 const prependEntry = (entry) => {
36 if (typeof entry === 'function') {
37 return () => Promise.resolve(entry()).then(prependEntry);
38 }
39
40 if (typeof entry === 'object' && !Array.isArray(entry)) {
41 const clone = {};
42
43 Object.keys(entry).forEach((key) => {
44 clone[key] = entries.concat(entry[key]);
45 });
46
47 return clone;
48 }
49
50 return entries.concat(entry);
51 };
52
53 [].concat(config).forEach((config) => {
54 config.entry = prependEntry(config.entry || './src');
55
56 if (options.hot || options.hotOnly) {
57 config.plugins = config.plugins || [];
58 if (
59 !config.plugins.find(
60 (plugin) =>
61 plugin.constructor === webpack.HotModuleReplacementPlugin
62 )
63 ) {
64 config.plugins.push(new webpack.HotModuleReplacementPlugin());
65 }
66 }
67 });
68 }
69}
70
71module.exports = addEntries;