-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
125 lines (120 loc) · 4.02 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
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = () => {
// Set a valid environment - will default to production if an invalid value is set
const VALID_ENVIRONMENTS = ['production', 'development', 'none'];
const ENVIRONMENT = process.env.NODE_ENV.toLowerCase();
let mode = 'production';
if (VALID_ENVIRONMENTS.includes(ENVIRONMENT)) {
mode = ENVIRONMENT;
}
// Check for windows.
const IS_WINDOWS = process.platform === 'win32';
return {
/**
* Mode
* - Webpack 4 will do a number of modification tasks for you depending on the mode
*/
mode: mode,
/**
* WatchOptions
* - poll - as some windows environments cant watch file systems, use poll to reload on file changes
*/
watchOptions: {
poll: IS_WINDOWS ? 2000 : false
},
/**
* Entry
* - Define starting files for compilation here - each will be compiled as `<entry-key>.js`
*/
entry: {
app: path.resolve(__dirname, 'assets/js/app.js'),
},
/**
* Output
* - All files will be output with their entry-key as the name
*/
output: {
path: path.resolve(__dirname, 'assets/build/js'),
filename: '[name].js'
},
/**
* Devtool
* - Controls how source maps are generated
*/
devtool: 'cheap-source-map',
/**
* Module
* - Does the compilation!
* - Documentation on this can be found https://webpack.js.org/configuration/module/
*/
module: {
rules: [
/**
* Babel-loader - for use with older browsers we use babel-preset-env for compilation. See .browserslistrc for details on what browsers are targetted.
*/
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
modules: false,
corejs: 3
}
]
]
}
},
{
test: /\.(css|scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [require('autoprefixer')]
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
sourceMapContents: false
}
}
]
},
]
},
/**
* Optimization
* - Split chunks - to reduce bundle size, we split out all library/node_module bundles to a seperate vendor.js file
*/
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/].*\.js$/,
chunks: 'initial',
name: 'vendor',
enforce: true
}
}
}
},
/**
* Plugins
* MiniCssExtractPlugin - compile css down to a seperate CSS file
*/
plugins: [
new MiniCssExtractPlugin({
filename: '../../build/css/[name].css',
}),
].filter(Boolean)
};
};