Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set default SWC minimizer passes to 2 to reduce bundle size #8853

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object {
isOverSizeLimit: false,
name: main.js,
related: Array [],
size: 207,
size: 206,
type: asset,
},
],
Expand All @@ -49,7 +49,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: 6e63c93280ecf7f7,
hash: dc21001e16b79ee1,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -152,10 +152,10 @@ Object {
assets: Array [
Object {
name: main.js,
size: 207,
size: 206,
},
],
assetsSize: 207,
assetsSize: 206,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand All @@ -173,7 +173,7 @@ Object {
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: fbda3282340b27ec,
hash: 71a1e7d194ca24f9,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -246,10 +246,10 @@ Object {
assets: Array [
Object {
name: main.js,
size: 207,
size: 206,
},
],
assetsSize: 207,
assetsSize: 206,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand Down Expand Up @@ -318,7 +318,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: b7792c75de914bcf,
hash: bf2cde18064956f4,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -685,7 +685,7 @@ Object {
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: f0ec7fcc2231784a,
hash: 8a4abcf81482ccb3,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -1432,7 +1432,7 @@ Object {
related: Object {},
},
name: main.js,
size: 207,
size: 206,
type: asset,
},
],
Expand All @@ -1449,7 +1449,7 @@ Object {
files: Array [
main.js,
],
hash: 6e63c93280ecf7f7,
hash: dc21001e16b79ee1,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -1569,7 +1569,7 @@ Object {
isOverSizeLimit: false,
name: main.js,
related: Array [],
size: 346,
size: 337,
type: asset,
},
],
Expand All @@ -1589,7 +1589,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: cb183b73192ba5e0,
hash: 7a2353722113f6b4,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -1916,10 +1916,10 @@ exports.c = require("./c?c=3");,
assets: Array [
Object {
name: main.js,
size: 346,
size: 337,
},
],
assetsSize: 346,
assetsSize: 337,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand All @@ -1937,7 +1937,7 @@ exports.c = require("./c?c=3");,
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: 19c21d4b968cc3c3,
hash: bbbee3eda42d5c59,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -2234,10 +2234,10 @@ exports.c = require("./c?c=3");,
assets: Array [
Object {
name: main.js,
size: 346,
size: 337,
},
],
assetsSize: 346,
assetsSize: 337,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ const path = require("path");
it("[minify-disable-minify]: should not minify code", () => {
const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8");

expect(content).toContain("let a = process.env.a;");
expect(content).toContain("console.log(a + process.env.b);");
expect(content).toContain("console.log(process.env.a + process.env.b);");
});
6 changes: 3 additions & 3 deletions packages/rspack-test-tools/tests/statsAPICases/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = {
expect(stats?.toJson(statsOptions)).toMatchSnapshot();
expect(stats?.toString(statsOptions)).toMatchInlineSnapshot(`
PublicPath: auto
asset main.js 207 bytes {909} [emitted] (name: main)
Entrypoint main 207 bytes = main.js
asset main.js 206 bytes {909} [emitted] (name: main)
Entrypoint main 206 bytes = main.js
chunk {909} (runtime: main) main.js (main) 55 bytes [entry] [rendered]
> ./fixtures/a main
./fixtures/a.js [585] 55 bytes {909} [depth 0] [built] [code generated]
Expand All @@ -39,7 +39,7 @@ module.exports = {
entry ./fixtures/a
cjs self exports reference self [585] ./fixtures/a.js

Rspack compiled successfully (fbda3282340b27ec)
Rspack compiled successfully (71a1e7d194ca24f9)
`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
related: Object {},
},
name: TestChild.js,
size: 353,
size: 344,
type: asset,
},
Object {
Expand All @@ -90,7 +90,7 @@ module.exports = {
related: Object {},
},
name: main.js,
size: 207,
size: 206,
type: asset,
},
],
Expand Down Expand Up @@ -120,7 +120,7 @@ module.exports = {
related: Object {},
},
name: TestChild.js,
size: 353,
size: 344,
type: asset,
},
],
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/tests/statsAPICases/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
chunkB.js,
],
filteredModules: undefined,
hash: cea3fd1b25a4eb4d,
hash: 64feb0c0aec6d474,
id: 250,
idHints: Array [],
initial: false,
Expand Down Expand Up @@ -144,7 +144,7 @@ module.exports = {
main.js,
],
filteredModules: undefined,
hash: cebfd5f70fd80ce4,
hash: ecb98ed10bf9b94d,
id: 909,
idHints: Array [],
initial: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/tests/statsAPICases/ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
};
expect(stats?.toJson(options)).toMatchSnapshot();
expect(stats?.toString(options)).toMatchInlineSnapshot(`
asset main.js 207 bytes {909} [emitted] (name: main)
asset main.js 206 bytes {909} [emitted] (name: main)
chunk {909} (runtime: main) main.js (main) 55 bytes [entry] [rendered]
./fixtures/a.js [585] 55 bytes {909} [built] [code generated]
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
async check(stats) {
expect(stats?.toString({ timings: false, version: false }))
.toMatchInlineSnapshot(`
asset main.js 353 bytes [emitted] (name: main)
asset main.js 344 bytes [emitted] (name: main)
./fixtures/abc.js 83 bytes [built] [code generated]
./fixtures/a.js 55 bytes [built] [code generated]
./fixtures/b.js 94 bytes [built] [code generated]
Expand Down
6 changes: 4 additions & 2 deletions packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,16 @@ export const SwcJsMinimizerRspackPlugin = create(
...options?.minimizerOptions?.format
};

// terser defaults to 1, SWC defaults to 3
// Rspack uses 2 to balance the build performance and the bundle size
if (compress && typeof compress === "object") {
compress = {
passes: 1, // terser and swc use different default value: 0
passes: 2,
...compress
};
} else if (compress) {
compress = {
passes: 1
passes: 2
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Rspack x.x.x compiled <CLR=32,BOLD>successfully</CLR> in X s"
`;

exports[`StatsTestCases should print correct stats for common-libs 1`] = `
"asset react.js 3.76 KiB [emitted] (name: react)
"asset react.js 2.08 KiB [emitted] (name: react)
./react.js 74 bytes [built] [code generated]
../../../../node_modules/.pnpm/[email protected]/node_modules/react/index.js 190 bytes [built] [code generated]
../../../../node_modules/.pnpm/[email protected]/node_modules/react/cjs/react.production.min.js 6.77 KiB [built] [code generated]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const defaultOptions = {
minify: true,
mangle: true,
compress: {
passes: 1,
passes: 2,
}
format: {
comments: false,
Expand All @@ -179,3 +179,15 @@ new rspack.SwcJsMinimizerRspackPlugin({
},
});
```

For example, set a higher `passes` to run more compression passes. In some cases this may result in a smaller bundle size, but the more passes that are run, the more time it takes to compress.

```js
new rspack.SwcJsMinimizerRspackPlugin({
minimizerOptions: {
compress: {
passes: 4,
},
},
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const defaultOptions = {
minify: true,
mangle: true,
compress: {
passes: 1,
passes: 2,
}
format: {
comments: false,
Expand All @@ -179,3 +179,15 @@ new rspack.SwcJsMinimizerRspackPlugin({
},
});
```

例如,设置更高的 `passes` 来运行更多的压缩次数。在一些情况下,这可能会输出更小的包体积,但执行次数越多,压缩需要的时间就越长。

```js
new rspack.SwcJsMinimizerRspackPlugin({
minimizerOptions: {
compress: {
passes: 4,
},
},
});
```
Loading