Skip to content

Commit 4913b6f

Browse files
purfectliteratureadi-herwana-nus
authored andcommitted
perf(webpack): tweak/update plugins, build optimizations
- allow configuring client port - remove unused expose-loader - remove unused webpack-manifest-plugin - change favicon-webpack-plugin to fix development mode warnings - add swc plugin to optimize compiled js - add sharp, svgo plugins to optimize images, SVGs - cache production build data on filesystem, clean build folder before building
1 parent da35935 commit 4913b6f

File tree

6 files changed

+322
-44
lines changed

6 files changed

+322
-44
lines changed

client/app/lib/moment-timezone.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

client/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"@formatjs/cli": "^6.2.7",
118118
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
119119
"@svgr/webpack": "^8.1.0",
120+
"@swc/core": "^1.11.21",
120121
"@testing-library/jest-dom": "^6.4.2",
121122
"@testing-library/react": "^15.0.7",
122123
"@testing-library/user-event": "^14.5.2",
@@ -170,11 +171,11 @@
170171
"eslint-plugin-react-hooks": "^4.6.2",
171172
"eslint-plugin-simple-import-sort": "^12.1.0",
172173
"eslint-plugin-sonarjs": "^0.24.0",
173-
"expose-loader": "^5.0.0",
174174
"favicons": "^7.1.4",
175175
"favicons-webpack-plugin": "^6.0.1",
176176
"fork-ts-checker-webpack-plugin": "^9.0.2",
177177
"html-webpack-plugin": "^5.6.0",
178+
"image-minimizer-webpack-plugin": "^4.1.3",
178179
"jest": "^29.7.0",
179180
"jest-canvas-mock": "^2.5.2",
180181
"jest-environment-jsdom": "^29.7.0",
@@ -189,15 +190,16 @@
189190
"redux-logger": "^3.0.6",
190191
"sass": "^1.76.0",
191192
"sass-loader": "^14.1.0",
193+
"sharp": "^0.34.1",
192194
"style-loader": "^3.3.4",
195+
"svgo": "^3.3.2",
193196
"tailwindcss": "^3.4.1",
194197
"terser-webpack-plugin": "^5.3.10",
195198
"ts-jest": "^29.1.2",
196199
"typescript": "^5.4.3",
197200
"webpack": "^5.94.0",
198201
"webpack-cli": "^5.1.4",
199202
"webpack-dev-server": "^5.0.4",
200-
"webpack-manifest-plugin": "^5.0.0",
201203
"webpack-merge": "^5.10.0"
202204
},
203205
"resolutions": {

client/webpack.common.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ContextReplacementPlugin,
55
DefinePlugin,
66
} = require('webpack');
7-
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
87
const DotenvPlugin = require('dotenv-webpack');
98
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
109
const HtmlWebpackPlugin = require('html-webpack-plugin');
@@ -15,6 +14,9 @@ const cssIncludes = require('./css-includes.json');
1514

1615
const ENV_DIR = process.env.BABEL_ENV === 'e2e-test' ? './.env.test' : './.env';
1716

17+
/**
18+
* @type {import('webpack').Configuration}
19+
*/
1820
module.exports = {
1921
entry: './app/index.tsx',
2022
output: {
@@ -46,12 +48,12 @@ module.exports = {
4648
plugins: [
4749
new DotenvPlugin({ path: ENV_DIR }),
4850
new IgnorePlugin({ resourceRegExp: /__test__/ }),
49-
new WebpackManifestPlugin({
50-
publicPath: '/webpack/',
51-
writeToFileEmit: true,
52-
}),
5351
new HtmlWebpackPlugin({ template: './public/index.html' }),
54-
new FaviconsWebpackPlugin({ logo: './favicon.svg', inject: true }),
52+
new FaviconsWebpackPlugin({
53+
logo: './favicon.svg',
54+
inject: true,
55+
mode: 'auto',
56+
}),
5557
// Do not require all locales in moment
5658
new ContextReplacementPlugin(/moment\/locale$/, /^\.\/(en-.*|zh-.*)$/),
5759
new ForkTsCheckerWebpackPlugin({
@@ -118,13 +120,6 @@ module.exports = {
118120
],
119121
exclude: /node_modules/,
120122
},
121-
{
122-
test: require.resolve('./app/lib/moment-timezone'),
123-
loader: 'expose-loader',
124-
options: {
125-
exposes: 'moment',
126-
},
127-
},
128123
{
129124
test: /\.md$/,
130125
type: 'asset/source',

client/webpack.dev.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const common = require('./webpack.common');
55
const packageJSON = require('./package.json');
66

77
const SERVER_PORT = packageJSON.devServer.serverPort;
8+
const CLIENT_PORT = packageJSON.devServer.clientPort;
89
const APP_HOST = packageJSON.devServer.appHost;
910

1011
const BLUE_ANSI = '\x1b[36m%s\x1b[0m';
@@ -20,10 +21,14 @@ const bypassProxyIf = [
2021
(request) => request.url.startsWith('/oauth'),
2122
];
2223

24+
/**
25+
* @type {import('webpack').Configuration}
26+
*/
2327
module.exports = merge(common, {
2428
mode: 'development',
2529
devtool: 'eval-cheap-module-source-map',
2630
devServer: {
31+
port: CLIENT_PORT,
2732
allowedHosts: [`.${APP_HOST}`],
2833
historyApiFallback: true,
2934
devMiddleware: {

client/webpack.prod.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
const { merge } = require('webpack-merge');
22
const TerserPlugin = require('terser-webpack-plugin');
33
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
4+
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
45

56
const common = require('./webpack.common');
67

78
const AVAILABLE_CPUS = +process.env.AVAILABLE_CPUS;
89

10+
/**
11+
* @type {import('webpack').Configuration}
12+
*/
913
module.exports = merge(common, {
1014
mode: 'production',
1115
devtool: 'source-map',
1216
output: {
1317
filename: '[name]-[contenthash].js',
1418
publicPath: '/static/',
19+
clean: true,
20+
},
21+
cache: {
22+
type: 'filesystem',
1523
},
1624
optimization: {
1725
usedExports: true,
1826
minimizer: [
1927
new TerserPlugin({
2028
parallel: AVAILABLE_CPUS || true,
29+
minify: TerserPlugin.swcMinify,
30+
extractComments: false,
31+
terserOptions: {
32+
compress: true,
33+
mangle: true,
34+
format: { comments: false },
35+
},
2136
}),
2237
],
2338
},
@@ -30,5 +45,27 @@ module.exports = merge(common, {
3045
},
3146
],
3247
},
33-
plugins: [new MomentTimezoneDataPlugin({ startYear: 2014 })],
48+
plugins: [
49+
new MomentTimezoneDataPlugin({ startYear: 2014 }),
50+
new ImageMinimizerPlugin({
51+
test: /\.(jpe?g|png|gif)$/i,
52+
minimizer: {
53+
implementation: ImageMinimizerPlugin.sharpMinify,
54+
options: {
55+
encodeOptions: {
56+
png: {
57+
quality: 90,
58+
compressionLevel: 9,
59+
},
60+
},
61+
},
62+
},
63+
}),
64+
new ImageMinimizerPlugin({
65+
test: /\.svg$/i,
66+
minimizer: {
67+
implementation: ImageMinimizerPlugin.svgoMinify,
68+
},
69+
}),
70+
],
3471
});

0 commit comments

Comments
 (0)