-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
149 lines (147 loc) · 3.63 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
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const Dotenv = require("dotenv-webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = (env, agrv) => {
const isDev = agrv.mode === "development";
const isAnalyze = env && env.analyze;
const basePlugins = [
new Dotenv(),
new HtmlWebpackPlugin({
template: "public/index.html",
}),
new CopyPlugin({
patterns: [
{
from: "**/*",
globOptions: {
ignore: ["index.html"],
},
to: "",
context: path.resolve("public"),
},
],
}),
new MiniCssExtractPlugin({
filename: isDev ? "[name].css" : "static/css/[name].[contenthash:6].css",
}),
new webpack.ProgressPlugin(),
new NodePolyfillPlugin(),
];
let prodPlugins = [
...basePlugins,
new CleanWebpackPlugin(),
new CompressionPlugin({
test: /\.(css|js|html|svg)$/,
}),
];
if (isAnalyze) {
prodPlugins = [...prodPlugins, new BundleAnalyzerPlugin()];
}
return {
entry: "./src/index.jsx",
module: {
rules: [
// Nhận vào một array. Đây là nơi chứa các loader
{
test: /\.(js|jsx)$/, // Nhận vào một Regex để xác định kiểu file
exclude: /(node_modules|bower_components)/, // Nhận vào một regex để loader loại trừ ra những file này
use: {
// Nhận vào một object hoặc một array chứa thông tin loader
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
},
},
},
{
test: /\.(ts|tsx)$/,
use: ["ts-loader", "eslint-loader"],
exclude: /node_modules/,
},
{
test: /\.(s[ac]ss|css)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { sourceMap: isDev ? true : false },
},
{
loader: "sass-loader",
options: { sourceMap: isDev ? true : false },
},
"postcss-loader",
],
},
{
test: /\.(eot|ttf|woff|woff2)$/,
use: [
{
loader: "file-loader",
options: {
name: isDev
? "[path][name].[ext]"
: "static/fonts/[name].[ext]",
},
},
],
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: "file-loader",
options: {
name: isDev
? "[path][name].[ext]"
: "static/media/[name].[contenthash:6].[ext]",
},
},
],
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js"],
alias: {
"@": path.resolve("src"),
"@@": path.resolve(),
},
},
output: {
path: path.resolve("build"),
publicPath: "/",
filename: "static/js/main.[contenthash:6].js",
environment: {
arrowFunction: false,
bigIntLiteral: false,
const: false,
destructuring: false,
dynamicImport: false,
forOf: false,
module: false,
},
},
devtool: isDev ? "source-map" : false,
devServer: {
static: "public",
port: 3000,
hot: true,
// watchContentBase: true,
// historyApiFallback: true,
open: true,
},
plugins: isDev ? basePlugins : prodPlugins,
performance: {
maxEntrypointSize: 800000, // Khi có 1 file build vượt quá giới hạn này (tính bằng byte) thì sẽ bị warning trên terminal.
},
};
};