Skip to content

Commit 0af5acb

Browse files
committed
refactor: remove runtime module prefix accessor
1 parent 5244de8 commit 0af5acb

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

crates/rspack_binding_api/src/plugins/interceptor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,11 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
14891489
runtime_template: &runtime_template,
14901490
};
14911491
let source_string = module.generate(&context).await?;
1492+
let runtime_module_prefix = if compilation.runtime_template.render_mode().is_legacy() {
1493+
"webpack/runtime/"
1494+
} else {
1495+
"rspack/runtime/"
1496+
};
14921497
let arg = JsRuntimeModuleArg {
14931498
module: JsRuntimeModule {
14941499
source: Some(JsSourceToJs::from(source_string)),
@@ -1497,7 +1502,7 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
14971502
name: module
14981503
.name()
14991504
.as_str()
1500-
.cow_replace(compilation.runtime_template.runtime_module_prefix(), "")
1505+
.cow_replace(runtime_module_prefix, "")
15011506
.into_owned(),
15021507
stage: module.stage().into(),
15031508
isolate: module.should_isolate(compilation.options.experiments.runtime_mode),

crates/rspack_core/src/runtime_template.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ pub enum RuntimeTemplateRenderMode {
100100
}
101101

102102
impl RuntimeTemplateRenderMode {
103+
/// Returns whether all runtime globals use webpack-compatible identifiers.
104+
pub fn is_legacy(self) -> bool {
105+
matches!(self, Self::Webpack)
106+
}
107+
103108
/// Returns the runtime-global render mode for ordinary module code generation.
104109
pub fn module_render_mode(self) -> RuntimeGlobalsRenderMode {
105110
match self {
@@ -123,13 +128,6 @@ impl RuntimeTemplateRenderMode {
123128
Self::Rspack => RuntimeGlobalsRenderMode::RspackContext,
124129
}
125130
}
126-
127-
fn runtime_module_prefix(self) -> &'static str {
128-
match self {
129-
Self::Webpack => "webpack/runtime/",
130-
Self::Rspack => "rspack/runtime/",
131-
}
132-
}
133131
}
134132

135133
impl From<RuntimeMode> for RuntimeTemplateRenderMode {
@@ -256,7 +254,7 @@ impl RuntimeTemplate {
256254
})),
257255
);
258256

259-
let runtime_globals_cloned = runtime_globals.clone();
257+
let runtime_globals_cloned = runtime_globals;
260258
dojang.functions.insert(
261259
"weak".into(),
262260
FunctionContainer::F1(Box::new(move |runtime_global: Operand| {
@@ -283,8 +281,9 @@ impl RuntimeTemplate {
283281
}
284282
}
285283

286-
pub fn runtime_module_prefix(&self) -> &'static str {
287-
self.render_mode.runtime_module_prefix()
284+
/// Returns the render-mode combination selected for this compilation.
285+
pub fn render_mode(&self) -> RuntimeTemplateRenderMode {
286+
self.render_mode
288287
}
289288

290289
pub fn create_runtime_module_identifier(&self, name: &str) -> Identifier {
@@ -293,15 +292,16 @@ impl RuntimeTemplate {
293292
} else {
294293
name
295294
};
296-
Identifier::from(format!(
297-
"{}{}",
298-
self.runtime_module_prefix(),
299-
module_name.to_snake_case()
300-
))
295+
self.create_custom_runtime_module_identifier(&module_name.to_snake_case())
301296
}
302297

303298
pub fn create_custom_runtime_module_identifier(&self, custom: &str) -> Identifier {
304-
Identifier::from(format!("{}{custom}", self.runtime_module_prefix()))
299+
let prefix = if self.render_mode.is_legacy() {
300+
"webpack/runtime/"
301+
} else {
302+
"rspack/runtime/"
303+
};
304+
Identifier::from(format!("{prefix}{custom}"))
305305
}
306306

307307
/// Creates the template used to render runtime globals referenced by ordinary modules.

crates/rspack_plugin_runtime/src/runtime_module/export_require.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub struct ExportRequireRuntimeModule {}
1212

1313
impl ExportRequireRuntimeModule {
1414
pub fn new(runtime_template: &RuntimeTemplate) -> Self {
15-
let name = if runtime_template.runtime_module_prefix() == "rspack/runtime/" {
16-
EXPORT_REQUIRE_RSPACK_RUNTIME_MODULE_ID
17-
} else {
15+
let name = if runtime_template.render_mode().is_legacy() {
1816
EXPORT_REQUIRE_RUNTIME_MODULE_ID
17+
} else {
18+
EXPORT_REQUIRE_RSPACK_RUNTIME_MODULE_ID
1919
};
2020
Self::with_name(runtime_template, name)
2121
}

0 commit comments

Comments
 (0)