-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.js
199 lines (177 loc) · 5 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Webpack base config block.
*
* @see https://webpack.github.io/docs/configuration.html
*/
const core = require('@webpack-blocks/core')
const webpack = require('webpack')
const webpackMerge = require('webpack-merge')
const path = require('path')
const parseVersion = require('./lib/parseVersion')
const webpackVersion = parseVersion(require('webpack/package.json').version)
exports.env = core.env
exports.group = core.group
exports.match = core.match
exports.when = core.when
exports.createConfig = createConfig
exports.setMode = setMode
exports.addPlugins = addPlugins
exports.customConfig = customConfig
exports.defineConstants = require('./lib/defineConstants')
exports.setEnv = require('./lib/setEnv')
exports.entryPoint = entryPoint
exports.performance = performance
exports.optimization = optimization
exports.resolve = resolve
exports.setContext = setContext
exports.setDevTool = setDevTool
exports.setOutput = setOutput
exports.sourceMaps = sourceMaps
/**
* Takes an array of webpack blocks and creates a webpack config out of them.
* Each webpack block is a callback function which will be invoked to return a
* partial webpack config. These partial configs are merged to create the
* final, complete webpack config that will be returned.
*
* Wraps @webpack-blocks/core's `createConfig`.
*
* @param {Function[]} configSetters Array of functions as returned by webpack blocks.
* @return {object} Webpack config object.
*/
function createConfig(configSetters) {
return core.createConfig({ webpack, webpackVersion }, [createEmptyConfig].concat(configSetters))
}
function createEmptyConfig(context, util) {
return util.merge({
module: {
rules: []
},
plugins: []
})
}
/**
* @see https://webpack.js.org/concepts/mode
*/
function setMode(mode) {
return (context, util) => {
context.mode = mode
return util.merge({ mode })
}
}
/**
* Adds one or multiple entry points. If the parameter is not an object the
* entry point(s) will be added to the default chunk named `main`.
*
* @param {object|string[]|string} entry
* @see https://webpack.github.io/docs/configuration.html#entry
*/
function entryPoint(entry) {
return (context, util) =>
util.merge({
entry: normalizeEntry(entry)
})
}
function normalizeEntry(entry) {
if (Array.isArray(entry)) {
return {
main: entry
}
} else if (typeof entry === 'string') {
return {
main: [entry]
}
} else if (typeof entry === 'object') {
Object.keys(entry).forEach(entryName => {
if (!Array.isArray(entry[entryName])) {
entry[entryName] = [entry[entryName]]
}
})
return entry
} else {
throw new Error(`Expected entry point to be object, array or string. Instead got: ${entry}`)
}
}
/**
* @see https://webpack.github.io/docs/configuration.html#plugins
*/
function addPlugins(plugins) {
return (context, util) => util.merge({ plugins })
}
function customConfig(wpConfig) {
return (context, util) => util.merge(wpConfig)
}
/**
* @param {object} performanceBudget
* @param {number} performanceBudget.maxAssetSize
* @param {number} performanceBudget.maxEntrypointSize
* @param {string} performanceBudget.hints 'warning' or 'error'
*/
function performance(performanceBudget) {
return (context, util) =>
util.merge({
performance: performanceBudget
})
}
/**
* @param {object} optimizationOptions
* @see https://webpack.js.org/configuration/optimization/
*/
function optimization(optimizationOptions) {
return (context, util) =>
util.merge({
optimization: optimizationOptions
})
}
/**
* @see https://webpack.js.org/configuration/resolve/
*/
function resolve(config) {
const strategy = { 'resolve.extensions': 'prepend' }
const merge = webpackMerge.smartStrategy(strategy)
return () => prevConfig =>
merge(prevConfig, {
resolve: config
})
}
/**
* @see https://webpack.github.io/docs/configuration.html#context
*/
function setContext(contextPath) {
return (context, util) =>
util.merge({
context: path.resolve(contextPath)
})
}
/**
* @see https://webpack.github.io/docs/configuration.html#devtool
*/
function setDevTool(devtool) {
return (context, util) => util.merge({ devtool })
}
/**
* @see https://webpack.github.io/docs/configuration.html#output
*/
function setOutput(output) {
if (typeof output === 'string') {
output = {
filename: path.basename(output) || 'bundle.js',
path: path.resolve(path.dirname(output) || './build')
}
}
return (context, util) => util.merge({ output })
}
/**
* Just a convenience wrapper to enable sourcemaps in an easier-to-read fashion
* than `setDevTool()`.
* @TODO: Only sets the javascript sourcemaps now. Would be nice to make loaders
* enable their specific sourcemaps when `sourceMaps()` is used.
*
* @param {string} [devtool]
* @return {Function}
*/
function sourceMaps(devtool = 'cheap-module-eval-source-map') {
return (context, util) => {
context.sourceMaps = true
return util.merge({ devtool })
}
}