-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.js
69 lines (58 loc) · 1.93 KB
/
index.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
/**
* Webpack dev-server block.
*
* @see https://github.com/webpack/webpack-dev-server
*/
module.exports = devServer
/**
* @param {object} [options] See https://webpack.js.org/configuration/dev-server/
* @param {string|string[]} [entry]
* @return {Function}
*/
function devServer(options = {}, entry = []) {
if (options && (typeof options === 'string' || Array.isArray(options))) {
entry = options
options = {}
}
if (!Array.isArray(entry)) {
entry = entry ? [entry] : []
}
const setter = context => prevConfig => {
context.devServer = context.devServer || { entry: [], options: {} }
context.devServer.entry = context.devServer.entry.concat(entry)
context.devServer.options = Object.assign({}, context.devServer.options, options)
return prevConfig
}
return Object.assign(setter, { post: postConfig })
}
function postConfig(context, util) {
const entryPointsToAdd = context.devServer.entry
return prevConfig => {
return util.merge({
devServer: Object.assign(
{
hot: true,
hotOnly: true,
historyApiFallback: true,
inline: true,
// Disable verbose logging in browser’s console, only print errors
clientLogLevel: 'error',
// Do not print chunks list on every compilation, only print errors
stats: 'errors-only'
},
context.devServer.options
),
entry: addDevEntryToAll(prevConfig.entry || {}, entryPointsToAdd),
plugins: [new context.webpack.HotModuleReplacementPlugin()]
})(prevConfig)
}
}
function addDevEntryToAll(presentEntryPoints, devServerEntry) {
const newEntryPoints = {}
Object.keys(presentEntryPoints).forEach(chunkName => {
// It's fine to just set the `devServerEntry`, instead of concat()-ing the present ones.
// Will be concat()-ed by webpack-merge (see `createConfig()`)
newEntryPoints[chunkName] = devServerEntry
})
return newEntryPoints
}