Skip to content

Commit dccf124

Browse files
authored
refactor: render mf runtime by runtime template (#12298)
1 parent 9051144 commit dccf124

10 files changed

Lines changed: 130 additions & 75 deletions

crates/rspack_plugin_mf/src/container/remote_runtime_module.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ impl RuntimeModule for RemoteRuntimeModule {
3737
RuntimeModuleStage::Attach
3838
}
3939

40+
fn template(&self) -> Vec<(String, String)> {
41+
vec![(
42+
self.id.to_string(),
43+
include_str!("./remotesLoading.ejs").to_string(),
44+
)]
45+
}
46+
4047
async fn generate(&self, compilation: &Compilation) -> rspack_error::Result<String> {
4148
let chunk_ukey = self
4249
.chunk
@@ -100,7 +107,9 @@ impl RuntimeModule for RemoteRuntimeModule {
100107
.render_runtime_globals(&RuntimeGlobals::ENSURE_CHUNK_HANDLERS),
101108
)
102109
} else {
103-
include_str!("./remotesLoading.js").to_string()
110+
compilation
111+
.runtime_template
112+
.render(self.id.as_str(), None)?
104113
};
105114
Ok(format!(
106115
r#"

crates/rspack_plugin_mf/src/container/remotesLoading.js renamed to crates/rspack_plugin_mf/src/container/remotesLoading.ejs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
__webpack_require__.f.remotes = function (chunkId, promises) {
2-
var chunkMapping = __webpack_require__.remotesLoadingData.chunkMapping;
3-
var moduleIdToRemoteDataMapping = __webpack_require__.remotesLoadingData.moduleIdToRemoteDataMapping;
4-
if (__webpack_require__.o(chunkMapping, chunkId)) {
1+
<%- ENSURE_CHUNK_HANDLERS %>.remotes = function (chunkId, promises) {
2+
var chunkMapping = <%- REQUIRE %>.remotesLoadingData.chunkMapping;
3+
var moduleIdToRemoteDataMapping = <%- REQUIRE %>.remotesLoadingData.moduleIdToRemoteDataMapping;
4+
if (<%- HAS_OWN_PROPERTY %>(chunkMapping, chunkId)) {
55
chunkMapping[chunkId].forEach(function (id) {
6-
var getScope = __webpack_require__.R;
6+
var getScope = <%- CURRENT_REMOTE_GET_SCOPE %>;
77
if (!getScope) getScope = [];
88
var data = moduleIdToRemoteDataMapping[id];
99
if (getScope.indexOf(data) >= 0) return;
@@ -14,7 +14,7 @@ __webpack_require__.f.remotes = function (chunkId, promises) {
1414
if (typeof error.message === "string")
1515
error.message +=
1616
'\nwhile loading "' + data.name + '" from ' + data.externalModuleId;
17-
__webpack_require__.m[id] = function () {
17+
<%- MODULE_FACTORIES %>[id] = function () {
1818
throw error;
1919
};
2020
data.p = 0;
@@ -36,18 +36,18 @@ __webpack_require__.f.remotes = function (chunkId, promises) {
3636
}
3737
};
3838
var onExternal = function (external, _, first) {
39-
return external ? handleFunction(__webpack_require__.I, data.shareScope, 0, external, onInitialized, first) : onError();
39+
return external ? handleFunction(<%- INITIALIZE_SHARING %>, data.shareScope, 0, external, onInitialized, first) : onError();
4040
};
4141
var onInitialized = function (_, external, first) {
4242
return handleFunction(external.get, data.name, getScope, 0, onFactory, first);
4343
};
4444
var onFactory = function (factory) {
4545
data.p = 1;
46-
__webpack_require__.m[id] = function (module) {
46+
<%- MODULE_FACTORIES %>[id] = function (module) {
4747
module.exports = factory();
4848
};
4949
};
50-
handleFunction(__webpack_require__, data.externalModuleId, 0, 0, onExternal, 1);
50+
handleFunction(<%- REQUIRE %>, data.externalModuleId, 0, 0, onExternal, 1);
5151
});
5252
}
5353
};

crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ impl ConsumeSharedRuntimeModule {
2424
enhanced,
2525
)
2626
}
27+
28+
fn get_template_id(&self, template_id: TemplateId) -> String {
29+
match template_id {
30+
TemplateId::Common => format!("{}_consumesCommon", self.id),
31+
TemplateId::Initial => format!("{}_consumesInitial", self.id),
32+
TemplateId::Loading => format!("{}_consumesLoading", self.id),
33+
}
34+
}
35+
}
36+
37+
enum TemplateId {
38+
Common,
39+
Initial,
40+
Loading,
2741
}
2842

2943
#[async_trait::async_trait]
@@ -36,6 +50,23 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
3650
RuntimeModuleStage::Attach
3751
}
3852

53+
fn template(&self) -> Vec<(String, String)> {
54+
vec![
55+
(
56+
self.get_template_id(TemplateId::Common),
57+
include_str!("./consumesCommon.ejs").to_string(),
58+
),
59+
(
60+
self.get_template_id(TemplateId::Initial),
61+
include_str!("./consumesInitial.ejs").to_string(),
62+
),
63+
(
64+
self.get_template_id(TemplateId::Loading),
65+
include_str!("./consumesLoading.ejs").to_string(),
66+
),
67+
]
68+
}
69+
3970
async fn generate(&self, compilation: &Compilation) -> rspack_error::Result<String> {
4071
let chunk_ukey = self
4172
.chunk
@@ -151,14 +182,20 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
151182
}
152183
return Ok(source);
153184
}
154-
source += include_str!("./consumesCommon.js");
185+
source += &compilation
186+
.runtime_template
187+
.render(&self.get_template_id(TemplateId::Common), None)?;
155188
if !initial_consumes.is_empty() {
156-
source += include_str!("./consumesInitial.js");
189+
source += &compilation
190+
.runtime_template
191+
.render(&self.get_template_id(TemplateId::Initial), None)?;
157192
}
158193
if ChunkGraph::get_chunk_runtime_requirements(compilation, &chunk_ukey)
159194
.contains(RuntimeGlobals::ENSURE_CHUNK_HANDLERS)
160195
{
161-
source += include_str!("./consumesLoading.js");
196+
source += &compilation
197+
.runtime_template
198+
.render(&self.get_template_id(TemplateId::Loading), None)?;
162199
}
163200
Ok(source)
164201
}

crates/rspack_plugin_mf/src/sharing/consumesCommon.js renamed to crates/rspack_plugin_mf/src/sharing/consumesCommon.ejs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ var satisfy = function(range, version) {
373373
return !!p();
374374
}
375375
var ensureExistence = function(scopeName, key) {
376-
var scope = __webpack_require__.S[scopeName];
377-
if(!scope || !__webpack_require__.o(scope, key)) throw new Error("Shared module " + key + " doesn't exist in shared scope " + scopeName);
376+
var scope = <%- SHARE_SCOPE_MAP %>[scopeName];
377+
if(!scope || !<%- HAS_OWN_PROPERTY %>(scope, key)) throw new Error("Shared module " + key + " doesn't exist in shared scope " + scopeName);
378378
return scope;
379379
};
380380
var findVersion = function(scope, key) {
@@ -438,17 +438,17 @@ var get = function(entry) {
438438
return entry.get()
439439
};
440440
var init = function(fn) { return function(scopeName, a, b, c) {
441-
var promise = __webpack_require__.I(scopeName);
442-
if (promise && promise.then) return promise.then(fn.bind(fn, scopeName, __webpack_require__.S[scopeName], a, b, c));
443-
return fn(scopeName, __webpack_require__.S[scopeName], a, b, c);
441+
var promise = <%- INITIALIZE_SHARING %>(scopeName);
442+
if (promise && promise.then) return promise.then(fn.bind(fn, scopeName, <%- SHARE_SCOPE_MAP %>[scopeName], a, b, c));
443+
return fn(scopeName, <%- SHARE_SCOPE_MAP %>[scopeName], a, b, c);
444444
}; };
445445

446446
var load = /*#__PURE__*/ init(function(scopeName, scope, key) {
447447
ensureExistence(scopeName, key);
448448
return get(findVersion(scope, key));
449449
});
450450
var loadFallback = /*#__PURE__*/ init(function(scopeName, scope, key, fallback) {
451-
return scope && __webpack_require__.o(scope, key) ? get(findVersion(scope, key)) : fallback();
451+
return scope && <%- HAS_OWN_PROPERTY %>(scope, key) ? get(findVersion(scope, key)) : fallback();
452452
});
453453
var loadVersionCheck = /*#__PURE__*/ init(function(scopeName, scope, key, version) {
454454
ensureExistence(scopeName, key);
@@ -471,23 +471,23 @@ var loadStrictSingletonVersionCheck = /*#__PURE__*/ init(function(scopeName, sco
471471
return getStrictSingletonVersion(scope, scopeName, key, version);
472472
});
473473
var loadVersionCheckFallback = /*#__PURE__*/ init(function(scopeName, scope, key, version, fallback) {
474-
if(!scope || !__webpack_require__.o(scope, key)) return fallback();
474+
if(!scope || !<%- HAS_OWN_PROPERTY %>(scope, key)) return fallback();
475475
return get(findValidVersion(scope, key, version) || warnInvalidVersion(scope, scopeName, key, version) || findVersion(scope, key));
476476
});
477477
var loadSingletonFallback = /*#__PURE__*/ init(function(scopeName, scope, key, fallback) {
478-
if(!scope || !__webpack_require__.o(scope, key)) return fallback();
478+
if(!scope || !<%- HAS_OWN_PROPERTY %>(scope, key)) return fallback();
479479
return getSingleton(scope, scopeName, key);
480480
});
481481
var loadSingletonVersionCheckFallback = /*#__PURE__*/ init(function(scopeName, scope, key, version, fallback) {
482-
if(!scope || !__webpack_require__.o(scope, key)) return fallback();
482+
if(!scope || !<%- HAS_OWN_PROPERTY %>(scope, key)) return fallback();
483483
return getSingletonVersion(scope, scopeName, key, version);
484484
});
485485
var loadStrictVersionCheckFallback = /*#__PURE__*/ init(function(scopeName, scope, key, version, fallback) {
486-
var entry = scope && __webpack_require__.o(scope, key) && findValidVersion(scope, key, version);
486+
var entry = scope && <%- HAS_OWN_PROPERTY %>(scope, key) && findValidVersion(scope, key, version);
487487
return entry ? get(entry) : fallback();
488488
});
489489
var loadStrictSingletonVersionCheckFallback = /*#__PURE__*/ init(function(scopeName, scope, key, version, fallback) {
490-
if(!scope || !__webpack_require__.o(scope, key)) return fallback();
490+
if(!scope || !<%- HAS_OWN_PROPERTY %>(scope, key)) return fallback();
491491
return getStrictSingletonVersion(scope, scopeName, key, version);
492492
});
493493
var resolveHandler = function(data) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%- REQUIRE %>.consumesLoadingData.initialConsumes.forEach(function(id) {
2+
<%- MODULE_FACTORIES %>[id] = function(module) {
3+
// Handle case when module is used sync
4+
installedModules[id] = 0;
5+
delete <%- MODULE_CACHE %>[id];
6+
var factory = resolveHandler(<%- REQUIRE %>.consumesLoadingData.moduleIdToConsumeDataMapping[id])();
7+
if (typeof factory !== "function")
8+
throw new Error(
9+
"Shared module is not available for eager consumption: " + id
10+
);
11+
module.exports = factory();
12+
};
13+
});

crates/rspack_plugin_mf/src/sharing/consumesInitial.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<%- ENSURE_CHUNK_HANDLERS %>.consumes = function(chunkId, promises) {
2+
var moduleIdToConsumeDataMapping = <%- REQUIRE %>.consumesLoadingData.moduleIdToConsumeDataMapping
3+
var chunkMapping = <%- REQUIRE %>.consumesLoadingData.chunkMapping;
4+
if(<%- HAS_OWN_PROPERTY %>(chunkMapping, chunkId)) {
5+
chunkMapping[chunkId].forEach(function(id) {
6+
if(<%- HAS_OWN_PROPERTY %>(installedModules, id)) return promises.push(installedModules[id]);
7+
var onFactory = function(factory) {
8+
installedModules[id] = 0;
9+
<%- MODULE_FACTORIES %>[id] = function(module) {
10+
delete <%- MODULE_CACHE %>[id];
11+
module.exports = factory();
12+
}
13+
};
14+
var onError = function(error) {
15+
delete installedModules[id];
16+
<%- MODULE_FACTORIES %>[id] = function(module) {
17+
delete <%- MODULE_CACHE %>[id];
18+
throw error;
19+
}
20+
};
21+
try {
22+
var promise = resolveHandler(moduleIdToConsumeDataMapping[id])();
23+
if(promise.then) {
24+
promises.push(installedModules[id] = promise.then(onFactory)['catch'](onError));
25+
} else onFactory(promise);
26+
} catch(e) { onError(e); }
27+
});
28+
}
29+
}

crates/rspack_plugin_mf/src/sharing/consumesLoading.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

crates/rspack_plugin_mf/src/sharing/initializeSharing.js renamed to crates/rspack_plugin_mf/src/sharing/initializeSharing.ejs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var initPromises = {};
22
var initTokens = {};
3-
__webpack_require__.I = function(name, initScope) {
3+
<%- INITIALIZE_SHARING %> = function(name, initScope) {
44
if (!initScope) initScope = [];
55
// handling circular init calls
66
var initToken = initTokens[name];
@@ -10,14 +10,14 @@ __webpack_require__.I = function(name, initScope) {
1010
// only runs once
1111
if (initPromises[name]) return initPromises[name];
1212
// creates a new share scope if needed
13-
if (!__webpack_require__.o(__webpack_require__.S, name))
14-
__webpack_require__.S[name] = {};
13+
if (!<%- HAS_OWN_PROPERTY %>(<%- SHARE_SCOPE_MAP %>, name))
14+
<%- SHARE_SCOPE_MAP %>[name] = {};
1515
// runs all init snippets from all modules reachable
16-
var scope = __webpack_require__.S[name];
16+
var scope = <%- SHARE_SCOPE_MAP %>[name];
1717
var warn = function (msg) {
1818
if (typeof console !== "undefined" && console.warn) console.warn(msg);
1919
};
20-
var uniqueName = __webpack_require__.initializeSharingData.uniqueName;
20+
var uniqueName = <%- REQUIRE %>.initializeSharingData.uniqueName;
2121
var register = function (name, version, factory, eager) {
2222
var versions = (scope[name] = scope[name] || {});
2323
var activeVersion = versions[version];
@@ -35,13 +35,13 @@ __webpack_require__.I = function(name, initScope) {
3535
warn("Initialization of sharing external failed: " + err);
3636
};
3737
try {
38-
var module = __webpack_require__(id);
38+
var module = <%- REQUIRE %>(id);
3939
if (!module) return;
4040
var initFn = function (module) {
4141
return (
4242
module &&
4343
module.init &&
44-
module.init(__webpack_require__.S[name], initScope)
44+
module.init(<%- SHARE_SCOPE_MAP %>[name], initScope)
4545
);
4646
};
4747
if (module.then) return promises.push(module.then(initFn, handleError));
@@ -53,7 +53,7 @@ __webpack_require__.I = function(name, initScope) {
5353
}
5454
};
5555
var promises = [];
56-
var scopeToSharingDataMapping = __webpack_require__.initializeSharingData.scopeToSharingDataMapping;
56+
var scopeToSharingDataMapping = <%- REQUIRE %>.initializeSharingData.scopeToSharingDataMapping;
5757
if (scopeToSharingDataMapping[name]) {
5858
scopeToSharingDataMapping[name].forEach(function (stage) {
5959
if (typeof stage === "object") register(stage.name, stage.version, stage.factory, stage.eager);

crates/rspack_plugin_mf/src/sharing/share_runtime_module.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ impl RuntimeModule for ShareRuntimeModule {
2929
self.id
3030
}
3131

32+
fn template(&self) -> Vec<(String, String)> {
33+
vec![(
34+
self.id.to_string(),
35+
include_str!("./initializeSharing.ejs").to_string(),
36+
)]
37+
}
38+
3239
async fn generate(&self, compilation: &Compilation) -> rspack_error::Result<String> {
3340
let chunk_ukey = self
3441
.chunk
@@ -111,7 +118,9 @@ impl RuntimeModule for ShareRuntimeModule {
111118
.render_runtime_globals(&RuntimeGlobals::INITIALIZE_SHARING)
112119
)
113120
} else {
114-
include_str!("./initializeSharing.js").to_string()
121+
compilation
122+
.runtime_template
123+
.render(self.id.as_str(), None)?
115124
};
116125
Ok(format!(
117126
r#"

0 commit comments

Comments
 (0)