-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.cjs
134 lines (128 loc) · 4.79 KB
/
webpack.config.cjs
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
const Webpack = require('webpack');
const TerserPlugin = require("terser-webpack-plugin");
const HTMLInlinePlugin = require("html-inline-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CSSMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CSSManglePlugin = require("css-mangle-webpack-plugin");
// Whether it is in debug build mode.
const isDebug = process.env.BUILD_TYPE.includes("dev");
/** @type {import("webpack").Configuration} */
module.exports = {
mode: isDebug ? "development" : "production",
entry: "./src/index.tsx",
output: { publicPath: "/" },
devServer: {
port: 9000,
proxy: [
{ // About proxy settings for sub domain api.
context: ['/api'],
target: 'http://localhost:8080',
changeOrigin: true
}
],
historyApiFallback: true // SPA
},
module: {
rules: [
{ // Converts a TSX files to JS files and it load all.
test: /\.tsx?$/,
use: "ts-loader",
exclude: "/node_modules/"
},
{ // Includes CSS files within the TSX in the bundler.
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
},
{ // Converts a SVG files to preact components all.
test: /\.svg$/,
use: ["preact-svg-loader"],
},
{ // To export the font assets files from `src/` to `dist/`.
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: "asset/resource",
generator: {
filename: "fonts/[name][ext]"
}
},
// To export the image assets files from `src/` to `dist/`.
// And by default, extensions are typical extensions supported by Chrome.
{
test: /\.(png|jpe?g|webp|avif|hei[cf]|gif|tiff)$/i,
use: [{
loader: "image-encode-loader",
options: {
// This format has the best compression rate at the moment.
format: "avif",
generator: {
filename: "images/[name].[ext]"
}
}
}],
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".css"],
// Resolves compatibility issues that arise during the building of React packages.
alias: {
"react": "preact/compat",
"react-dom": "preact/compat",
"react/jsx-runtime": "preact/jsx-runtime"
},
},
plugins: [
new MiniCssExtractPlugin(),
new CSSMinimizerPlugin({
parallel: true, // Is parallel processing.
minify: [
CSSMinimizerPlugin.cssoMinify,
CSSMinimizerPlugin.cssnanoMinify,
]
}),
new CSSManglePlugin({mangle: !isDebug}),
new HTMLInlinePlugin({
template: "./src/index.html",
filename: "./index.html",
favicon: "./src/assets/favicon.svg",
// When in debug mode, static resources such as CSS or JS are
// not merged into the document in inline form.
//
// Instead, they are requested asynchronously by the client to track changes.
inline: isDebug == false,
pretty: false,
}),
new Webpack.DefinePlugin({"process.env.IS_DEBUG": isDebug})
],
// Ignores about assets resource size all. (i.g. font)
performance: { hints: false },
optimization: {
minimize: !isDebug,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
compress: {
// Remove a unnecessary function arguments.
keep_fargs: true,
// In most cases, it is recommended to remove debug-related code because
// the user does not need to debug, but remove it in special cases.
drop_console: true,
drop_debugger: true,
dead_code: true,
unused: true,
},
mangle: {
properties: false,
toplevel: true,
eval: true
},
output: {
// In most cases, No comments providing is required to user.
// So, rather, it is recommended to remove the comments.
comments: false,
}
}
})
]
}
}