-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
210 lines (195 loc) · 6.37 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const path = require("path"); //path is now a module that has access to pre defined methods that are built into Node.js
const HtmlWebpackPlugin = require("html-webpack-plugin"); //to use a plugin with webpack, you must use require
const CleanObsoleteChunks = require("webpack-clean-obsolete-chunks");
const CompressionPlugin = require("compression-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const WorkboxPlugin = require("workbox-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const isProduction = process.env.NODE_ENV === "production";
module.exports = {
mode: isProduction ? "production" : "development", //this will set the mode to production, which will minify the code and make it production ready
// Add source-map optimization
devtool: isProduction ? "hidden-source-map" : "eval-cheap-module-source-map",
name: "React Webpack",
entry: "./src/index.js",
output: {
path: path.join(__dirname, "/build"),
pathinfo: true,
filename: isProduction ? "[name].[contenthash].js" : "[name].[fullhash].js",
chunkFilename: isProduction
? "[name].[contenthash].js"
: "[name].[fullhash].js",
libraryTarget: "umd",
clean: true, // Clean the output directory before emit.
assetModuleFilename: "[name][ext]",
sourceMapFilename: "[name].js.map",
},
resolve: {
extensions: [".js", ".jsx", ".json"], // Automatically resolve certain extensions
alias: {
App: path.resolve(__dirname, "src/App.js"), // Alias for App.js
Components: path.resolve(__dirname, "src/components/"), // Alias for components directory
Common: path.resolve(__dirname, "src/Common/"), // Alias for common directory
},
modules: [path.resolve(__dirname, "src"), "node_modules"], // Look for modules in src and node_modules
symlinks: false, // Don't follow symlinks
cacheWithContext: false,
fallback: {
path: require.resolve("path-browserify"),
},
},
plugins: [
new CopyWebpackPlugin({
patterns: [{ from: "public", to: "" }],
}),
new HtmlWebpackPlugin({
//this plugin will help us generate the production html file in our /build
filename: "index.html", //our production html file will be named index.html
template: "./src/index.html", //this is a template for our production html file, we are defining how the html will look like before we make our production html file
favicon: "./src/Common/Icons/tic.png",
minify: {
collapseWhitespace: true,
collapseBooleanAttributes: true,
useShortDoctype: true,
removeComments: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
removeRedundantAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
inject: true,
hash: true,
title: "development",
description: "HtmlWebpackPlugin",
}),
new CleanObsoleteChunks({
verbose: true,
deep: true,
}),
new CleanWebpackPlugin({
root: process.cwd(),
verbose: true,
dry: false,
cleanOnceBeforeBuildPatterns: [
"**/*",
"!stats.json",
"!important.js",
"!folder/**/*",
],
}),
new MiniCssExtractPlugin({
filename: isProduction
? "[name].[contenthash].css"
: "[name].[fullhash].css",
chunkFilename: isProduction
? "[name].[contenthash].css"
: "[name].[fullhash].css",
}),
new CompressionPlugin({
filename: "[path][base].gz",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
new WorkboxPlugin.InjectManifest({
swSrc: "./src/serviceWorker.js",
swDest: "service-worker.js",
}),
],
devServer: {
port: 3001,
historyApiFallback: true,
compress: true,
hot: true,
host: "localhost",
server: "http",
allowedHosts: "auto",
client: {
progress: true,
reconnect: true,
},
},
module: {
rules: [
{
//loaders are transformations that are applied to files (typescript to javascript, sass to css)
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader", //for all .js files, we will load the babel transpiler
options: { presets: ["@babel/preset-env", "@babel/preset-react"] }, //preset-env is a group of babel plugins that will transpile all the new features of javascript
}, //preset-react is also a group of babel plugins, but it will transpile jsx with other new features of javascript
},
{
test: /\.css$/,
use: [{ loader: "style-loader" }, { loader: "css-loader" }], //using style loader and css loader to load css onto application
},
{
test: /\.(png|jpg|webp|mp4|wav|svg)$/,
type: "asset/resource", //asset/resource loads files such as images, audio and videos
use: [
{
loader: "image-webpack-loader",
options: {
mozjpeg: {
progressive: true,
quality: 65,
},
optipng: {
enabled: true,
},
pngquant: {
quality: [0.65, 0.9],
speed: 4,
},
gifsicle: {
interlaced: false,
},
webp: {
quality: 75,
},
},
},
],
},
],
},
optimization: {
minimize: true,
runtimeChunk: true,
splitChunks: false,
removeAvailableModules: true,
removeEmptyChunks: true,
mergeDuplicateChunks: true,
minimizer: [
new TerserPlugin({
parallel: true,
test: /\.js(\?.*)?$/i,
terserOptions: {
compress: false,
mangle: true,
output: { comments: false, ascii_only: true },
},
}),
new CssMinimizerPlugin(),
],
flagIncludedChunks: true,
usedExports: true,
sideEffects: true,
},
performance: false,
cache: {
type: "filesystem",
buildDependencies: {
config: [__filename],
},
},
};