-
Notifications
You must be signed in to change notification settings - Fork 13
/
webpack.config.js
308 lines (282 loc) · 8.78 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
/**
* @file Manages the root configuration settings for webpack.
* @see {@link https://webpack.js.org/} For further information.
*/
const path = require('path');
const childProcess = require('child_process');
const webpack = require('webpack');
const merge = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const eslintFriendlyFormatter = require('eslint-friendly-formatter');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const camelCase = require('lodash/camelCase');
const globalObject = require('webpack-global-object-x');
const PACKAGE = require('./package.json');
const filename = PACKAGE.name.replace('@xotic750/', '');
const library = camelCase(filename);
const dist = path.resolve(__dirname, 'dist');
/**
* The NODE_ENV environment variable.
*
* @type {!object}
*/
const {NODE_ENV} = process.env;
/**
* The production string.
*
* @type {string}
*/
const PRODUCTION = 'production';
/**
* The development string.
*
* @type {string}
*/
const DEVELOPMENT = 'development';
/**
* The default include paths.
*
* @type {string}
*/
const DEFAULT_INCLUDE = [path.resolve(__dirname, 'src'), path.resolve(__dirname, '__tests__')];
/**
* Allows you to pass in as many environment variables as you like using --env.
* See {@link http://webpack.js.org/guides/environment-variables}.
*
* @param {!object} [env={}] - The env object.
* @returns {undefined} Default.
*/
module.exports = function generateConfig(env) {
/**
* The reference created bu git describe --dirty`.
*
* @type {string}
* @see {@link https://git-scm.com/docs/git-describe}
*/
const DESCRIBE = childProcess
.spawnSync('git', ['describe', '--dirty'])
.output[1].toString()
.trim();
/**
* The date as of now.
*
* @type {string}
*/
const NOW = new Date().toISOString();
const base = {
/**
* This option controls if and how source maps are generated.
*
* Nosources-source-map - A SourceMap is created without the sourcesContent in it.
* It can be used to map stack traces on the client without exposing all of the
* source code. You can deploy the Source Map file to the web-server.
*
* Eval-source-map - Each module is executed with eval() and a SourceMap is added as
* a DataUrl to the eval(). Initially it is slow, but it provides fast rebuild speed
* and yields real files. Line numbers are correctly mapped since it gets mapped to
* the original code. It yields the best quality SourceMaps for development.
*
* Source-map - A full SourceMap is emitted as a separate file. It adds a reference
* comment to the bundle so development tools know where to find it.
*
* @type {string}
* @see {@link https://webpack.js.org/configuration/devtool/}
*/
devtool: 'source-map',
/**
* Define the entry points for the application.
*
* @type {Array.<string>}
* @see {@link https://webpack.js.org/concepts/entry-points/}
*/
entry: PACKAGE.module,
mode: NODE_ENV === PRODUCTION ? PRODUCTION : DEVELOPMENT,
/**
* In modular programming, developers break programs up into discrete chunks of functionality
* called a module. Each module has a smaller surface area than a full program, making verification,
* debugging, and testing trivial. Well-written modules provide solid abstractions and encapsulation
* boundaries, so that each module has a coherent design and a clear purpose within the overall
* application.
*
* Webpack supports modules written in a variety of languages and preprocessors, via loaders.
* Loaders describe to webpack how to process non-JavaScript modules and include these dependencies
* into your bundles.
*
* @type {Array.<!object>}
* @see {@link https://webpack.js.org/configuration/module/#module-rules}
*/
module: {
rules: [
/**
* Extract sourceMappingURL comments from modules and offer it to webpack.
*
* @see {@link https://github.com/webpack-contrib/source-map-loader}
*/
{
enforce: 'pre',
loader: 'source-map-loader',
test: /\.js$/,
},
/**
* Eslint-loader options.
*
* @type {!object}
* @see {@link https://github.com/MoOx/eslint-loader}
*/
{
enforce: 'pre',
include: DEFAULT_INCLUDE,
loader: 'eslint-loader',
options: {
emitError: true,
emitWarning: false,
failOnError: true,
failOnWarning: false,
formatter: eslintFriendlyFormatter,
quiet: true,
},
test: /\.(js|json)$/,
},
/**
* This package allows transpiling JavaScript files using Babel and webpack.
*
* @see {@link https://webpack.js.org/loaders/babel-loader/}
*/
{
include: DEFAULT_INCLUDE,
loader: 'babel-loader',
test: /\.js$/,
},
],
},
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
node: {
child_process: 'empty',
dgram: 'empty',
fs: 'empty',
net: 'empty',
// prevent webpack from injecting useless setImmediate polyfill.
setImmediate: false,
tls: 'empty',
},
/**
* Configuring the output configuration options tells webpack how to write the compiled
* files to disk.
*
* @type {!object}
* @see {@link https://webpack.js.org/configuration/output/}
*/
output: {
// https://github.com/webpack/webpack/issues/6525
globalObject: `(${globalObject.toString()}())`,
library,
libraryTarget: 'umd',
path: dist,
},
/**
* Plugins are the backbone of webpack. Webpack itself is built on the same plugin system
* that you use in your webpack configuration!
*
* A webpack plugin is a JavaScript object that has an apply property. This apply property
* is called by the webpack compiler, giving access to the entire compilation lifecycle.
*
*/
plugins: [
/**
* Use the shorthand version.
*
* @type {!object}
* @see {@link https://webpack.js.org/plugins/environment-plugin/}
*/
new webpack.EnvironmentPlugin({
DEBUG: false, // use 'false' unless process.env.DEBUG is defined.
NODE_ENV: DEVELOPMENT, // use 'development' unless process.env.NODE_ENV is defined.
}),
/**
* Smaller lodash builds. We are not opting in to path feature.
*
* @type {!object}
* @see {@link https://github.com/lodash/lodash-webpack-plugin}
*/
new LodashModuleReplacementPlugin({
paths: true,
}),
/**
* Adds a banner to the top of each generated chunk.
*
* @type {!object}
* @see {@link https://webpack.js.org/plugins/banner-plugin/}
*/
new webpack.BannerPlugin({
banner: `/*!\n${JSON.stringify(
{
author: PACKAGE.author.name,
copywrite: PACKAGE.copyright,
date: NOW,
describe: DESCRIBE,
description: PACKAGE.description,
file: '[file]',
hash: '[hash]',
license: PACKAGE.license,
version: PACKAGE.version,
},
null,
2,
)}\n*/`,
raw: true,
}),
],
/**
* These options change how modules are resolved.
*
* @type {!object}
* @see {@link https://webpack.js.org/configuration/resolve/}
*/
resolve: {
/**
* Create aliases to import or require certain modules more easily.
*
* @type {!object}
* @see {@link https://webpack.js.org/configuration/resolve/#resolve-alias}
*/
alias: {
RootDir: path.resolve(__dirname, '.'),
dist: path.resolve(__dirname, './dist'),
src: path.resolve(__dirname, './src'),
},
extensions: ['.js', '.json'],
},
};
const browser = merge(base, {
optimization: {
minimize: false,
},
output: {
filename: `${filename}.js`,
},
});
const minified = merge(browser, {
output: {
filename: `${filename}.min.js`,
},
/**
* Webpack plugin and CLI utility that represents bundle content as convenient
* interactive zoomable treemap.
*
* @see {@link https://github.com/webpack-contrib/webpack-bundle-analyzer}
*/
plugins: [
new TerserPlugin({
parallel: true,
sourceMap: true,
terserOptions: {
ecma: 5,
},
}),
...(env && env.report ? [new BundleAnalyzerPlugin()] : []),
],
});
return [browser, minified];
};