Skip to content

Commit 7a46505

Browse files
committed
fix(minify): only preserve some comments and annotations by default
1 parent 7bf3757 commit 7a46505

File tree

18 files changed

+122
-55
lines changed

18 files changed

+122
-55
lines changed

packages/core/src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export function composeMinifyConfig(config: LibConfig): RsbuildConfig {
325325
toplevel: format !== 'mf',
326326
},
327327
format: {
328-
comments: 'all',
328+
comments: 'some',
329+
preserve_annotations: true,
329330
},
330331
},
331332
},

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
9090
"unused": true,
9191
},
9292
"format": {
93-
"comments": "all",
93+
"comments": "some",
94+
"preserve_annotations": true,
9495
},
9596
"mangle": false,
9697
"minify": false,
@@ -327,7 +328,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
327328
"unused": true,
328329
},
329330
"format": {
330-
"comments": "all",
331+
"comments": "some",
332+
"preserve_annotations": true,
331333
},
332334
"mangle": false,
333335
"minify": false,
@@ -557,7 +559,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
557559
"unused": true,
558560
},
559561
"format": {
560-
"comments": "all",
562+
"comments": "some",
563+
"preserve_annotations": true,
561564
},
562565
"mangle": false,
563566
"minify": false,

packages/core/tests/config.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ describe('minify', () => {
331331
"unused": true,
332332
},
333333
"format": {
334-
"comments": "all",
334+
"comments": "some",
335+
"preserve_annotations": true,
335336
},
336337
"mangle": false,
337338
"minify": false,

pnpm-lock.yaml

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

tests/integration/asset/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
2424
]
2525
})
2626
});
27-
/* ESM default export */ const logoreact = SvgLogo;
27+
const logoreact = SvgLogo;
2828
console.log(logoreact);
2929
"
3030
`;
@@ -53,7 +53,7 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
5353
]
5454
})
5555
});
56-
/* ESM default export */ const logoreact = SvgLogo;
56+
const logoreact = SvgLogo;
5757
console.log(logoreact);
5858
"
5959
`;

tests/integration/bundle-false/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
2424
]
2525
})
2626
});
27-
/* ESM default export */ const logoreact = SvgLogo;
28-
/* ESM default export */ const src_rslib_entry_ = logoreact;
27+
const logoreact = SvgLogo;
28+
const src_rslib_entry_ = logoreact;
2929
export { src_rslib_entry_ as default };
3030
"
3131
`;
3232

3333
exports[`svgr in bundleless 2`] = `
3434
""use strict";
35-
// The require scope
3635
var __webpack_require__ = {};
37-
/************************************************************************/ // webpack/runtime/define_property_getters
3836
(()=>{
3937
__webpack_require__.d = function(exports1, definition) {
4038
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
@@ -43,15 +41,12 @@ var __webpack_require__ = {};
4341
});
4442
};
4543
})();
46-
// webpack/runtime/has_own_property
4744
(()=>{
4845
__webpack_require__.o = function(obj, prop) {
4946
return Object.prototype.hasOwnProperty.call(obj, prop);
5047
};
5148
})();
52-
// webpack/runtime/make_namespace_object
5349
(()=>{
54-
// define __esModule on exports
5550
__webpack_require__.r = function(exports1) {
5651
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
5752
value: 'Module'
@@ -61,12 +56,10 @@ var __webpack_require__ = {};
6156
});
6257
};
6358
})();
64-
/************************************************************************/ var __webpack_exports__ = {};
65-
// ESM COMPAT FLAG
59+
var __webpack_exports__ = {};
6660
__webpack_require__.r(__webpack_exports__);
67-
// EXPORTS
6861
__webpack_require__.d(__webpack_exports__, {
69-
default: ()=>/* binding */ src_rslib_entry_
62+
default: ()=>src_rslib_entry_
7063
});
7164
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
7265
require("react");
@@ -91,8 +84,8 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg
9184
]
9285
})
9386
});
94-
/* ESM default export */ const logoreact = SvgLogo;
95-
/* ESM default export */ const src_rslib_entry_ = logoreact;
87+
const logoreact = SvgLogo;
88+
const src_rslib_entry_ = logoreact;
9689
var __webpack_export_target__ = exports;
9790
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
9891
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {

tests/integration/extension-alias/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ exports[`resolve.extensionAlias should work 1`] = `
55
var __webpack_exports__ = {};
66
const bar = 'bar';
77
const foo = 'foo';
8-
// Relative import paths need explicit file extensions in ECMAScript imports
9-
// when '--moduleResolution' is 'node16' or 'nodenext'.
108
console.log(foo + bar);
119
var __webpack_export_target__ = exports;
1210
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
@@ -19,8 +17,6 @@ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_targe
1917
exports[`resolve.extensionAlias should work 2`] = `
2018
"const bar = 'bar';
2119
const foo = 'foo';
22-
// Relative import paths need explicit file extensions in ECMAScript imports
23-
// when '--moduleResolution' is 'node16' or 'nodenext'.
2420
console.log(foo + bar);
2521
"
2622
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/*! Legal Comment */
2+
import { jsx } from 'react/jsx-runtime';
3+
14
export const foo = () => {};
25

36
const bar = () => {};
47
const baz = () => {
58
return bar();
69
};
10+
11+
// normal comment
12+
export const Button = () => /*#__PURE__*/ jsx('button', {});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "minify-diabled-config-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}

tests/integration/minify/config/rslib.config.ts renamed to tests/integration/minify/config/disabled/rslib.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ export default defineConfig({
66
lib: [generateBundleEsmConfig()],
77
output: {
88
minify: false,
9+
externals: ['react/jsx-runtime'],
910
},
1011
source: {
1112
entry: {
12-
index: path.resolve(__dirname, '../__fixtures__/src/index.ts'),
13+
index: path.resolve(__dirname, '../../__fixtures__/src/index.ts'),
1314
},
1415
},
1516
});

tests/integration/minify/config/package.json renamed to tests/integration/minify/config/enabled/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "minify-config-test",
2+
"name": "minify-true-config-test",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleEsmConfig } from 'test-helper';
4+
5+
export default defineConfig({
6+
lib: [generateBundleEsmConfig()],
7+
output: {
8+
minify: {
9+
js: true,
10+
jsOptions: {
11+
minimizerOptions: {
12+
format: {
13+
comments: 'some',
14+
preserve_annotations: true,
15+
},
16+
},
17+
},
18+
},
19+
externals: ['react/jsx-runtime'],
20+
},
21+
source: {
22+
entry: {
23+
index: path.resolve(__dirname, '../../__fixtures__/src/index.ts'),
24+
},
25+
},
26+
});

