Skip to content

Commit 3875176

Browse files
authored
refactor: processing more string concatenation in runtime template part 1 (#12225)
* refactor: processing string concanation in runtime template * refactor: processing string concanation in runtime template * refactor: processing string concanation in runtime template
1 parent b531d85 commit 3875176

File tree

48 files changed

+470
-616
lines changed

Some content is hidden

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

48 files changed

+470
-616
lines changed

crates/rspack_plugin_runtime/src/runtime_module/async_module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ impl RuntimeModule for AsyncRuntimeModule {
1818
compilation.runtime_template.render(
1919
&self.id,
2020
Some(serde_json::json!({
21-
"queues": "__webpack_queues__",
22-
"error": "__webpack_error__",
23-
"done": "__webpack_done__",
24-
"defer": "__webpack_defer__",
25-
"module_cache": "__webpack_module_cache__",
21+
"_queues": "__webpack_queues__",
22+
"_error": "__webpack_error__",
23+
"_done": "__webpack_done__",
24+
"_defer": "__webpack_defer__",
25+
"_module_cache": "__webpack_module_cache__",
2626
})),
2727
)
2828
}

crates/rspack_plugin_runtime/src/runtime_module/auto_public_path.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use rspack_collections::Identifier;
22
use rspack_core::{
3-
ChunkUkey, Compilation, OutputOptions, PathData, RuntimeGlobals, RuntimeModule,
4-
RuntimeModuleStage, RuntimeTemplate, SourceType, get_js_chunk_filename_template, get_undo_path,
5-
impl_runtime_module,
3+
ChunkUkey, Compilation, OutputOptions, PathData, RuntimeModule, RuntimeModuleStage,
4+
RuntimeTemplate, SourceType, get_js_chunk_filename_template, get_undo_path, impl_runtime_module,
65
};
76

87
#[impl_runtime_module]
@@ -85,22 +84,14 @@ fn auto_public_path_template(
8584
) -> rspack_error::Result<String> {
8685
let output_path = output.path.as_str().to_string();
8786
let undo_path = get_undo_path(filename, output_path, false);
88-
let assign = if undo_path.is_empty() {
89-
format!("{} = scriptUrl", RuntimeGlobals::PUBLIC_PATH)
90-
} else {
91-
format!(
92-
"{} = scriptUrl + '{undo_path}'",
93-
RuntimeGlobals::PUBLIC_PATH
94-
)
95-
};
9687
let import_meta_name = output.import_meta_name.clone();
9788

9889
runtime_template.render(
9990
id,
10091
Some(serde_json::json!({
101-
"script_type": output.script_type,
102-
"import_meta_name": import_meta_name,
103-
"assign": assign
92+
"_script_type": output.script_type,
93+
"_import_meta_name": import_meta_name,
94+
"_undo_path": undo_path
10495
})),
10596
)
10697
}

crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_preload_function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ impl RuntimeModule for ChunkPrefetchPreloadFunctionRuntimeModule {
4242
let source = compilation.runtime_template.render(
4343
&self.id,
4444
Some(serde_json::json!({
45-
"RUNTIME_HANDLERS": &self.runtime_handlers.to_string(),
46-
"RUNTIME_FUNCTION": &self.runtime_function.to_string(),
45+
"_runtime_handlers": &self.runtime_handlers.to_string(),
46+
"_runtime_function": &self.runtime_function.to_string(),
4747
})),
4848
)?;
4949

crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_startup.rs

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use itertools::Itertools;
22
use rspack_collections::Identifier;
3-
use rspack_core::{
4-
ChunkUkey, Compilation, RuntimeGlobals, RuntimeModule, RuntimeModuleStage, impl_runtime_module,
5-
};
3+
use rspack_core::{ChunkUkey, Compilation, RuntimeModule, RuntimeModuleStage, impl_runtime_module};
64

75
#[impl_runtime_module]
86
#[derive(Debug)]
@@ -43,60 +41,45 @@ impl RuntimeModule for ChunkPrefetchStartupRuntimeModule {
4341
let chunk_ukey = self.chunk.expect("chunk do not attached");
4442

4543
let source = self
46-
.startup_chunks
44+
.startup_chunks
45+
.iter()
46+
.map(|(group_chunks, child_chunks)| {
47+
let group_chunk_ids = group_chunks
4748
.iter()
48-
.map(|(group_chunks, child_chunks)| {
49-
let group_chunk_ids = group_chunks
50-
.iter()
51-
.filter_map(|c| {
52-
if c.to_owned().eq(&chunk_ukey) {
53-
compilation
54-
.chunk_by_ukey
55-
.expect_get(c)
56-
.id(&compilation.chunk_ids_artifact)
57-
} else {
58-
None
59-
}
60-
})
61-
.collect_vec();
49+
.filter_map(|c| {
50+
if c.to_owned().eq(&chunk_ukey) {
51+
compilation
52+
.chunk_by_ukey
53+
.expect_get(c)
54+
.id(&compilation.chunk_ids_artifact)
55+
} else {
56+
None
57+
}
58+
})
59+
.collect_vec();
6260

63-
let child_chunk_ids = child_chunks
64-
.iter()
65-
.filter_map(|c| {
66-
compilation
67-
.chunk_by_ukey
68-
.expect_get(c)
69-
.id(&compilation.chunk_ids_artifact)
70-
})
71-
.collect_vec();
72-
73-
let body = match child_chunks.len() {
74-
x if x < 3 => child_chunk_ids
75-
.iter()
76-
.map(|id| {
77-
format!(
78-
"{}({});",
79-
RuntimeGlobals::PREFETCH_CHUNK,
80-
serde_json::to_string(&id).expect("invalid json tostring")
81-
)
82-
})
83-
.join("\n"),
84-
_ => {
85-
format!(
86-
"{}.map({})",
87-
serde_json::to_string(&child_chunk_ids).expect("invalid json tostring"),
88-
RuntimeGlobals::PREFETCH_CHUNK
89-
)
90-
}
91-
};
61+
let child_chunk_ids = child_chunks
62+
.iter()
63+
.filter_map(|c| {
64+
compilation
65+
.chunk_by_ukey
66+
.expect_get(c)
67+
.id(&compilation.chunk_ids_artifact)
68+
})
69+
.collect_vec();
9270

93-
let source = compilation.runtime_template.render(&self.id, Some(serde_json::json!({
94-
"GROUP_CHUNK_IDS": serde_json::to_string(&group_chunk_ids).expect("invalid json tostring"),
95-
"BODY": body,
96-
})))?;
71+
let source = compilation.runtime_template.render(
72+
&self.id,
73+
Some(serde_json::json!({
74+
"_chunk_ids": serde_json::to_string(&group_chunk_ids).expect("invalid json tostring"),
75+
"_child_chunk_ids": serde_json::to_string(&child_chunk_ids).expect("invalid json tostring"),
76+
})),
77+
)?;
9778

98-
Ok(source)
99-
}).collect::<rspack_error::Result<Vec<String>>>()?.join("\n");
79+
Ok(source)
80+
})
81+
.collect::<rspack_error::Result<Vec<String>>>()?
82+
.join("\n");
10083

10184
Ok(source)
10285
}

crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_trigger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RuntimeModule for ChunkPrefetchTriggerRuntimeModule {
4242
let source = compilation.runtime_template.render(
4343
&self.id,
4444
Some(serde_json::json!({
45-
"CHUNK_MAP": &self.chunk_map,
45+
"_chunk_map": &self.chunk_map,
4646
})),
4747
)?;
4848
Ok(source)

crates/rspack_plugin_runtime/src/runtime_module/chunk_preload_trigger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RuntimeModule for ChunkPreloadTriggerRuntimeModule {
4242
let source = compilation.runtime_template.render(
4343
&self.id,
4444
Some(serde_json::json!({
45-
"CHUNK_MAP": &self.chunk_map,
45+
"_chunk_map": &self.chunk_map,
4646
})),
4747
)?;
4848

crates/rspack_plugin_runtime/src/runtime_module/create_script.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rspack_collections::Identifier;
2-
use rspack_core::{Compilation, RuntimeGlobals, RuntimeModule, impl_runtime_module};
2+
use rspack_core::{Compilation, RuntimeModule, impl_runtime_module};
33

44
#[impl_runtime_module]
55
#[derive(Debug)]
@@ -30,14 +30,7 @@ impl RuntimeModule for CreateScriptRuntimeModule {
3030
let source = compilation.runtime_template.render(
3131
&self.id,
3232
Some(serde_json::json!({
33-
"_return": if compilation.options.output.trusted_types.is_some() {
34-
format!(
35-
"{}().createScript(script)",
36-
RuntimeGlobals::GET_TRUSTED_TYPES_POLICY
37-
)
38-
} else {
39-
"script".to_string()
40-
},
33+
"_trusted_types": compilation.options.output.trusted_types.is_some(),
4134
})),
4235
)?;
4336

crates/rspack_plugin_runtime/src/runtime_module/create_script_url.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rspack_collections::Identifier;
2-
use rspack_core::{Compilation, RuntimeGlobals, RuntimeModule, impl_runtime_module};
2+
use rspack_core::{Compilation, RuntimeModule, impl_runtime_module};
33

44
#[impl_runtime_module]
55
#[derive(Debug)]
@@ -30,14 +30,7 @@ impl RuntimeModule for CreateScriptUrlRuntimeModule {
3030
let source = compilation.runtime_template.render(
3131
&self.id,
3232
Some(serde_json::json!({
33-
"_return": if compilation.options.output.trusted_types.is_some() {
34-
format!(
35-
"{}().createScriptURL(url)",
36-
RuntimeGlobals::GET_TRUSTED_TYPES_POLICY
37-
)
38-
} else {
39-
"url".to_string()
40-
},
33+
"_trusted_types": compilation.options.output.trusted_types.is_some(),
4134
})),
4235
)?;
4336

crates/rspack_plugin_runtime/src/runtime_module/ensure_chunk.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ impl RuntimeModule for EnsureChunkRuntimeModule {
6262
compilation.runtime_template.render(
6363
&self.template_id(TemplateId::Raw),
6464
Some(serde_json::json!({
65-
"_args": format!("chunkId{}", fetch_priority),
6665
"_fetch_priority": fetch_priority,
6766
})),
6867
)?

crates/rspack_plugin_runtime/src/runtime_module/get_main_filename.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl RuntimeModule for GetMainFilenameRuntimeModule {
5656
.runtime(chunk.runtime().as_str()),
5757
)
5858
.await?;
59+
5960
Ok(format!(
6061
"{} = function () {{
6162
return \"{}\";

0 commit comments

Comments
 (0)