-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
159 lines (147 loc) · 3.91 KB
/
webpack.config.ts
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
/** @format */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const TerserPlugin = require('terser-webpack-plugin');
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const isProduction = process.env.NODE_ENV === "production";
const config = {
mode: "development",
entry: {
main: "./src/index.tsx",
home: "./src/pages/HomePage.tsx",
login: "./src/pages/LoginPage.tsx",
registration: "./src/pages/RegistrationPage.tsx",
product: "./src/pages/DisplayPage.tsx",
cart: "./src/pages/CartPage.tsx",
order: "./src/pages/OrderPage.tsx",
confirmation: "./src/pages/ConfirmationPage.tsx"
}, // Entry point of your application
output: {
path: path.resolve(__dirname, "build"), // Output directory
filename: 'bundle.[contenthash].js', // Output filename
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
},
devtool: "source-map",
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 3000,
hot: true,
open: true
},
module: {
rules: [
{
test: /\.(js|jsx|tsx|ts)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
}
] as any[],
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + '/public/index.html',
filename: 'index.html',
inject: 'body'
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'public', to: '',
globOptions: {
ignore: ['**.html'],
},
},
],
}),
new MiniCssExtractPlugin()
],
optimization: {
usedExports: true,
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false,
},
compress: {
dead_code: true,
drop_console: true,
drop_debugger: true,
warnings: true
}
},
extractComments: false,
}),
],
splitChunks: {
chunks: "all",
minSize: 244 * 1024,
},
runtimeChunk: 'single',
},
};
if (isProduction) {
// JSON loading
config.module.rules.push({
test: /\.json$/,
use: 'file-loader',
})
// Image minification
config.module.rules.push({
test: /\.(jpe?g|png|gif|svg)$/i,
type: "asset/resource",
use: [
'file-loader', // or 'url-loader'
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
quality: 80,
progressive: true,
},
pngquant: {
quality: '65-90',
speed: 4,
}
},
},
],
})
config.optimization.minimizer.push(new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
plugins: [
["jpegtran", { progressive: true }],
["optipng", { optimizationLevel: 5 }],
]
},
},
}))
// Compression
config.plugins.push(new CompressionPlugin({
algorithm: 'gzip', // Use gzip compression
filename: '[path][base].gz', // Output filename pattern
test: /\.(js|css|html)$/, // Apply compression to JavaScript, CSS, and HTML files
threshold: 10240, // Only compress files larger than 10KB
minRatio: 0.8, // Only compress files with a compression ratio of at least 0.8 (80%)
}))
// Debugging
config.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = config;