Skip to content

Commit 41ec155

Browse files
Merge branch 'feat/mf-layers' into cursor/manifest-rsc-client-data-d493
2 parents 98fd8ba + d213490 commit 41ec155

File tree

16 files changed

+84
-69
lines changed

16 files changed

+84
-69
lines changed

crates/rspack_core/src/stats/mod.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,7 @@ impl Stats<'_> {
11391139
options: &ExtendedStatsOptions,
11401140
) -> Result<StatsModule<'a>> {
11411141
let identifier = module.identifier();
1142-
let mgm = module_graph
1143-
.module_graph_module_by_identifier(&identifier)
1144-
.unwrap_or_else(|| panic!("Could not find ModuleGraphModule by identifier: {identifier:?}"));
1142+
let mgm = module_graph.module_graph_module_by_identifier(&identifier);
11451143

11461144
let built = if executed {
11471145
self.module_executor_is_module_built(&identifier)
@@ -1294,7 +1292,7 @@ impl Stats<'_> {
12941292
self
12951293
.build_chunk_graph_artifact()
12961294
.chunk_graph
1297-
.get_chunk_graph_module(mgm.module_identifier)
1295+
.get_chunk_graph_module(identifier)
12981296
.map(|cgm| {
12991297
cgm
13001298
.chunks
@@ -1335,8 +1333,8 @@ impl Stats<'_> {
13351333

13361334
if options.reasons {
13371335
let mut reasons: Vec<StatsModuleReason> = mgm
1338-
.incoming_connections()
1339-
.iter()
1336+
.into_iter()
1337+
.flat_map(|mgm| mgm.incoming_connections().iter())
13401338
.filter_map(|dep_id| {
13411339
// the connection is removed
13421340
let connection = module_graph.connection_by_dependency_id(dep_id)?;
@@ -1447,13 +1445,17 @@ impl Stats<'_> {
14471445
if options.optimization_bailout {
14481446
stats.optimization_bailout = Some(
14491447
mgm
1450-
.optimization_bailout
1451-
.iter()
1452-
.map(|b| match b {
1453-
OptimizationBailoutItem::Message(msg) => Cow::Borrowed(msg.as_str()),
1454-
b => Cow::Owned(b.to_string()),
1448+
.map(|mgm| {
1449+
mgm
1450+
.optimization_bailout
1451+
.iter()
1452+
.map(|b| match b {
1453+
OptimizationBailoutItem::Message(msg) => Cow::Borrowed(msg.as_str()),
1454+
b => Cow::Owned(b.to_string()),
1455+
})
1456+
.collect()
14551457
})
1456-
.collect(),
1458+
.unwrap_or_default(),
14571459
);
14581460
}
14591461

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
};

crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
145145
)
146146
};
147147
module_id_to_consume_data_mapping.insert(id, consume_data);
148-
149148
}
150149
};
151150
// Match enhanced/webpack behavior: include all referenced chunks so async ones are mapped too

crates/rspack_plugin_mf/src/sharing/share_runtime_module.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ impl RuntimeModule for ShareRuntimeModule {
9696
"{{ name: {}, version: {}, factory: {}, eager: {}, layer: {}, treeShakingMode: {}",
9797
json_stringify(&info.name),
9898
json_stringify(&info.version.to_string()),
99-
10099
info.factory,
101100
if info.eager { "1" } else { "0" },
102101
json_stringify(&info.layer),

crates/rspack_plugin_mf/src/sharing/shared_used_exports_optimizer_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rspack_core::{
44
AsyncDependenciesBlockIdentifier, ChunkUkey, Compilation,
55
CompilationAdditionalTreeRuntimeRequirements, CompilationDependencyReferencedExports,
66
CompilationOptimizeDependencies, CompilationProcessAssets, DependenciesBlock, Dependency,
7-
DependencyId, DependencyType, ExportsInfoArtifact, ExtendedReferencedExport, Module, ModuleGraph,
7+
DependencyId, DependencyType, ExportsInfoArtifact, ExtendedReferencedExport, ModuleGraph,
88
ModuleIdentifier, Plugin, RuntimeGlobals, RuntimeModule, RuntimeModuleExt, RuntimeSpec,
99
SideEffectsOptimizeArtifact,
1010
build_module_graph::BuildModuleGraphArtifact,

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+
});

0 commit comments

Comments
 (0)