-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
481 lines (440 loc) · 17.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const RemoveBuildFile = require('remove-files-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
/**
* This variable represents the file name in './src/js' that remove after build.
* With this feature you can delete unnecessary files.
* this feature works in only 'production' mode
* e.g: const RemoveFileAfterBuild = ['file1.js', 'file2.js'];
*/
const RemoveFileAfterBuild = [];
/**
* If you want to copy a file (files) without changing it during of the build,
* You must complete this variable. These files are copied to the build folder.
* e.g:
const copyStaticFile = [
{ from: './src/js/script.js', to: './js/script.js' },
{ from: './src/css/style.css', to: './css/style.css' },
{ from: './src/LICENSE.txt', to: './LICENSE.txt' },
];
*/
const copyStaticFile = [];
/**
* entry webpack (The point or points where to start the application bundling process.)
* e.g:
const entry = {
index: './src/js/entry/index.js',
single: './src/js/entry/single.js',
};
*/
const entry = {
// Insert script files here ...
};
/**
*
* This variable represents the options of the 'postcss-base64' plugin, which is used in the 'postcss-loader' loader
* To convert photos and font to data-uri code, it's enough to end the file name to '.b64.[ext]'
* In other words, photos and fonts will not normally be converted to data-uri unless
* the final portion of the file name is terminated to '.b64.[ext]'
*/
const base64Options = {
extensions: ['.b64.jpg', '.b64.jpeg', '.b64.png', '.b64.svg', '.b64.woff', '.b64.woff2'],
root: './src/**',
excludeAtFontFace: false,
};
/**
* This variable represents the mode configuration of the plugin 'HtmlWebpackPlugin'
* If is equal 'development', It will not import css and javascript files into html output. (when compile)
*
* The default 'StaticFile' variable is false(an empty array), But If the 'copyStaticFile' variable has a value (copyStaticFile.lenght > 0)
* And is also a development mode, the first condition is set and the for loop is applied.
* Now if the extension of one of the 'copyStaticFile[i].to' valuse ends with js or css,
* the value of the 'StaticFile' variable will be true(An array of value)
* That value will be automatically added before the close body tag.
*
* 'excludeChunks' variable Allows we to skip some chunks (e.g don't add the unit-test chunk)
* This variable only works in development mode and it's automatically work with 'RemoveFileAfterBuild' variable.
*/
const mode = process.env.NODE_ENV;
let staticFile = [];
if(process.env.NODE_ENV === 'development' && copyStaticFile.length > 0){
// first add style
for(let i = 0, len = copyStaticFile.length; i < len; i++){
if(/\.css$/.test(copyStaticFile[i].to)){
staticFile.push(`<link rel="stylesheet" href="${copyStaticFile[i].to}">`);
};
}
// then add js
for(let i = 0, len = copyStaticFile.length; i < len; i++){
if(/\.js$/.test(copyStaticFile[i].to)){
staticFile.push(`<script src="${copyStaticFile[i].to}"></script>`);
};
}
}
let excludeChunks = [];
if(process.env.NODE_ENV === 'development' && RemoveFileAfterBuild.length > 0){
for(let i = RemoveFileAfterBuild.length; i--;){
if(/\.js$/.test(RemoveFileAfterBuild[i])){
// remove extention (main.js ==> main)
excludeChunks.push(RemoveFileAfterBuild[i].substr(0 , RemoveFileAfterBuild[i].length-3));
};
}
}
/**
*
* These config are used for both development and production modes
*
*/
const config = {
entry: entry,
output: {
path: path.resolve(__dirname, 'build'),
filename: 'js/[name].js',
},
performance: {
maxAssetSize: 1024000 // 1 MB
},
stats: 'errors-warnings',
devServer: {
contentBase: path.resolve(__dirname, 'build'),
index: 'index.html',
compress: true,
port: 3000,
stats: {
assets: false,
children: false,
chunks: false,
chunkModules: false,
colors: true,
entrypoints: false,
hash: false,
modules: false,
timings: false,
version: false,
logging: 'warn',
},
},
module: {
/**
* Loaders are evaluated/executed from right to left (or from bottom to top)
*/
rules: [],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/layouts/index.html',
filename: 'index.html',
inject: mode === 'production' ? false : true,
chunks: mode === 'production' ? [] : true,
excludeChunks: excludeChunks,
mode: mode,
staticFile: staticFile,
}),
new CopyPlugin(copyStaticFile),
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
messages: ['Your application is running here http://localhost:3000 (Only for watch mode)'],
},
}),
/**
* All files inside webpack's output.path directory will be removed once, but the
* directory itself will not be.
*/
new CleanWebpackPlugin(),
],
};
/**
*
* These settings are based on the specific mode type
*
*/
module.exports = (env, { mode }) => {
/**
* return true or false if mode is development or Prodoction.
*/
const isDevelopment = mode === 'development';
/**
* Development Mode
*/
if (isDevelopment) {
config.module.rules.push(
...[
{
// all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
test: /\.tsx?$/,
exclude: /(node_modules|webpack-plugins)/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
],
},
]
);
}
config.module.rules.push(
...[
{
// Apply rule for .css files
test: /\.css$/,
use: [
{
// extracts CSS into separate files
loader: isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
// Translates CSS into CommonJS
loader: 'css-loader',
options: isDevelopment ? { sourceMap: true } : {},
},
{
// In development mode the 'postcss-base64' plugin is used
// In production mode the 'autoprefixer', 'cssnano' & 'postcss-base64' plugins are used
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: isDevelopment ? true : false,
plugins: loader =>
isDevelopment
? [require('postcss-base64')(base64Options)]
: [
require('autoprefixer'),
require('cssnano')({
preset: 'default',
}),
require('postcss-base64')(base64Options),
],
},
},
],
},
{
// Apply rule for .sass, .scss files and compiles it to CSS
test: /\.s[ac]ss$/,
use: [
{
// extracts CSS into separate files
loader: isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
// Translates CSS into CommonJS
loader: 'css-loader',
options: isDevelopment ? { sourceMap: true } : {},
},
{
// In production mode the 'autoprefixer' plugin is used
// And in both mode (production and development) the 'cssnano' & 'postcss-base64' plugins are used.
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: isDevelopment ? true : false,
plugins: loader =>
isDevelopment
? [require('postcss-base64')(base64Options)]
: [
require('autoprefixer'),
require('cssnano')({
preset: 'default',
}),
require('postcss-base64')(base64Options),
],
},
},
{
// Compiles Sass to CSS
loader: 'sass-loader',
options: isDevelopment ? { sourceMap: true } : {},
},
],
},
{
// Apply rule for .less files and compiles it to CSS
test: /\.less$/,
use: [
{
// extracts CSS into separate files
loader: isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
// Translates CSS into CommonJS
loader: 'css-loader',
options: isDevelopment ? { sourceMap: true } : {},
},
{
// In production mode the 'autoprefixer' plugin is used
// And in both mode (production and development) the 'cssnano' & 'postcss-base64' plugins are used.
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: isDevelopment ? true : false,
plugins: loader =>
isDevelopment
? [require('postcss-base64')(base64Options)]
: [
require('autoprefixer'),
require('cssnano')({
preset: 'default',
}),
require('postcss-base64')(base64Options),
],
},
},
{
// Compiles less to CSS
loader: 'less-loader',
options: isDevelopment ? { sourceMap: true } : {},
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
publicPath: isDevelopment ? './images' : '../images',
outputPath: 'images',
name: '[name].[ext]',
},
},
{
loader: 'image-webpack-loader',
options: {
// This loader will be disabled when in development mode. (Images cannot be compressed and optimized)
disable: isDevelopment,
mozjpeg: {
progressive: true,
quality: 70,
},
optipng: {
enabled: true,
},
pngquant: {
quality: [0.7, 0.9],
speed: 4,
},
gifsicle: {
interlaced: false,
},
webp: {
quality: 80,
},
},
},
],
},
{
test: /\.(woff|woff2|ttf|eot|otf)$/,
use: [
{
loader: 'file-loader',
options: {
publicPath: isDevelopment ? './fonts' : '../fonts',
outputPath: 'fonts',
name: '[name].[ext]',
},
},
],
},
]
);
/**
* Prodoction Mode
*/
if (!isDevelopment) {
config.module.rules.push(
...[
{
test: /\.js$/,
exclude: /(node_modules|webpack-plugins)/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
plugins: [
'@babel/plugin-transform-classes',
'@babel/plugin-transform-arrow-functions',
'@babel/plugin-transform-shorthand-properties',
'@babel/plugin-transform-spread',
],
presets: [
[
'@babel/preset-env',
{
targets: {
chrome: '55',
firefox: '55',
safari: '11',
edge: '14',
},
},
],
],
},
},
},
{
// all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
test: /\.tsx?$/,
exclude: /(node_modules|webpack-plugins)/,
use: [
{
loader: 'babel-loader',
options: {
// cacheDirectory: true,
plugins: [
'@babel/plugin-transform-classes',
'@babel/plugin-transform-arrow-functions',
'@babel/plugin-transform-shorthand-properties',
'@babel/plugin-transform-spread',
],
presets: [
[
'@babel/preset-env',
{
targets: {
chrome: '55',
firefox: '55',
safari: '11',
edge: '14',
},
},
],
],
},
},
{
loader: 'ts-loader',
},
],
},
]
);
config.plugins.push(
...[
new RemoveBuildFile({
after: {
root: './build/js',
include: RemoveFileAfterBuild,
allowRootAndOutside: true,
},
}),
/**
* This plugin extracts CSS into separate files.
* It creates a CSS file per JS file which contains CSS.
*/
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
]
);
}
return config;
};