-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
103 lines (86 loc) · 2.41 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
/**
* Configuration webpack.
*
* @link https://imranhsayed.medium.com/set-up-webpack-and-babel-for-your-wordpress-theme-4ab56a00c873
*
*/
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const SRC_DIR = path.resolve(__dirname, 'src');
const JS_DIR = path.resolve(__dirname, 'src/js');
const PAGES_DIR = path.resolve(__dirname, 'src/js/pages');
const BUILD_DIR = path.resolve(__dirname, 'dist');
const entry = {
index: SRC_DIR + '/index.js',
// Separated compilation file (for any other page added after this line you need restart webpack)
home: PAGES_DIR + '/home.js',
};
const output = {
path: BUILD_DIR,
filename: 'js/[name].bundle.js',
clean: true
};
const rules = [
{
test: /\.js$/,
include: [JS_DIR],
exclude: /node_modules/,
use: 'babel-loader'
}, {
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
],
}, {
test: /\.(png|jpg|svg|jpeg|gif|ico)$/,
type: 'asset/resource',
},
{
test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
type: 'asset/resource',
}
];
const plugins = (argv) => [
new CleanWebpackPlugin({
cleanStateWebpackAssets: ('production' === argv.mode),
}),
new MiniCssExtractPlugin({
filename: 'css/[name].bundle.css'
}),
];
module.exports = (env, argv) => ({
entry: entry,
output: output,
devtool: 'source-map',
module: {
rules: rules,
},
optimization: {
minimize: 'production' === process.env.NODE_ENV ? true : false,
minimizer: [
new CssMinimizerPlugin({
parallel: 4,
minimizerOptions: {
preset: [
"default",
{
discardComments: { removeAll: 'production' === process.env.NODE_ENV ? true : false },
},
],
},
}),
new TerserPlugin({
parallel: 4,
}),
]
},
plugins: plugins(argv),
externals: {
jquery: 'jQuery'
}
});