-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.js
94 lines (83 loc) · 2.14 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
/**
* Webpack configuration.
*/
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserJsPlugin = require("terser-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const JS_DIR = path.resolve(__dirname, "assets/js");
const IMG_DIR = path.resolve(__dirname, "assets/img");
const BUILD_DIR = path.resolve(__dirname, "build");
const entry = {
main: JS_DIR + "/index.js",
};
const output = {
path: BUILD_DIR,
filename: "js/[name].js",
};
const plugins = (argv) => [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: "production" === argv.mode,
}),
new MiniCssExtractPlugin({
filename: "css/[name].css",
}),
new CopyPlugin({
patterns: [{ from: IMG_DIR, to: BUILD_DIR + "/img" }],
}),
];
const rules = [
{
test: /\.(?:js|mjs|cjs)$/,
include: [JS_DIR],
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [["@babel/preset-env", { targets: "defaults" }]],
},
},
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
},
{
test: /\.(png|jpg|svg|jpeg|gif|ico)$/,
use: {
loader: "file-loader",
options: {
name: "[path][name].[ext]",
publicPath: "production" === process.env.NODE_ENV ? "../" : "../../",
},
},
},
{
test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
exclude: [IMG_DIR, /node_modules/],
use: {
loader: "file-loader",
options: {
name: "[path][name].[ext]",
publicPath: "production" === process.env.NODE_ENV ? "../" : "../../",
},
},
},
];
module.exports = (env, argv) => ({
entry,
output,
devtool: "source-map",
module: {
rules,
},
optimization: {
minimizer: [new CssMinimizerPlugin(), new TerserJsPlugin()],
minimize: true,
},
plugins: plugins(argv),
externals: {},
});