tests/integration/minify/default/rslib.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ export default defineConfig({
99
index: path.resolve(__dirname, '../__fixtures__/src/index.ts'),
1010
},
1111
},
12+
output: {
13+
externals: ['react/jsx-runtime'],
14+
},
1215
});

tests/integration/minify/index.test.ts

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,88 @@ import { join } from 'node:path';
22
import { buildAndGetResults } from 'test-helper';
33
import { expect, test } from 'vitest';
44

5-
test('tree shaking is enabled by default, bar and baz should be shaken', async () => {
5+
test('tree shaking is enabled by default, bar and baz should be shaken, some comments and annotations are preserved', async () => {
66
const fixturePath = join(__dirname, 'default');
77
const { entries } = await buildAndGetResults({ fixturePath });
88
expect(entries.esm).toMatchInlineSnapshot(`
9-
"const foo = ()=>{};
10-
export { foo };
9+
"/*! For license information please see index.js.LICENSE.txt */
10+
import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
11+
/*! Legal Comment */ const foo = ()=>{};
12+
const Button = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)('button', {});
13+
export { Button, foo };
1114
"
1215
`);
1316
});
1417

15-
test('tree shaking is disabled by the user, bar and baz should be kept', async () => {
16-
const fixturePath = join(__dirname, 'config');
18+
test('minify is disabled, nothing will be stripped', async () => {
19+
const fixturePath = join(__dirname, 'config/disabled');
1720
const { entries } = await buildAndGetResults({ fixturePath });
1821
expect(entries.esm).toMatchInlineSnapshot(`
19-
"
20-
;// CONCATENATED MODULE: ../__fixtures__/src/index.ts?__rslib_entry__
22+
"import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
23+
24+
;// CONCATENATED MODULE: external "react/jsx-runtime"
25+
26+
;// CONCATENATED MODULE: ../../__fixtures__/src/index.ts?__rslib_entry__
27+
/*! Legal Comment */
2128
const foo = ()=>{};
2229
const bar = ()=>{};
2330
const baz = ()=>{
2431
return bar();
2532
};
33+
// normal comment
34+
const Button = ()=>/*#__PURE__*/ (0,__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)('button', {});
2635
27-
export { foo };
36+
export { Button, foo };
2837
"
2938
`);
3039
});
3140

32-
test('minify is enabled by default in mf format, bar and baz should be minified', async () => {
41+
test('minify is enabled, only preserve some comments and annotations', async () => {
42+
const fixturePath = join(__dirname, 'config/enabled');
43+
const { entries } = await buildAndGetResults({ fixturePath });
44+
expect(entries.esm).toMatchInlineSnapshot(`
45+
"/*! For license information please see index.js.LICENSE.txt */
46+
import*as t from"react/jsx-runtime";/*! Legal Comment */let o=()=>{},r=()=>/*#__PURE__*/(0,t.jsx)("button",{});export{r as Button,o as foo};"
47+
`);
48+
});
49+
50+
test('minify is enabled by default in mf format, bar and baz should be shaken, some comments and annotations are preserved', async () => {
3351
const fixturePath = join(__dirname, 'mf/default');
3452
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
3553
// biome-ignore format: snapshot
36-
expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{163:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{foo:function(){return foo}});const foo=()=>{}}}]);"`);
54+
expect(mfExposeEntry).toMatchInlineSnapshot(`
55+
"/*! For license information please see __federation_expose_default_export.js.LICENSE.txt */
56+
"use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{163:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{Button:function(){return Button},foo:function(){return foo}});var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(37);/*! Legal Comment */const foo=()=>{};const Button=()=>/*#__PURE__*/(0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx)("button",{})}}]);"
57+
`);
3758
});
3859

39-
test('minify is disabled by the user, bar and baz should not be minified', async () => {
60+
test('minify is disabled by the user, nothing not be stripped', async () => {
4061
const fixturePath = join(__dirname, 'mf/config');
4162
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
4263

43-
expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";
44-
(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], {
45-
"163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
46-
__webpack_require__.r(__webpack_exports__);
47-
__webpack_require__.d(__webpack_exports__, {
48-
foo: function() { return foo; }
49-
});
50-
const foo = ()=>{};
51-
const bar = ()=>{};
52-
const baz = ()=>{
53-
return bar();
54-
};
64+
expect(mfExposeEntry).toMatchInlineSnapshot(`
65+
""use strict";
66+
(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], {
67+
"163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
68+
__webpack_require__.r(__webpack_exports__);
69+
__webpack_require__.d(__webpack_exports__, {
70+
Button: function() { return Button; },
71+
foo: function() { return foo; }
72+
});
73+
/* ESM import */var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(37);
74+
/* ESM import */var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
75+
/*! Legal Comment */
76+
const foo = ()=>{};
77+
const bar = ()=>{};
78+
const baz = ()=>{
79+
return bar();
80+
};
81+
// normal comment
82+
const Button = ()=>/*#__PURE__*/ (0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx)('button', {});
5583
5684
57-
}),
85+
}),
5886
59-
}]);"`);
87+
}]);"
88+
`);
6089
});

tests/integration/minify/mf/config/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default defineConfig({
3030
output: {
3131
target: 'web',
3232
minify: false,
33+
externals: ['react/jsx-runtime'],
3334
},
3435
source: {
3536
entry: {

tests/integration/minify/mf/default/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ export default defineConfig({
3434
},
3535
output: {
3636
target: 'web',
37+
externals: ['react/jsx-runtime'],
3738
},
3839
});

tests/integration/resolve/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test('resolve data url', async () => {
88

99
expect(isSuccess).toBeTruthy();
1010
expect(entries.esm).toMatchInlineSnapshot(`
11-
"/* ESM default export */ const javascript_export_default_42 = 42;
11+
"const javascript_export_default_42 = 42;
1212
console.log('x:', javascript_export_default_42);
1313
"
1414
`);

tests/integration/shims/index.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ describe('CJS shims', () => {
111111
const importMetaUrl = import.meta.url;
112112
const src_rslib_entry_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
113113
const requiredModule = src_rslib_entry_require('./ok.cjs');
114-
// https://github.com/web-infra-dev/rslib/issues/425
115114
const src_rslib_entry_filename = (0, __WEBPACK_EXTERNAL_MODULE_url__.fileURLToPath)(import.meta.url);
116115
console.log(src_rslib_entry_filename);
117-
// https://github.com/web-infra-dev/rslib/pull/399
118116
const src_rslib_entry_module = null;
119117
export { src_rslib_entry_filename as __filename, importMetaUrl, src_rslib_entry_module as module, requiredModule };
120118
"

0 commit comments

Comments
 (0)