-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
175 lines (159 loc) · 4.36 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
const path = require("path")
const HTMLWebpackPlugin = require("html-webpack-plugin")
const {CleanWebpackPlugin} = require("clean-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin")
const TerserWebpackPlugin = require("terser-webpack-plugin")
const {BundleAnalyzerPlugin} = require("webpack-bundle-analyzer")
const isDevMode = process.env.NODE_ENV === "development"
const isProdMode = !isDevMode
const filename = ext => isDevMode ? `[name].${ext}` : `[name].[contenthash].${ext}`
const cssLoaders = extra => {
const loaders = [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDevMode,
reloadAll: true
}
},
"css-loader",
"postcss-loader"
]
if (extra)
loaders.push(extra)
return loaders
}
const optimization = () => {
const config = {
splitChunks: {
chunks: "all"
}
}
if (isProdMode)
config.minimizer = [
new OptimizeCssAssetsWebpackPlugin(),
new TerserWebpackPlugin()
]
return config
}
const babelOptions = preset => {
const opt = {
presets: [
"@babel/preset-env"
]
}
if (preset)
opt.presets.push(preset)
return opt
}
const plugins = () => {
const base = [
new HTMLWebpackPlugin({
template: "./index.html",
minify: {
collapseWhitespace: isProdMode
}
}),
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "src/favicon.ico"),
to: path.resolve(__dirname, "dist")
}
]
}),
new MiniCssExtractPlugin({
filename: filename("css")
})
]
if (isProdMode)
base.push(new BundleAnalyzerPlugin())
return base
}
// noinspection WebpackConfigHighlighting
module.exports = {
context: path.resolve(__dirname, "src"),
mode: "development",
entry: {
main: [
"@babel/polyfill",
"./js/app.js"
]
},
output: {
filename: filename("js"),
path: path.resolve(__dirname, "dist")
},
resolve: {
extensions: [".js", ".json", ".xml"],
alias: {
"@": path.resolve(__dirname, "src/"),
"@css": path.resolve(__dirname, "src/css"),
"@assets": path.resolve(__dirname, "src/assets"),
"@img": path.resolve(__dirname, "src/assets/img"),
"@fonts": path.resolve(__dirname, "src/assets/fonts")
}
},
optimization: optimization(),
devServer: {
port: 4200,
hot: isDevMode
},
devtool: isDevMode ? "source-map" : "",
plugins: plugins(),
module: {
rules: [
{
test: /\.css$/,
use: cssLoaders()
},
{
test: /\.less$/,
use: cssLoaders("less-loader")
},
{
test: /\.s[ac]ss$/,
use: cssLoaders("sass-loader")
},
{
test: /\.(png|jpeg|jpg|gif|svg)$/,
use: ["file-loader"]
},
{
test: /\.(ttf|woff|woff2|eot)$/,
use: ["file-loader"]
},
{
test: /\.xml$/,
use: ["xml-loader"]
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: {
loader: "babel-loader",
options: babelOptions()
}
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: {
loader: "babel-loader",
options: babelOptions("@babel/preset-typescript")
}
},
{
test: /\.jsx$/,
exclude: /node_modules/,
loader: {
loader: "babel-loader",
options: babelOptions("@babel/preset-react")
}
}
]
}
}