Skip to content

Commit d213490

Browse files
Merge branch 'main' into feat/mf-layers
2 parents eb9b7b1 + 788fb8e commit d213490

File tree

12 files changed

+69
-54
lines changed

12 files changed

+69
-54
lines changed

crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_export_require_dependency.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_hash::FxHashSet;
1717
use swc_core::atoms::Atom;
1818

1919
use super::ExportsBase;
20+
use crate::dependency::commonjs::PROTOTYPE_PROPS;
2021

2122
#[cacheable]
2223
#[allow(unused)]
@@ -229,7 +230,7 @@ impl Dependency for CommonJsExportRequireDependency {
229230
exports: ExportsOfExportsSpec::Names(vec![ExportNameOrSpec::ExportSpec(ExportSpec {
230231
name: name.to_owned(),
231232
from: Some(from.to_owned()),
232-
can_mangle: Some(false),
233+
can_mangle: Some(!PROTOTYPE_PROPS.contains(&name.as_str())),
233234
export: Some(if ids.is_empty() {
234235
Nullable::Null
235236
} else {
@@ -289,7 +290,7 @@ impl Dependency for CommonJsExportRequireDependency {
289290
Some(ExportsSpec {
290291
exports: ExportsOfExportsSpec::Names(vec![ExportNameOrSpec::ExportSpec(ExportSpec {
291292
name: name.to_owned(),
292-
can_mangle: Some(false),
293+
can_mangle: Some(!PROTOTYPE_PROPS.contains(&name.as_str())),
293294
..Default::default()
294295
})]),
295296
dependencies: None,

crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ impl ExportsBase {
5151
}
5252
}
5353

54+
// we can't mangle names that are in an empty object because one could access the prototype property
55+
// when export isn't set yet. It's different for different targets. so here we only list common properties.
56+
pub static PROTOTYPE_PROPS: [&str; 12] = [
57+
"constructor",
58+
"__defineGetter__",
59+
"__defineSetter__",
60+
"hasOwnProperty",
61+
"__lookupGetter__",
62+
"__lookupSetter__",
63+
"isPrototypeOf",
64+
"propertyIsEnumerable",
65+
"toString",
66+
"valueOf",
67+
"__proto__",
68+
"toLocaleString",
69+
];
70+
5471
#[cacheable]
5572
#[derive(Debug, Clone)]
5673
pub struct CommonJsExportsDependency {
@@ -103,9 +120,10 @@ impl Dependency for CommonJsExportsDependency {
103120
_mg_cache: &ModuleGraphCacheArtifact,
104121
_exports_info_artifact: &ExportsInfoArtifact,
105122
) -> Option<ExportsSpec> {
123+
let name = self.names[0].clone();
106124
let vec = vec![ExportNameOrSpec::ExportSpec(ExportSpec {
107-
name: self.names[0].clone(),
108-
can_mangle: Some(false), // in webpack, object own property may not be mangled
125+
can_mangle: Some(!PROTOTYPE_PROPS.contains(&name.as_str())),
126+
name,
109127
..Default::default()
110128
})];
111129
Some(ExportsSpec {

crates/rspack_plugin_javascript/src/dependency/commonjs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod require_resolve_header_dependency;
1414
pub use common_js_export_require_dependency::{
1515
CommonJsExportRequireDependency, CommonJsExportRequireDependencyTemplate,
1616
};
17+
use common_js_exports_dependency::PROTOTYPE_PROPS;
1718
pub use common_js_exports_dependency::{
1819
CommonJsExportsDependency, CommonJsExportsDependencyTemplate, ExportsBase,
1920
};

packages/rspack/hot/emitter.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,4 @@ EventEmitter.prototype.emit = function (eventName) {
2020

2121
var emitter = new EventEmitter();
2222

23-
// TODO: remove default export when rspack-dev-server refactored
24-
export default emitter;
2523
export { emitter };

packages/rspack/hot/log.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,4 @@ log.groupEnd = groupEnd;
8484
log.setLogLevel = setLogLevel;
8585
log.formatError = formatError;
8686

87-
// TODO: remove default export when rspack-dev-server refactored
88-
export default log;
8987
export { log };
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { moduleId, setToString, toString, abc, a, $1, __1 } from "./module";
2-
// TODO: support CJS mangle exports
3-
// const moduleId2 = require("./commonjs").moduleId;
4-
// const toString2 = require("./commonjs").toString;
5-
// const setToString2 = require("./commonjs").setToString;
6-
// const abc2 = require("./commonjs").abc;
7-
// const a2 = require("./commonjs").a;
8-
// const equals2 = require("./commonjs")["="];
9-
// const $12 = require("./commonjs").$1;
10-
// const __12 = require("./commonjs").__1;
2+
const moduleId2 = require("./commonjs").moduleId;
3+
const toString2 = require("./commonjs").toString;
4+
const setToString2 = require("./commonjs").setToString;
5+
const abc2 = require("./commonjs").abc;
6+
const a2 = require("./commonjs").a;
7+
const equals2 = require("./commonjs")["="];
8+
const $12 = require("./commonjs").$1;
9+
const __12 = require("./commonjs").__1;
1110

1211
it("should mangle names and remove exports even with toString named export (ESM)", () => {
1312
expect(abc).toBe("abc");
@@ -28,23 +27,23 @@ it("should mangle names and remove exports even with toString named export (ESM)
2827
);
2928
});
3029

31-
// it("should mangle names and remove exports even with toString named export (CJS)", () => {
32-
// expect(abc2).toBe("abc");
33-
// expect(toString2).toBe(Object.prototype.toString);
34-
// setToString2();
35-
// const toString3 = require("./commonjs").toString;
36-
// expect(toString3()).toBe("toString");
37-
// expect(a2).toBe("single char");
38-
// expect(equals2).toBe("single char non-identifier");
39-
// expect($12).toBe("double char");
40-
// expect(__12).toBe("3 chars");
41-
// expect(
42-
// Object.keys(require.cache[moduleId2].exports)
43-
// .map(p => p.length)
44-
// .sort()
45-
// ).toEqual(
46-
// OPTIMIZATION === "deterministic"
47-
// ? [1, 2, 2, 2, 2, 2, 2, 8]
48-
// : [1, 1, 1, 1, 1, 1, 1, 8]
49-
// );
50-
// });
30+
it("should mangle names and remove exports even with toString named export (CJS)", () => {
31+
expect(abc2).toBe("abc");
32+
expect(toString2).toBe(Object.prototype.toString);
33+
setToString2();
34+
const toString3 = require("./commonjs").toString;
35+
expect(toString3()).toBe("toString");
36+
expect(a2).toBe("single char");
37+
expect(equals2).toBe("single char non-identifier");
38+
expect($12).toBe("double char");
39+
expect(__12).toBe("3 chars");
40+
expect(
41+
Object.keys(require.cache[moduleId2].exports)
42+
.map(p => p.length)
43+
.sort()
44+
).toEqual(
45+
OPTIMIZATION === "deterministic"
46+
? [1, 2, 2, 2, 2, 2, 2, 8]
47+
: [1, 1, 1, 1, 1, 1, 1, 8]
48+
);
49+
});

tests/rspack-test/esmOutputCases/basic/split-runtime-chunk/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const other = __webpack_require__("./other.js");
77

88

99
it('should have correct value', () => {
10-
expect((0,other.value)()).toBe(42)
10+
expect((0,other.U)()).toBe(42)
1111
})
1212

1313
```
@@ -20,7 +20,7 @@ __webpack_require__.add({
2020
!*** ./other.js ***!
2121
\******************/
2222
(__unused_rspack_module, exports) {
23-
exports.value = () => 42
23+
exports.U = () => 42
2424

2525
},
2626
});

tests/rspack-test/esmOutputCases/interop/contains-cjs/__snapshots__/esm.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.foo = 42
1717
(__unused_rspack_module, exports) {
1818
var __webpack_unused_export__;
1919
__webpack_unused_export__ = true
20-
exports["default"] = 'esm'
20+
exports.A = 'esm'
2121

2222

2323
},
@@ -34,7 +34,7 @@ it('should have correct import', () => {
3434
expect(foo).toHaveProperty('foo')
3535
expect(foo.foo).toBe(42)
3636

37-
expect(from_esm["default"]).toBe('esm')
37+
expect(from_esm.A).toBe('esm')
3838

3939
expect(foo.foo).toBe(42)
4040
expect((/* inlined export .value */"value")).toBe('value')

tests/rspack-test/esmOutputCases/interop/type-module/__snapshots__/esm.snap.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[exportSymbol()] = () => 'bar'
1616
!*** ./cjs.cjs ***!
1717
\*****************/
1818
(__unused_rspack_module, exports) {
19-
exports.foo = () => 'foo'
19+
exports.p = () => 'foo'
2020

2121

2222
},
@@ -37,9 +37,9 @@ const cjs_unknown = __webpack_require__("./cjs-unknown.cjs");
3737
var bar_0 = cjs_unknown.bar;
3838

3939
const cjs = __webpack_require__("./cjs.cjs");
40-
var foo_0 = cjs.foo;
40+
var p = cjs.p;
4141

42-
export { bar_0 as bar, foo_0 as foo };
42+
export { bar_0 as bar, p as foo };
4343
export default cjs_unknown;
4444

4545
```

tests/rspack-test/esmOutputCases/namespace/basic/__snapshots__/esm.snap.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ __webpack_require__.add({
66
!*** ./foo.js ***!
77
\****************/
88
(__unused_rspack_module, exports) {
9-
exports.foo = 123
9+
exports.p = 123
1010

1111

1212
},
@@ -16,8 +16,8 @@ const foo = __webpack_require__("./foo.js");
1616

1717

1818
it('should import namespace', () => {
19-
expect(foo.foo).toBe(123);
20-
expect(foo.foo).toBe(123);
19+
expect(foo.p).toBe(123);
20+
expect(foo.p).toBe(123);
2121
})
2222

2323

0 commit comments

Comments
 (0)