Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/chatty-seas-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Configure the default minimizer for Rspack and webpack configuration
5 changes: 0 additions & 5 deletions .changeset/tall-towns-pay.md

This file was deleted.

2 changes: 2 additions & 0 deletions packages/repack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"pretty-format": "^26.6.2",
"react-refresh": "^0.14.0",
"schema-utils": "^4.2.0",
"semver": "^7.7.2",
"shallowequal": "^1.1.0",
"tapable": "^2.2.1",
"terser-webpack-plugin": "^5.3.14",
Expand All @@ -119,6 +120,7 @@
"@types/mime-types": "^2.1.1",
"@types/node": "catalog:",
"@types/react-dom": "^17.0.7",
"@types/semver": "^7.7.0",
"@types/shallowequal": "^1.1.1",
"babel-jest": "^29.7.0",
"clang-format": "^1.8.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { makeCompilerConfig } from '../makeCompilerConfig.js';

jest.mock('../getConfigFilePath.js');
jest.mock('../loadProjectConfig.js');
jest.mock('../getMinimizerConfig.js');
jest.mock('../validatePlugins.js');

const setupMocks = () => {
Expand Down
60 changes: 60 additions & 0 deletions packages/repack/src/commands/common/config/getMinimizerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import semver from 'semver';

// prefer `terser-webpack-plugin` installed in the project root to the one shipped with Re.Pack
async function getTerserPlugin(rootDir: string) {
let terserPluginPath: string;
try {
terserPluginPath = require.resolve('terser-webpack-plugin', {
paths: [rootDir],
});
} catch {
terserPluginPath = require.resolve('terser-webpack-plugin');
}
const plugin = await import(terserPluginPath);
return 'default' in plugin ? plugin.default : plugin;
}

async function getTerserConfig(rootDir: string) {
const TerserPlugin = await getTerserPlugin(rootDir);
return new TerserPlugin({
test: /\.(js)?bundle(\?.*)?$/i,
extractComments: false,
terserOptions: {
format: { comments: false },
},
});
}

// use SwcJsMinimizerRspackPlugin for Rspack 1.5.0 and above
function shouldUseTerserForRspack(rspackVersion: string): boolean {
const version = semver.coerce(rspackVersion) ?? '0.0.0';
return semver.lt(version, '1.5.0');
}

async function getWebpackMinimizer(rootDir: string) {
return [await getTerserConfig(rootDir)];
}

async function getRspackMinimizer(rootDir: string) {
const rspack = await import('@rspack/core');
return [
shouldUseTerserForRspack(rspack.rspackVersion)
? await getTerserConfig(rootDir)
: new rspack.SwcJsMinimizerRspackPlugin({
test: /\.(js)?bundle(\?.*)?$/i,
extractComments: false,
minimizerOptions: {
format: { comments: false },
},
}),
];
}

export async function getMinimizerConfig(
bundler: 'rspack' | 'webpack',
rootDir: string
) {
return bundler === 'rspack'
? getRspackMinimizer(rootDir)
: getWebpackMinimizer(rootDir);
}
24 changes: 8 additions & 16 deletions packages/repack/src/commands/common/config/getRepackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import TerserPlugin from 'terser-webpack-plugin';
import { getMinimizerConfig } from './getMinimizerConfig.js';

export async function getRepackConfig(
bundler: 'rspack' | 'webpack',
rootDir: string
) {
const minimizerConfiguration = await getMinimizerConfig(bundler, rootDir);

export function getRepackConfig() {
return {
devtool: 'source-map',
output: {
Expand All @@ -13,20 +18,7 @@ export function getRepackConfig() {
},
optimization: {
chunkIds: 'named',
minimizer: [
// TODO: remove this default in favour explicit configuration in webpack
// and implicit configuration in rspack using SwcJsMinimizerRspackPlugin
// once https://github.com/web-infra-dev/rspack/issues/11183 is resolved
new TerserPlugin({
test: /\.(js)?bundle(\?.*)?$/i,
extractComments: false,
terserOptions: {
format: {
comments: false,
},
},
}),
],
minimizer: minimizerConfiguration,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function makeCompilerConfig<C extends ConfigurationObject>(
const commandConfig = getCommandConfig(command, bundler);

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

// load the project config
const rawConfig = await loadProjectConfig<C>(configPath);
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.