-
Notifications
You must be signed in to change notification settings - Fork 51
/
webpack.config.js
328 lines (296 loc) · 9.68 KB
/
webpack.config.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* This is the base config for all builds. It uses env.buildtype to change
* the output configuration for different build types.
*/
var webpack = require('webpack')
var autoprefixer = require('autoprefixer')
var join = require('path').join
var _ = require('lodash')
var postcssDiscardDuplicates = require('postcss-discard-duplicates')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var postcssImport = require('postcss-import')
var postcssCustomProperties = require('postcss-custom-properties')
var postcssCalc = require('postcss-calc')
var postcssColorFunction = require('postcss-color-function')
var postcssCustomMedia = require('postcss-custom-media')
var postcssEsplit = require('postcss-esplit')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var ManifestPlugin = require('webpack-manifest-plugin')
var cssNano = require('cssnano')
// `CheckerPlugin` is optional. Use it if you want async error reporting.
// We need this plugin to detect a `--watch` mode. It may be removed later
// after https://github.com/webpack/webpack/issues/3460 will be resolved.
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin
/**
* Helper so we can use ternary with undefined to not specify a key
* @param {any} obj
*/
function dropUndef (obj) {
return _(obj).omitBy(_.isNil).value()
}
var postCssLoader = {
loader: 'postcss-loader',
options: {
plugins: [
postcssDiscardDuplicates(),
postcssImport(),
postcssCustomProperties,
postcssCalc,
cssNano(),
postcssColorFunction,
postcssCustomMedia,
postcssEsplit({
quiet: true
}),
autoprefixer({
browsers: [
'Explorer >= 9',
'last 2 Chrome versions',
'last 2 Firefox versions',
'last 2 Safari versions',
'last 2 iOS versions',
'Android 4'
]
})
]
}
}
/**
* To set env on command line:
* webpack --env.buildtype=draft
*
* To set env in build scripts, just pass it as a normal function argument:
* import createConfig from '../webpack.config'
* const config = createConfig({ buildtype: 'draft' })
*
* Valid buildtype settings: prod (default), draft, dev, storybook.
*
* More info:
* https://blog.flennik.com/the-fine-art-of-the-webpack-2-config-dc4d19d7f172
* @param {any} env
* @param {boolean=} isEditor
* @param {number=} devServerPort
*/
module.exports = function (env, isEditor, devServerPort) {
var buildtype = env && env.buildtype || 'prod'
const distDir = isEditor ? 'dist.editor' : 'dist'
/**
* Development build.
*
* This build is meant to use with webpack-dev-server for hot redeployment. It
* should be optimised primarily for in-browser debugging, then for fast
* incremental builds.
*/
var dev = buildtype === 'dev'
/**
* Storybook config.
*
* Used by storybook to build individual components. Should inline the css and
* optimize for fast incremental builds.
*/
var storybook = buildtype === 'storybook'
/**
* Draft build (the fast build).
*
* This build is intended to build as fast as possible, while being
* semantically the same as the production build.
*
* The output is not optimised for artifact size, human-readability or to work
* well with development tools or debuggers. Use the prod or dev config if you
* want those properties in the output.
*/
var draft = buildtype === 'draft'
/**
* Production build config.
*
* This should be optimised for production performance and a small download.
* Builds with this config should fail on any error, including linting errors.
*/
var prod = buildtype === 'prod'
// several options have the same values for both draft and prod
var fullBuild = draft || prod
require.extensions['.css'] = () => {
return
}
return dropUndef({
entry: storybook ? undefined : dropUndef({
'frontend': './app/entrypoint/index',
'editor': './app/editor/entrypoint/index.js',
'frontend.legacy': fullBuild ? './app/entrypoint/legacy' : undefined
}),
output: storybook ? undefined : dropUndef({
path: join(__dirname, distDir),
filename: fullBuild ? '[name].[chunkhash:8].cache.js' : '[name].js',
chunkFilename: fullBuild ? '[name].[chunkhash:8].cache.js' : '[name].js',
// includes comments in the generated code about where the code came from
pathinfo: dev,
// required for hot module replacement
// or is it https://github.com/webpack-contrib/style-loader/issues/55 ?
publicPath: dev
? `http://localhost:${devServerPort}/`
: undefined
}),
module: {
rules: _.compact([
/* Checks for errors in syntax, and for problematic and inconsistent
* code in all JavaScript files.
* Configured in .eslintrc
*/
// TODO consider turning on for storybook
storybook ? undefined : {
test: /\.jsx?$/,
exclude: /node_modules/,
enforce: 'pre',
loader: 'eslint-loader',
options: {
failOnWarning: !dev,
failOnError: !dev
}
},
// TODO consider turning on for storybook
storybook ? undefined : {
test: /\.tsx?$/,
exclude: /node_modules/,
enforce: 'pre',
loader: 'tslint-loader',
options: {
failOnHint: !dev,
formatter: 'verbose'
}
},
/* Transpiles JS/JSX/TS/TSX files through TypeScript (tsc)
*/
{
test: /\.(j|t)sx?$/,
exclude: /node_modules/,
include: join(__dirname, 'app'),
loader: 'awesome-typescript-loader'
},
/* TODO:
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
*/
/* Bundles all the css and allows use of various niceties, including
* imports, variables, calculations, and non-prefixed codes.
* The draft and prod options were removed as they were causing
* errors with css-loader. In both cases, the css is minified.
*/
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: _.compact([
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
postCssLoader
])
})
},
/* Bundles bootstrap css into the same bundle as the other css.
*/
{
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'postcss-loader',
options: {
plugins: [
require('stylelint'),
require('postcss-discard-duplicates')
]
}
},
postCssLoader,
'less-loader'
]
})
}
])
},
plugins: _.compact([
// This makes it easier to see if watch has picked up changes yet.
// https://github.com/webpack/webpack/issues/1499#issuecomment-155064216
// There's probably a config option for this (stats?) but I can't find it.
function () {
this.plugin('watch-run',
/**
* @param {any} _watching
* @param {any} callback
*/
function (_watching, callback) {
// eslint-disable-next-line no-console
console.log('Begin compile at ' + new Date())
callback()
})
},
new CheckerPlugin(),
/* Outputs css to a separate file per entry-point.
Note the call to .extract above */
new ExtractTextPlugin({
filename: '[name].[chunkhash:8].cache.css',
// storybook should use the fallback: style-loader
disable: storybook || dev
}),
// new webpack.NoEmitOnErrorsPlugin(),
prod
? new webpack.optimize.UglifyJsPlugin({ sourceMap: true })
: undefined,
prod
// Workaround to switch old loaders to minimize mode
? new webpack.LoaderOptionsPlugin({ minimize: true })
: undefined,
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(
dev || storybook ? 'development' : 'production')
}
}),
fullBuild ? new webpack.HashedModuleIdsPlugin() : undefined,
// Convert source (en) and translated strings to the format the app
// can consume, in the dist directory.
fullBuild
? new CopyWebpackPlugin([
{
from: join(__dirname, 'messages/*.json'),
to: 'messages',
toType: 'dir',
flatten: true,
transform (content, path) {
// Minimize the JSON files
return JSON.stringify(JSON.parse(content))
}
}
])
: undefined,
new ManifestPlugin()
]),
optimization: {
// namedModules: true, // NamedModulesPlugin()
splitChunks: { // CommonsChunkPlugin()
name: 'vendor',
minChunks: 2
},
noEmitOnErrors: true // NoEmitOnErrorsPlugin
// concatenateModules: true // ModuleConcatenationPlugin
},
resolve: {
/* Subdirectories to check while searching up tree for module
* Default is ['', '.js'] */
extensions: ['.js', '.jsx', '.json', '.css', '.less', '.ts', '.tsx']
},
node: {
__dirname: true
},
/* Caching for incremental builds, only needed for dev mode */
cache: !fullBuild,
/* fail on first error */
bail: fullBuild,
devtool: prod ? 'source-map' : 'eval-source-map'
})
}