build: improve tree-shaking (#7325)

* build: improve tree-shaking

* dedup terser
This commit is contained in:
Zach Pomerantz 2023-09-21 11:29:22 -07:00 committed by GitHub
parent d4f19e42f8
commit 5e1c430657
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 14 deletions

@ -5,7 +5,6 @@ const { execSync } = require('child_process')
const MiniCssExtractPlugin = require('mini-css-extract-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const path = require('path') const path = require('path')
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin') const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
const TerserPlugin = require('terser-webpack-plugin')
const { IgnorePlugin, ProvidePlugin } = require('webpack') const { IgnorePlugin, ProvidePlugin } = require('webpack')
const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin') const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin')
@ -146,18 +145,20 @@ module.exports = {
return rule return rule
}) })
// Run terser compression on node_modules before tree-shaking, so that tree-shaking is more effective.
// This works by eliminating dead code, so that webpack can identify unused imports and tree-shake them;
// it is only necessary for node_modules - it is done through linting for our own source code -
// see https://medium.com/engineering-housing/dead-code-elimination-and-tree-shaking-at-housing-part-1-307a94b30f23#7e03:
webpackConfig.module.rules.push({
enforce: 'post',
test: /node_modules.*\.(js)$/,
loader: path.join(__dirname, 'scripts/terser-loader.js'),
options: { compress: true, mangle: false },
})
// Configure webpack optimization: // Configure webpack optimization:
webpackConfig.optimization = Object.assign( webpackConfig.optimization = Object.assign(
webpackConfig.optimization, webpackConfig.optimization,
{
minimize: isProduction,
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
parallel: require('os').cpus().length,
}),
],
},
isProduction isProduction
? { ? {
splitChunks: { splitChunks: {

@ -152,6 +152,7 @@
"source-map-explorer": "^2.5.3", "source-map-explorer": "^2.5.3",
"start-server-and-test": "^2.0.0", "start-server-and-test": "^2.0.0",
"swc-loader": "^0.2.3", "swc-loader": "^0.2.3",
"terser": "^5.19.4",
"terser-webpack-plugin": "^5.3.9", "terser-webpack-plugin": "^5.3.9",
"ts-jest": "^29.1.1", "ts-jest": "^29.1.1",
"ts-transform-graphql-tag": "^0.2.1", "ts-transform-graphql-tag": "^0.2.1",

15
scripts/terser-loader.js Normal file

@ -0,0 +1,15 @@
/* eslint-env node */
const { minify } = require('terser')
module.exports = async function terserLoader(source, map, meta) {
const callback = this.async()
const options = this.getOptions()
try {
const data = await minify(source, options)
const { code } = data || {}
callback(null, code, map, meta)
} catch (e) {
callback(e)
}
}

@ -19494,10 +19494,10 @@ terser-webpack-plugin@^5.2.5, terser-webpack-plugin@^5.3.7, terser-webpack-plugi
serialize-javascript "^6.0.1" serialize-javascript "^6.0.1"
terser "^5.16.8" terser "^5.16.8"
terser@^5.0.0, terser@^5.10.0, terser@^5.16.8: terser@^5.0.0, terser@^5.10.0, terser@^5.16.8, terser@^5.19.4:
version "5.19.2" version "5.19.4"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.19.2.tgz#bdb8017a9a4a8de4663a7983f45c506534f9234e" resolved "https://registry.yarnpkg.com/terser/-/terser-5.19.4.tgz#941426fa482bf9b40a0308ab2b3cd0cf7c775ebd"
integrity sha512-qC5+dmecKJA4cpYxRa5aVkKehYsQKc+AHeKl0Oe62aYjBL8ZA33tTljktDHJSaxxMnbI5ZYw+o/S2DxxLu8OfA== integrity sha512-6p1DjHeuluwxDXcuT9VR8p64klWJKo1ILiy19s6C9+0Bh2+NWTX6nD9EPppiER4ICkHDVB1RkVpin/YW2nQn/g==
dependencies: dependencies:
"@jridgewell/source-map" "^0.3.3" "@jridgewell/source-map" "^0.3.3"
acorn "^8.8.2" acorn "^8.8.2"