-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.utils.js
244 lines (229 loc) · 6.46 KB
/
webpack.utils.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
'use strict';
/**
* This file contains utilities for generating valid and useful webpack
* configuration options.
*
* @see - https://webpack.js.org/guides/production/
*
* The webpack configuration in this project isn't structured exactly the same
* as the webpack guide, but it's similar. This `utils` file is similar to the
* `common` file, the `clsp-player` file is similar to the `prod` file, and the
* `demos` file is similar to the `dev` file.
*
* @todo - would it be easier / more readable to use `webpack-merge`?
*/
const webpack = require('webpack');
const path = require('path');
const chalk = require('chalk');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const babelConfig = require('./babel.config')();
const isDevMode = process.env.NODE_ENV !== 'production';
/**
* @private
*
* Helper function to generate a formatted progress bar plugin for a webpack
* config.
*
* @see - https://www.npmjs.com/package/progress-bar-webpack-plugin
*
* @param {String} name
* The file / module name (typically the `entry` name) used for logging
* purposes
*/
function generateProgressBarPlugin (name) {
const building = chalk.bold(`Building ${name} page...`);
const bar = chalk.bgBlue.white.bold('[:bar]');
const percent = chalk.bold.green(':percent');
const elapsed = chalk.bold('(:elapsed seconds)');
return new ProgressBarPlugin({
format: ` ${building} ${bar} ${percent} ${elapsed}`,
clear: false,
});
}
/**
* The directory where the built / compiled files will go.
*
* @type {String}
*/
const outputPath = path.resolve(
__dirname,
'dist',
);
/**
* Generate a webpack configuration object. The resulting configuration object
* is currently suitable for all files this project exports.
*
* @see - https://webpack.js.org/configuration/
*
* @param {String} name
* The entry name, which will be used to construct the file name
* @param {String} entry
* The entry point for the webpack configuration - must point to a single file
*
* @returns {Object}
* A webpack configuration object
*/
function generateConfig (name, entry) {
return {
name,
entry: {
// @see - https://github.com/webpack-contrib/webpack-serve/issues/27
[name]: [
entry,
],
},
output: {
path: outputPath,
filename: '[name].js',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader?cacheDirectory=true',
options: babelConfig,
// @see - https://github.com/webpack/webpack/issues/2031
include: [
path.resolve(
__dirname,
'src',
),
path.resolve(
__dirname,
'demos',
),
// @see - https://github.com/visionmedia/debug/issues/668
path.resolve(
__dirname,
'node_modules',
'debug',
),
],
},
// @see - https://github.com/bensmithett/webpack-css-example/blob/master/webpack.config.js
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'url-loader',
},
{
// @see - https://github.com/webpack-contrib/mini-css-extract-plugin
// @see - https://github.com/webpack-contrib/sass-loader
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// set this to false to not interrupt ongoing tests until the
// developer is ready
hmr: false,
},
},
'css-loader',
// @todo
// 'postcss-loader',
'sass-loader',
],
},
],
},
resolve: {
alias: {
'~root': __dirname,
},
},
plugins: [
generateProgressBarPlugin(name),
new MiniCssExtractPlugin({}),
new WriteFilePlugin(),
],
};
}
/**
* Given an array of webpack configurations, add properties to them to make the
* resulting built / compiled files more suitable for a development environment.
*
* @see - https://webpack.js.org/configuration/mode/
*
* @param {Array} webpackConfigs
* An Array of webpack configuration objects
*
* @returns {Array}
* An Array of webpack configuration objects with added development
* configuration
*/
function exportAsDevConfig (webpackConfigs) {
return webpackConfigs.map((webpackConfig) => {
const config = {
...webpackConfig,
mode: 'development',
devtool: 'eval-source-map',
output: {
...webpackConfig.output,
pathinfo: true,
},
plugins: [
...webpackConfig.plugins,
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
},
}),
],
};
return config;
});
}
/**
* Given an array of webpack configurations, add properties to them to make the
* resulting built / compiled files more suitable for a production environment.
*
* @see - https://webpack.js.org/configuration/mode/
*
* @param {Array} webpackConfigs
* An Array of webpack configuration objects
*
* @returns {Array}
* An Array of webpack configuration objects with added production
* configuration
*/
function exportAsProdConfig (webpackConfigs) {
return webpackConfigs.map((webpackConfig) => {
const config = {
...webpackConfig,
mode: 'production',
cache: true,
// @todo - minimization breaks the plugin and player!
optimization: {
minimize: false,
},
plugins: [
...webpackConfig.plugins,
new webpack.DefinePlugin({
// This is needed to ensure that things like react and redux compile and
// minify properly - if you need access to this in code, use
// global.app.environment instead.
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
],
};
if (process.env.ANALYZE_BUILD) {
config.plugins.push(new BundleAnalyzerPlugin({
analyzerMode: 'static',
}));
}
config.output.filename = '[name].min.js';
return config;
});
}
module.exports = {
isDevMode,
outputPath,
generateConfig,
exportAsDevConfig,
exportAsProdConfig,
};