Skip to content

Commit f2b5f23

Browse files
authored
feat: configure default minimizer for both Rspack & webpack (#1258)
1 parent 9c54343 commit f2b5f23

File tree

8 files changed

+86
-25
lines changed

8 files changed

+86
-25
lines changed

.changeset/chatty-seas-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Configure the default minimizer for Rspack and webpack configuration

.changeset/tall-towns-pay.md

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

packages/repack/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"pretty-format": "^26.6.2",
9797
"react-refresh": "^0.14.0",
9898
"schema-utils": "^4.2.0",
99+
"semver": "^7.7.2",
99100
"shallowequal": "^1.1.0",
100101
"tapable": "^2.2.1",
101102
"terser-webpack-plugin": "^5.3.14",
@@ -119,6 +120,7 @@
119120
"@types/mime-types": "^2.1.1",
120121
"@types/node": "catalog:",
121122
"@types/react-dom": "^17.0.7",
123+
"@types/semver": "^7.7.0",
122124
"@types/shallowequal": "^1.1.1",
123125
"babel-jest": "^29.7.0",
124126
"clang-format": "^1.8.0",

packages/repack/src/commands/common/config/__tests__/makeCompilerConfig.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { makeCompilerConfig } from '../makeCompilerConfig.js';
66

77
jest.mock('../getConfigFilePath.js');
88
jest.mock('../loadProjectConfig.js');
9+
jest.mock('../getMinimizerConfig.js');
910
jest.mock('../validatePlugins.js');
1011

1112
const setupMocks = () => {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import semver from 'semver';
2+
3+
// prefer `terser-webpack-plugin` installed in the project root to the one shipped with Re.Pack
4+
async function getTerserPlugin(rootDir: string) {
5+
let terserPluginPath: string;
6+
try {
7+
terserPluginPath = require.resolve('terser-webpack-plugin', {
8+
paths: [rootDir],
9+
});
10+
} catch {
11+
terserPluginPath = require.resolve('terser-webpack-plugin');
12+
}
13+
const plugin = await import(terserPluginPath);
14+
return 'default' in plugin ? plugin.default : plugin;
15+
}
16+
17+
async function getTerserConfig(rootDir: string) {
18+
const TerserPlugin = await getTerserPlugin(rootDir);
19+
return new TerserPlugin({
20+
test: /\.(js)?bundle(\?.*)?$/i,
21+
extractComments: false,
22+
terserOptions: {
23+
format: { comments: false },
24+
},
25+
});
26+
}
27+
28+
// use SwcJsMinimizerRspackPlugin for Rspack 1.5.0 and above
29+
function shouldUseTerserForRspack(rspackVersion: string): boolean {
30+
const version = semver.coerce(rspackVersion) ?? '0.0.0';
31+
return semver.lt(version, '1.5.0');
32+
}
33+
34+
async function getWebpackMinimizer(rootDir: string) {
35+
return [await getTerserConfig(rootDir)];
36+
}
37+
38+
async function getRspackMinimizer(rootDir: string) {
39+
const rspack = await import('@rspack/core');
40+
return [
41+
shouldUseTerserForRspack(rspack.rspackVersion)
42+
? await getTerserConfig(rootDir)
43+
: new rspack.SwcJsMinimizerRspackPlugin({
44+
test: /\.(js)?bundle(\?.*)?$/i,
45+
extractComments: false,
46+
minimizerOptions: {
47+
format: { comments: false },
48+
},
49+
}),
50+
];
51+
}
52+
53+
export async function getMinimizerConfig(
54+
bundler: 'rspack' | 'webpack',
55+
rootDir: string
56+
) {
57+
return bundler === 'rspack'
58+
? getRspackMinimizer(rootDir)
59+
: getWebpackMinimizer(rootDir);
60+
}
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import TerserPlugin from 'terser-webpack-plugin';
1+
import { getMinimizerConfig } from './getMinimizerConfig.js';
2+
3+
export async function getRepackConfig(
4+
bundler: 'rspack' | 'webpack',
5+
rootDir: string
6+
) {
7+
const minimizerConfiguration = await getMinimizerConfig(bundler, rootDir);
28

3-
export function getRepackConfig() {
49
return {
510
devtool: 'source-map',
611
output: {
@@ -13,20 +18,7 @@ export function getRepackConfig() {
1318
},
1419
optimization: {
1520
chunkIds: 'named',
16-
minimizer: [
17-
// TODO: remove this default in favour explicit configuration in webpack
18-
// and implicit configuration in rspack using SwcJsMinimizerRspackPlugin
19-
// once https://github.com/web-infra-dev/rspack/issues/11183 is resolved
20-
new TerserPlugin({
21-
test: /\.(js)?bundle(\?.*)?$/i,
22-
extractComments: false,
23-
terserOptions: {
24-
format: {
25-
comments: false,
26-
},
27-
},
28-
}),
29-
],
21+
minimizer: minimizerConfiguration,
3022
},
3123
};
3224
}

packages/repack/src/commands/common/config/makeCompilerConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function makeCompilerConfig<C extends ConfigurationObject>(
4343
const commandConfig = getCommandConfig(command, bundler);
4444

4545
// get defaults that will be applied on top of built-in ones (Rspack/webpack)
46-
const repackConfig = getRepackConfig();
46+
const repackConfig = await getRepackConfig(bundler, rootDir);
4747

4848
// load the project config
4949
const rawConfig = await loadProjectConfig<C>(configPath);

pnpm-lock.yaml

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)