Skip to content

Commit e6680dd

Browse files
committed
fix(runtime): align rspack context global rendering
1 parent d032084 commit e6680dd

235 files changed

Lines changed: 459 additions & 434 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/rspack_core/src/artifacts/runtime_proxy_metadata_artifact.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl RuntimeProxyMetadata {
5757
let mut source = String::new();
5858
for (_, runtime_global) in self.context_fields().iter_names() {
5959
let (Some(property_name), Some(lexical_name)) = (
60-
runtime_global.property_name(),
60+
runtime_global.rspack_context_property_name(),
6161
runtime_global.to_lexical_name(),
6262
) else {
6363
continue;

crates/rspack_core/src/runtime_globals.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ pub fn runtime_globals_property_name(runtime_globals: &RuntimeGlobals) -> Option
389389
RuntimeGlobals::STARTUP_NO_DEFAULT => "x (no default handler)",
390390
RuntimeGlobals::ENSURE_CHUNK_INCLUDE_ENTRIES => "f (include entries)",
391391
RuntimeGlobals::STARTUP => "x",
392-
RuntimeGlobals::MAKE_NAMESPACE_OBJECT => "N",
392+
RuntimeGlobals::MAKE_NAMESPACE_OBJECT => "r",
393393
RuntimeGlobals::MAKE_DEFERRED_NAMESPACE_OBJECT => "z",
394394
RuntimeGlobals::MAKE_OPTIMIZED_DEFERRED_NAMESPACE_OBJECT => "zO",
395395
RuntimeGlobals::DEFERRED_MODULES_ASYNC_TRANSITIVE_DEPENDENCIES => "zT",
@@ -528,6 +528,14 @@ impl RuntimeGlobals {
528528
runtime_globals_property_name(self)
529529
}
530530

531+
pub fn rspack_context_property_name(&self) -> Option<&'static str> {
532+
if *self == RuntimeGlobals::MAKE_NAMESPACE_OBJECT {
533+
Some("N")
534+
} else {
535+
self.property_name()
536+
}
537+
}
538+
531539
pub fn name(&self) -> Option<&'static str> {
532540
RUNTIME_GLOBAL_MAP.0.get(self).copied()
533541
}
@@ -536,6 +544,14 @@ impl RuntimeGlobals {
536544
RUNTIME_GLOBAL_MAP.2.get(property_name).copied()
537545
}
538546

547+
pub fn from_rspack_context_property_name(property_name: &str) -> Option<Self> {
548+
if property_name == "N" {
549+
return Some(RuntimeGlobals::MAKE_NAMESPACE_OBJECT);
550+
}
551+
let runtime_global = Self::from_property_name(property_name)?;
552+
(runtime_global != RuntimeGlobals::MAKE_NAMESPACE_OBJECT).then_some(runtime_global)
553+
}
554+
539555
pub fn renderable_require_scope(self) -> Self {
540556
self.intersection(*REQUIRE_SCOPE_GLOBALS)
541557
}

crates/rspack_core/src/runtime_template.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn runtime_globals_to_render_map(render_mode: RuntimeGlobalRenderMode) -> Runtim
295295
} else if runtime_globals == RuntimeGlobals::REQUIRE {
296296
format!("{}.r", runtime_variable_name(&RuntimeVariable::Context))
297297
} else if runtime_globals.renderable_require_scope() == runtime_globals {
298-
if let Some(name) = runtime_globals.property_name() {
298+
if let Some(name) = runtime_globals.rspack_context_property_name() {
299299
format!(
300300
"{}{}",
301301
runtime_variable_name(&RuntimeVariable::Context),

crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ fn static_require_member_chain(
204204
if for_name == API_REQUIRE
205205
&& let Some(property) = members.first()
206206
{
207-
if let Some(runtime_global) = RuntimeGlobals::from_property_name(property.as_ref()) {
207+
if let Some(runtime_global) =
208+
RuntimeGlobals::from_rspack_context_property_name(property.as_ref())
209+
{
208210
let dep_span = if members.len() > 1 {
209211
member_ranges
210212
.and_then(|ranges| ranges.get(1))

crates/rspack_plugin_javascript/src/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ fn render_runtime_context_fields(
514514
continue;
515515
}
516516

517-
let Some(key) = runtime_global.property_name() else {
517+
let Some(key) = runtime_global.rspack_context_property_name() else {
518518
continue;
519519
};
520520
let Some(lexical_name) = runtime_global.to_lexical_name() else {
@@ -564,7 +564,7 @@ fn render_runtime_context_field_initializers(
564564
let runtime_context = runtime_template.render_runtime_variable(&RuntimeVariable::Context);
565565

566566
for (_, runtime_global) in metadata.lexical_fields().iter_names() {
567-
let Some(key) = runtime_global.property_name() else {
567+
let Some(key) = runtime_global.rspack_context_property_name() else {
568568
continue;
569569
};
570570
let Some(lexical_name) = runtime_global.to_lexical_name() else {

packages/rspack/src/RuntimeGlobals.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,26 @@ function renderRuntimeGlobals(
508508
runtimeGlobals: RuntimeGlobals,
509509
_compilerOptions?: RspackOptionsNormalized,
510510
): string {
511-
const scope_name = renderRuntimeVariables(
511+
const require_name = renderRuntimeVariables(
512512
RuntimeVariable.Require,
513513
_compilerOptions,
514514
);
515+
const context_name = renderRuntimeVariables(
516+
RuntimeVariable.Context,
517+
_compilerOptions,
518+
);
519+
const usesRuntimeContext =
520+
_compilerOptions?.experiments.runtimeMode === 'rspack';
521+
const scope_name = usesRuntimeContext ? context_name : require_name;
515522
const exports_name = renderRuntimeVariables(
516523
RuntimeVariable.Exports,
517524
_compilerOptions,
518525
);
519526
switch (runtimeGlobals) {
520527
case RuntimeGlobals.require:
521-
return scope_name;
528+
return usesRuntimeContext ? `${context_name}.r` : require_name;
522529
case RuntimeGlobals.requireScope:
523-
return `${scope_name}.*`;
530+
return usesRuntimeContext ? context_name : `${require_name}.*`;
524531
case RuntimeGlobals.exports:
525532
return exports_name;
526533
case RuntimeGlobals.thisAsExports:
@@ -560,7 +567,7 @@ function renderRuntimeGlobals(
560567
case RuntimeGlobals.definePropertyGetters:
561568
return `${scope_name}.d`;
562569
case RuntimeGlobals.makeNamespaceObject:
563-
return `${scope_name}.N`;
570+
return usesRuntimeContext ? `${context_name}.N` : `${require_name}.r`;
564571
case RuntimeGlobals.createFakeNamespaceObject:
565572
return `${scope_name}.t`;
566573
case RuntimeGlobals.compatGetDefaultExport:

tests/rspack-test/builtinCases/plugin-asset/asset-advanced/__snapshots__/output.snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
(self["rspackChunk"] = self["rspackChunk"] || []).push([["main"], {
44
"./index.js"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
5-
__webpack_require__.N(__webpack_exports__);
5+
__webpack_require__.r(__webpack_exports__);
66
/* import */ var _images_file_svg__rspack_import_0 = __webpack_require__("./images/file.svg");
77

88

tests/rspack-test/builtinCases/plugin-asset/asset-simple/__snapshots__/output.snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
(self["rspackChunk"] = self["rspackChunk"] || []).push([["main"], {
44
"./index.js"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
5-
__webpack_require__.N(__webpack_exports__);
5+
__webpack_require__.r(__webpack_exports__);
66
/* import */ var _images_file_png__rspack_import_0 = __webpack_require__("./images/file.png");
77
/* import */ var _images_file_jpg__rspack_import_1 = __webpack_require__("./images/file.jpg");
88
/* import */ var _images_file_svg__rspack_import_2 = __webpack_require__("./images/file.svg");

tests/rspack-test/builtinCases/plugin-asset/asset-source/__snapshots__/output.snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
(self["rspackChunk"] = self["rspackChunk"] || []).push([["main"], {
44
"./index.js"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
5-
__webpack_require__.N(__webpack_exports__);
5+
__webpack_require__.r(__webpack_exports__);
66
/* import */ var _data_txt__rspack_import_0 = __webpack_require__("./data.txt");
77

88

tests/rspack-test/builtinCases/plugin-css-modules/modules-composes/__snapshots__/output.snap.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"use strict";
3737
(self["rspackChunk"] = self["rspackChunk"] || []).push([["main"], {
3838
"./index.js"(__unused_rspack_module, __webpack_exports__, __webpack_require__) {
39-
__webpack_require__.N(__webpack_exports__);
39+
__webpack_require__.r(__webpack_exports__);
4040
/* import */ var _style_module_css__rspack_import_0 = __webpack_require__("./style.module.css");
4141

4242

@@ -48,7 +48,7 @@ var exports = {
4848
"b": "./_b.module_./_b--/__770815d283e5419e<770" + " " + "./_b.module_./_b-1--/__770815d283e5419e<770",
4949
};
5050

51-
__webpack_require__.N(module.exports = exports);
51+
__webpack_require__.r(module.exports = exports);
5252

5353

5454
},
@@ -58,7 +58,7 @@ var exports = {
5858
"d": "./_d.module_./_d--/__88281ab83c534746<882" + " " + "./_d.module_./_d-1--/__88281ab83c534746<882",
5959
};
6060

61-
__webpack_require__.N(module.exports = exports);
61+
__webpack_require__.r(module.exports = exports);
6262

6363

6464
},
@@ -68,7 +68,7 @@ var exports = {
6868
"f": "./_f.module_./_f--/__4abe9cc818c5f8e5<4ab" + " " + "./_f.module_./_f-1--/__4abe9cc818c5f8e5<4ab",
6969
};
7070

71-
__webpack_require__.N(module.exports = exports);
71+
__webpack_require__.r(module.exports = exports);
7272

7373

7474
},
@@ -79,7 +79,7 @@ var exports = {
7979
"root-class": "./_style.module_./_root-class--/__2f98424dcb9d6a7c<2f9" + " " + "./_style.module_./_chain1--/__2f98424dcb9d6a7c<2f9" + " " + "./_style.module_./_chain2--/__2f98424dcb9d6a7c<2f9" + " " + "e" + " " + __webpack_require__("./f.module.css")["f"] + " " + "c" + " " + __webpack_require__("./d.module.css")["d"] + " " + "a" + " " + __webpack_require__("./b.module.css")["b"],
8080
};
8181

82-
__webpack_require__.N(module.exports = exports);
82+
__webpack_require__.r(module.exports = exports);
8383

8484

8585
},

0 commit comments

Comments
 (0)