-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
143 lines (139 loc) · 4.61 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
const { defineOptimization } = require("./webpack.utils/defineOptimization");
const CssMinimizerWebpackPlugin = require("css-minimizer-webpack-plugin");
const { generateFilename } = require("./webpack.utils/generateFilename");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const { definePlugins } = require("./webpack.utils/definePlugins");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const EslintWebpackPlugin = require("eslint-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");
const path = require("path");
const isProd = process.env.NODE_ENV === "production";
const isDev = !isProd;
const multipleStylesheetsBundlerConfig = (env, argv) => ({
mode: argv?.mode || "development",
context: path.resolve(__dirname, "src/assets/scss"),
entry: {
app: "./app.scss",
dashboard: "./dashboard.scss"
},
output: {
filename: "[name].[contenthash].bundle.js",
path: path.resolve(__dirname, "bundle")
},
optimization: {
splitChunks: {
chunks: "all"
}
},
module: {
rules: [
{
test: /\.s[ac]ss$/gi,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
}
]
},
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": argv?.mode ? JSON.stringify(argv.mode) : "development"
}),
new webpack.ProgressPlugin(),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].bundle.css"
})
]
});
const webpackMainConfig = (env, argv) => ({
mode: env?.development ? "development" : "production",
target: "web",
context: path.resolve(__dirname, "src"),
entry: {
app: ["core-js/stable", "regenerator-runtime/runtime", "./app.js"]
},
output: {
filename: generateFilename("js", env),
path: path.resolve(__dirname, "bundle"),
clean: true
},
optimization: defineOptimization(process.env.NODE_ENV, [new TerserWebpackPlugin(), new CssMinimizerWebpackPlugin()]),
resolve: {
extensions: [".js", "json"],
alias: {
"@": path.resolve(__dirname, "src"),
"@core": path.resolve(__dirname, "src", "core"),
"@framework": path.resolve(__dirname, "src", "core", "framework")
}
},
devServer: {
port: 5000,
open: true,
hot: isDev,
watchFiles: "./",
historyApiFallback: isDev
},
devtool: isDev && "source-map",
module: {
rules: [
{
test: /\.s(a|c)ss$/gi,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
},
{
test: /\.csv$/gi,
use: ["csv-loader"]
},
{
test: /\.m?js$/gi,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-proposal-class-properties"]
}
}
}
]
},
plugins: definePlugins(
env,
[
new HtmlWebpackPlugin({
template: "./app.html",
filename: "index.html",
minify: {
collapseWhitespace: isProd
}
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "src/assets/favicon/Carlosjj-Microsoft-Office-2013-Excel.ico"),
to: path.resolve(__dirname, "bundle")
}
]
}),
new MiniCssExtractPlugin({
filename: generateFilename("css", env)
}),
new webpack.ids.DeterministicChunkIdsPlugin({
maxLength: 7
}),
new webpack.ProgressPlugin(),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
})
],
[new EslintWebpackPlugin(), new BundleAnalyzerPlugin()]
)
});
module.exports = (env, argv) => {
return argv?.mode ? [webpackMainConfig(env, argv), multipleStylesheetsBundlerConfig(env, argv)] : webpackMainConfig(env, argv);
};