Skip to content

perf: enable compress minify option for node target #424

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

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 22 additions & 11 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path, { dirname, extname, isAbsolute, join } from 'node:path';
import {
type EnvironmentConfig,
type RsbuildConfig,
type RsbuildTarget,
defineConfig as defineRsbuildConfig,
loadConfig as loadRsbuildConfig,
mergeRsbuildConfig,
Expand Down Expand Up @@ -285,14 +286,18 @@ export const composeAutoExternalConfig = (options: {
: {};
};

export function composeMinifyConfig(config: LibConfig): RsbuildConfig {
export function composeMinifyConfig(
config: LibConfig,
target?: RsbuildTarget,
): RsbuildConfig {
const minify = config.output?.minify;
const format = config.format;
if (minify !== undefined) {
// User's minify configuration will be merged afterwards.
return {};
}

const format = config.format;

// When minify is not specified, Rslib will use a sane default for minify options.
// The default options will only perform dead code elimination and unused code elimination.
return {
Expand All @@ -303,15 +308,21 @@ export function composeMinifyConfig(config: LibConfig): RsbuildConfig {
jsOptions: {
minimizerOptions: {
mangle: false,
// MF assets are loaded over the network, which means they will not be compressed by the project. Therefore, minifying them is necessary.
// MF assets are loaded over the network, which means they will not be compressed by the project.
// Therefore, minifying them is necessary.
minify: format === 'mf',
compress: {
defaults: false,
unused: true,
dead_code: true,
// remoteEntry's global variable will be tree-shaken if `toplevel` is enabled in "mf" format
toplevel: format !== 'mf',
},
compress:
// For the Node target, keep output bundles as small as possible while retaining the necessary information
// for debugging, as Node outputs usually executed directly at runtime rather than being built again.
target === 'node'
? true
: {
defaults: false,
unused: true,
dead_code: true,
// remoteEntry's global variable will be tree-shaken if `toplevel` is enabled in "mf" format
toplevel: format !== 'mf',
},
format: {
comments: 'all',
},
Expand Down Expand Up @@ -1106,7 +1117,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
autoExternalConfig?.output?.externals,
externalsConfig?.output?.externals,
);
const minifyConfig = composeMinifyConfig(config);
const minifyConfig = composeMinifyConfig(config, target);
const bannerFooterConfig = composeBannerFooterConfig(banner, footer);
const decoratorsConfig = composeDecoratorsConfig(
compilerOptions,
Expand Down
21 changes: 3 additions & 18 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"js": true,
"jsOptions": {
"minimizerOptions": {
"compress": {
"dead_code": true,
"defaults": false,
"toplevel": true,
"unused": true,
},
"compress": true,
"format": {
"comments": "all",
},
Expand Down Expand Up @@ -292,12 +287,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"js": true,
"jsOptions": {
"minimizerOptions": {
"compress": {
"dead_code": true,
"defaults": false,
"toplevel": true,
"unused": true,
},
"compress": true,
"format": {
"comments": "all",
},
Expand Down Expand Up @@ -502,12 +492,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"js": true,
"jsOptions": {
"minimizerOptions": {
"compress": {
"dead_code": true,
"defaults": false,
"toplevel": true,
"unused": true,
},
"compress": true,
"format": {
"comments": "all",
},
Expand Down
7 changes: 1 addition & 6 deletions packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,7 @@ describe('minify', () => {
"js": true,
"jsOptions": {
"minimizerOptions": {
"compress": {
"dead_code": true,
"defaults": false,
"toplevel": true,
"unused": true,
},
"compress": true,
"format": {
"comments": "all",
},
Expand Down
11 changes: 5 additions & 6 deletions tests/integration/alias/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`source.alias 1`] = `
"const a = 'hello world';
console.info(a);
"console.info('hello world');
"
`;

exports[`source.alias 2`] = `
""use strict";
var __webpack_exports__ = {};
const a = 'hello world';
console.info(a);
let a = 'hello world';
console.info('hello world');
var __webpack_export_target__ = exports;
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
value: true
__webpack_exports__.__esModule && Object.defineProperty(__webpack_export_target__, '__esModule', {
value: !0
});
"
`;
4 changes: 2 additions & 2 deletions tests/integration/auto-extension/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ describe('should respect output.filename.js to override builtin logic', () => {
const { entryFiles } = await buildAndGetResults({ fixturePath });
expect(extname(entryFiles.esm!)).toEqual('.mjs');
expect(entryFiles.cjs).toMatchInlineSnapshot(
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs/index.d08e1bb3.js"`,
`"<ROOT>/tests/integration/auto-extension/type-commonjs/config-override/dist/cjs/index.60cfaf97.js"`,
);
});

test('type is module', async () => {
const fixturePath = join(__dirname, 'type-module', 'config-override');
const { entryFiles } = await buildAndGetResults({ fixturePath });
expect(entryFiles.esm).toMatchInlineSnapshot(
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm/index.d2068839.js"`,
`"<ROOT>/tests/integration/auto-extension/type-module/config-override/dist/esm/index.387ba858.js"`,
);
expect(extname(entryFiles.cjs!)).toEqual('.cjs');
});
Expand Down
Loading
Loading