Skip to content

Commit 1a7868e

Browse files
authored
refactor: render runtime module identifier by runtime template (#12360)
* refactor: render runtime module name by runtime template * refactor: render runtime module name by runtime template * refactor: render runtime module name by runtime template
1 parent c7f428b commit 1a7868e

File tree

76 files changed

+666
-268
lines changed

Some content is hidden

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

76 files changed

+666
-268
lines changed

crates/rspack_binding_api/src/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{any::TypeId, cell::RefCell, ptr::NonNull, sync::Arc};
33

44
use napi::{CallContext, JsObject, JsString, JsSymbol, NapiRaw};
55
use napi_derive::napi;
6-
use rspack_collections::{IdentifierMap, UkeyMap};
6+
use rspack_collections::{Identifier, IdentifierMap, UkeyMap};
77
use rspack_core::{
88
BindingCell, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, Compilation, CompilerId,
99
FactoryMeta, LibIdentOptions, Module as _, ModuleIdentifier, RuntimeModuleStage, SourceType,
@@ -765,7 +765,7 @@ pub struct JsAddingRuntimeModule {
765765
impl From<JsAddingRuntimeModule> for RuntimeModuleFromJs {
766766
fn from(value: JsAddingRuntimeModule) -> Self {
767767
Self {
768-
name: value.name,
768+
id: Identifier::from(value.name),
769769
full_hash: value.full_hash,
770770
dependent_hash: value.dependent_hash,
771771
isolate: value.isolate,

crates/rspack_binding_api/src/plugins/interceptor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
13471347
name: module
13481348
.name()
13491349
.as_str()
1350-
.cow_replace("webpack/runtime/", "")
1350+
.cow_replace(compilation.runtime_template.runtime_module_prefix(), "")
13511351
.into_owned(),
13521352
},
13531353
chunk: ChunkWrapper::new(*chunk_ukey, compilation),

crates/rspack_core/src/runtime_template.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ impl RuntimeTemplate {
231231
pub fn render_runtime_variable(&self, runtime_variable: &RuntimeVariable) -> String {
232232
runtime_variable_to_string(runtime_variable, &self.compiler_options)
233233
}
234+
235+
pub fn runtime_module_prefix(&self) -> &'static str {
236+
"webpack/runtime/"
237+
}
234238
}
235239

236240
fn to_string(val: &Operand, runtime_globals: &Map<String, Value>) -> String {

crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rspack_core::{
1313
CompilationContentHash, CompilationId, CompilationParams, CompilationRenderManifest,
1414
CompilationRuntimeRequirementInTree, CompilerCompilation, DependencyType, ManifestAssetType,
1515
Module, ModuleGraph, ModuleType, ParserAndGenerator, PathData, Plugin, PublicPath,
16-
RenderManifestEntry, RuntimeGlobals, SelfModuleFactory, SourceType,
16+
RenderManifestEntry, RuntimeGlobals, RuntimeModuleExt, SelfModuleFactory, SourceType,
1717
get_css_chunk_filename_template,
1818
rspack_sources::{
1919
BoxSource, CachedSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt,
@@ -332,7 +332,10 @@ async fn runtime_requirements_in_tree(
332332
runtime_requirements_mut.insert(RuntimeGlobals::HAS_OWN_PROPERTY);
333333
runtime_requirements_mut.insert(RuntimeGlobals::MODULE_FACTORIES_ADD_ONLY);
334334
runtime_requirements_mut.insert(RuntimeGlobals::MAKE_NAMESPACE_OBJECT);
335-
compilation.add_runtime_module(chunk_ukey, Box::<CssLoadingRuntimeModule>::default())?;
335+
compilation.add_runtime_module(
336+
chunk_ukey,
337+
CssLoadingRuntimeModule::new(&compilation.runtime_template).boxed(),
338+
)?;
336339
}
337340

338341
Ok(None)

crates/rspack_plugin_css/src/runtime/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{borrow::Cow, ptr::NonNull};
33
use rspack_collections::Identifier;
44
use rspack_core::{
55
BooleanMatcher, ChunkGroupOrderKey, ChunkUkey, Compilation, CrossOriginLoading, RuntimeGlobals,
6-
RuntimeModule, RuntimeModuleStage, compile_boolean_matcher, impl_runtime_module,
6+
RuntimeModule, RuntimeModuleStage, RuntimeTemplate, compile_boolean_matcher, impl_runtime_module,
77
};
88
use rspack_plugin_runtime::{
99
CreateLinkData, LinkPrefetchData, LinkPreloadData, RuntimeModuleChunkWrapper, RuntimePlugin,
@@ -18,13 +18,17 @@ pub struct CssLoadingRuntimeModule {
1818
chunk: Option<ChunkUkey>,
1919
}
2020

21-
impl Default for CssLoadingRuntimeModule {
22-
fn default() -> Self {
23-
Self::with_default(Identifier::from("webpack/runtime/css_loading"), None)
21+
impl CssLoadingRuntimeModule {
22+
pub fn new(runtime_template: &RuntimeTemplate) -> Self {
23+
Self::with_default(
24+
Identifier::from(format!(
25+
"{}css_loading",
26+
runtime_template.runtime_module_prefix()
27+
)),
28+
None,
29+
)
2430
}
25-
}
2631

27-
impl CssLoadingRuntimeModule {
2832
fn template_id(&self, id: TemplateId) -> String {
2933
let base_id = self.id.to_string();
3034

crates/rspack_plugin_esm_library/src/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl EsmLibraryPlugin {
254254
if !compilation
255255
.chunk_graph
256256
.get_chunk_runtime_modules_iterable(chunk_ukey)
257-
.any(|m| m.contains(EXPORT_REQUIRE_RUNTIME_MODULE_ID.as_str()))
257+
.any(|m| m.contains(EXPORT_REQUIRE_RUNTIME_MODULE_ID))
258258
&& tree_runtime_requirements.contains(RuntimeGlobals::REQUIRE)
259259
{
260260
export_specifiers.insert(Cow::Owned(

crates/rspack_plugin_extract_css/src/plugin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ async fn runtime_requirement_in_tree(
535535
compilation.add_runtime_module(
536536
chunk_ukey,
537537
Box::new(GetChunkFilenameRuntimeModule::new(
538+
&compilation.runtime_template,
538539
"css",
539540
"mini-css",
540541
SOURCE_TYPE[0],
@@ -565,6 +566,7 @@ async fn runtime_requirement_in_tree(
565566
compilation.add_runtime_module(
566567
chunk_ukey,
567568
Box::new(CssLoadingRuntimeModule::new(
569+
&compilation.runtime_template,
568570
*chunk_ukey,
569571
self.options.attributes.clone(),
570572
self.options.link_type.clone(),

crates/rspack_plugin_extract_css/src/runtime.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::ptr::NonNull;
22

33
use itertools::Itertools;
4-
use rspack_collections::UkeySet;
4+
use rspack_collections::{Identifier, UkeySet};
55
use rspack_core::{
66
BooleanMatcher, ChunkUkey, Compilation, RuntimeGlobals, RuntimeModule, RuntimeModuleStage,
7-
compile_boolean_matcher, impl_runtime_module,
7+
RuntimeTemplate, compile_boolean_matcher, impl_runtime_module,
88
};
99
use rspack_error::Result;
1010
use rspack_plugin_runtime::{
@@ -18,6 +18,7 @@ use crate::plugin::{InsertType, SOURCE_TYPE};
1818
#[impl_runtime_module]
1919
#[derive(Debug)]
2020
pub(crate) struct CssLoadingRuntimeModule {
21+
id: Identifier,
2122
chunk: ChunkUkey,
2223
attributes: FxHashMap<String, String>,
2324
link_type: Option<String>,
@@ -26,12 +27,22 @@ pub(crate) struct CssLoadingRuntimeModule {
2627

2728
impl CssLoadingRuntimeModule {
2829
pub(crate) fn new(
30+
runtime_template: &RuntimeTemplate,
2931
chunk: ChunkUkey,
3032
attributes: FxHashMap<String, String>,
3133
link_type: Option<String>,
3234
insert: InsertType,
3335
) -> Self {
34-
Self::with_default(chunk, attributes, link_type, insert)
36+
Self::with_default(
37+
Identifier::from(format!(
38+
"{}css loading",
39+
runtime_template.runtime_module_prefix()
40+
)),
41+
chunk,
42+
attributes,
43+
link_type,
44+
insert,
45+
)
3546
}
3647

3748
fn get_css_chunks(&self, compilation: &Compilation) -> UkeySet<ChunkUkey> {
@@ -68,7 +79,7 @@ enum TemplateId {
6879
#[async_trait::async_trait]
6980
impl RuntimeModule for CssLoadingRuntimeModule {
7081
fn name(&self) -> rspack_collections::Identifier {
71-
"webpack/runtime/css loading".into()
82+
self.id
7283
}
7384

7485
fn stage(&self) -> RuntimeModuleStage {

crates/rspack_plugin_hmr/src/hot_module_replacement.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rspack_collections::Identifier;
2-
use rspack_core::{Compilation, RuntimeModule, impl_runtime_module};
2+
use rspack_core::{Compilation, RuntimeModule, RuntimeTemplate, impl_runtime_module};
33
use rspack_util::test::is_hot_test;
44

55
#[impl_runtime_module]
@@ -8,9 +8,12 @@ pub struct HotModuleReplacementRuntimeModule {
88
id: Identifier,
99
}
1010

11-
impl Default for HotModuleReplacementRuntimeModule {
12-
fn default() -> Self {
13-
Self::with_default(Identifier::from("webpack/runtime/hot_module_replacement"))
11+
impl HotModuleReplacementRuntimeModule {
12+
pub fn new(runtime_template: &RuntimeTemplate) -> Self {
13+
Self::with_default(Identifier::from(format!(
14+
"{}hot_module_replacement",
15+
runtime_template.runtime_module_prefix()
16+
)))
1417
}
1518
}
1619

crates/rspack_plugin_hmr/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ async fn additional_tree_runtime_requirements(
459459
runtime_requirements.insert(RuntimeGlobals::MODULE_CACHE);
460460
compilation.add_runtime_module(
461461
chunk_ukey,
462-
HotModuleReplacementRuntimeModule::default().boxed(),
462+
HotModuleReplacementRuntimeModule::new(&compilation.runtime_template).boxed(),
463463
)?;
464464

465465
Ok(())

0 commit comments

Comments
 (0)