Skip to content

Commit 730b2bd

Browse files
authored
refactor: optimize runtime multiple line string literal (#12381)
refactor: optimize RuntimeTemplate API usage across plugins
1 parent 5041023 commit 730b2bd

File tree

15 files changed

+142
-37
lines changed

15 files changed

+142
-37
lines changed

crates/rspack_core/src/chunk_graph/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ impl ChunkGraph {
103103

104104
let table_body = requests.clone();
105105

106-
format!("\n<<table bgcolor=\"{bg_color}\">\n{table_header}\n{table_body}\n</table>>\n")
106+
format!(
107+
r#"
108+
<<table bgcolor="{bg_color}">
109+
{table_header}
110+
{table_body}
111+
</table>>
112+
"#
113+
)
107114
};
108115

109116
// push entry_point chunk group into visiting queue

crates/rspack_core/src/runtime_template.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,17 @@ impl RuntimeTemplate {
423423
.environment
424424
.supports_arrow_function()
425425
{
426-
format!("({args}) => {{\n{body}\n}}")
426+
format!(
427+
r#"({args}) => {{
428+
{body}
429+
}}"#
430+
)
427431
} else {
428-
format!("function({args}) {{\n{body}\n}}")
432+
format!(
433+
r#"function({args}) {{
434+
{body}
435+
}}"#
436+
)
429437
}
430438
}
431439

@@ -883,7 +891,9 @@ impl RuntimeTemplate {
883891
let header = if weak {
884892
runtime_requirements.insert(RuntimeGlobals::MODULE_FACTORIES);
885893
Some(format!(
886-
"if(!{}[{module_id_expr}]) {{\n {} \n}}",
894+
r#"if(!{}[{module_id_expr}]) {{
895+
{}
896+
}}"#,
887897
self.render_runtime_globals(&RuntimeGlobals::MODULE_FACTORIES),
888898
self.weak_error(request)
889899
))
@@ -928,7 +938,10 @@ impl RuntimeTemplate {
928938
) {
929939
if let Some(header) = header {
930940
appending = format!(
931-
".then(function() {{\n {header}\nreturn {}\n}})",
941+
r#".then(function() {{
942+
{header}
943+
return {}
944+
}})"#,
932945
self.module_raw(compilation, runtime_requirements, dep_id, request, weak)
933946
)
934947
} else {
@@ -941,7 +954,9 @@ impl RuntimeTemplate {
941954
}
942955
appending.push_str(
943956
format!(
944-
".then(function(m){{\n return {}(m, {fake_type}) \n}})",
957+
r#".then(function(m){{
958+
return {}(m, {fake_type})
959+
}})"#,
945960
self.render_runtime_globals(&RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT)
946961
)
947962
.as_str(),
@@ -953,7 +968,11 @@ impl RuntimeTemplate {
953968
"{}({module_id_expr}, {fake_type}))",
954969
self.render_runtime_globals(&RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT)
955970
);
956-
appending = format!(".then(function() {{\n {header} return {expr};\n}})");
971+
appending = format!(
972+
r#".then(function() {{
973+
{header} return {expr};
974+
}})"#
975+
);
957976
} else {
958977
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
959978
appending = format!(

crates/rspack_plugin_banner/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ fn wrap_comment(str: &str) -> String {
9494
let result = TRIALING_WHITESPACE.replace_all(&result, "\n");
9595
let result = result.trim_end();
9696

97-
format!("/*!\n * {result}\n */")
97+
format!(
98+
r#"/*!
99+
* {result}
100+
*/"#
101+
)
98102
}
99103

100104
#[plugin]

crates/rspack_plugin_devtool/src/eval_source_map_dev_tool_plugin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ async fn eval_source_map_devtool_plugin_render_module_content(
221221
.unwrap_or_else(|e| panic!("{}", e.to_string()));
222222
let base64 = base64::encode_to_string(&map_buffer);
223223
let footer = format!(
224-
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{base64}\n//# sourceURL=webpack-internal:///{module_id}\n"
224+
r#"
225+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,{base64}
226+
//# sourceURL=webpack-internal:///{module_id}
227+
"#
225228
);
226229
let module_content =
227230
simd_json::to_string(&format!("{{{source}{footer}\n}}")).expect("should convert to string");

crates/rspack_plugin_esm_library/src/dependency/dyn_import.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ fn then_expr(
8686
);
8787
appending.push_str(
8888
format!(
89-
".then(function(m){{\n return {}(m, {fake_type}) \n}})",
89+
r#".then(function(m){{
90+
return {}(m, {fake_type})
91+
}})"#,
9092
compilation
9193
.runtime_template
9294
.render_runtime_globals(&RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT)

crates/rspack_plugin_esm_library/src/link.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ impl EsmLibraryPlugin {
380380
namespace_object_sources.insert(
381381
*module_info_id,
382382
format!(
383-
"// NAMESPACE OBJECT: {}\nvar {} = {{}};\n{}({});\n{}\n",
383+
r#"// NAMESPACE OBJECT: {}
384+
var {} = {{}};
385+
{}({});
386+
{}
387+
"#,
384388
module_readable_identifier,
385389
name,
386390
compilation

crates/rspack_plugin_esm_library/src/render.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ impl EsmLibraryPlugin {
232232
ChunkGraph::get_tree_runtime_requirements(compilation, chunk_ukey);
233233
if tree_runtime_requirements.contains(RuntimeGlobals::MODULE_FACTORIES) {
234234
runtime_source.add(RawStringSource::from(format!(
235-
"\nvar {} = {{}};\n",
235+
r#"
236+
var {} = {{}};
237+
"#,
236238
compilation
237239
.runtime_template
238240
.render_runtime_variable(&RuntimeVariable::Modules)
@@ -681,7 +683,9 @@ impl EsmLibraryPlugin {
681683

682684
if use_require || module_cache {
683685
source.add(RawStringSource::from(format!(
684-
"// The module cache\nvar {} = {{}};\n",
686+
r#"// The module cache
687+
var {} = {{}};
688+
"#,
685689
compilation
686690
.runtime_template
687691
.render_runtime_variable(&RuntimeVariable::ModuleCache)
@@ -690,18 +694,26 @@ impl EsmLibraryPlugin {
690694

691695
if use_require {
692696
source.add(RawStringSource::from(format!(
693-
"// The require function\nfunction {}(moduleId) {{\n",
697+
r#"// The require function
698+
function {}(moduleId) {{
699+
"#,
694700
compilation
695701
.runtime_template
696702
.render_runtime_globals(&RuntimeGlobals::REQUIRE)
697703
)));
698704
source.add(RawStringSource::from(
699705
JsPlugin::render_require(chunk_ukey, compilation).join("\n"),
700706
));
701-
source.add(RawStringSource::from_static("\n}\n"));
707+
source.add(RawStringSource::from_static(
708+
r#"
709+
}
710+
"#,
711+
));
702712
} else if require_scope_used {
703713
source.add(RawStringSource::from(format!(
704-
"// The require scope\nvar {} = {{}};\n",
714+
r#"// The require scope
715+
var {} = {{}};
716+
"#,
705717
compilation
706718
.runtime_template
707719
.render_runtime_globals(&RuntimeGlobals::REQUIRE)
@@ -710,7 +722,9 @@ impl EsmLibraryPlugin {
710722

711723
if module_factories {
712724
source.add(RawStringSource::from(format!(
713-
"// expose the modules object ({modules})\n{module_factories} = {modules};\n",
725+
r#"// expose the modules object ({modules})
726+
{module_factories} = {modules};
727+
"#,
714728
module_factories = compilation
715729
.runtime_template
716730
.render_runtime_globals(&RuntimeGlobals::MODULE_FACTORIES),
@@ -722,7 +736,9 @@ impl EsmLibraryPlugin {
722736

723737
if runtime_requirements.contains(RuntimeGlobals::MODULE_CACHE) {
724738
source.add(RawStringSource::from(format!(
725-
"// expose the module cache\n{} = {};\n",
739+
r#"// expose the module cache
740+
{} = {};
741+
"#,
726742
compilation
727743
.runtime_template
728744
.render_runtime_globals(&RuntimeGlobals::MODULE_CACHE),
@@ -734,7 +750,9 @@ impl EsmLibraryPlugin {
734750

735751
if intercept_module_execution {
736752
source.add(RawStringSource::from(format!(
737-
"// expose the module execution interceptor\n{} = [];\n",
753+
r#"// expose the module execution interceptor
754+
{} = [];
755+
"#,
738756
compilation
739757
.runtime_template
740758
.render_runtime_globals(&RuntimeGlobals::INTERCEPT_MODULE_EXECUTION)

crates/rspack_plugin_extract_css/src/plugin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ despite it was not able to fulfill desired ordering with these modules:
394394
let req_str = readable_identifier.cow_replace("*/", "*_/");
395395
let req_str_star = "*".repeat(req_str.len());
396396
RawStringSource::from(format!(
397-
"/*!****{req_str_star}****!*\\\n !*** {req_str} ***!\n \\****{req_str_star}****/\n"
397+
r#"/*!****{req_str_star}****!*\
398+
!*** {req_str} ***!
399+
\****{req_str_star}****/
400+
"#
398401
))
399402
});
400403

crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,9 @@ impl ESMExportImportedSpecifierDependency {
812812
ctxt.init_fragments.push(
813813
NormalInitFragment::new(
814814
format!(
815-
"{content}\n/* reexport */ {}({}, __rspack_reexport);\n",
815+
r#"{content}
816+
/* reexport */ {}({}, __rspack_reexport);
817+
"#,
816818
compilation
817819
.runtime_template
818820
.render_runtime_globals(&RuntimeGlobals::DEFINE_PROPERTY_GETTERS),

crates/rspack_plugin_javascript/src/plugin/mod.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ var module = ({}[moduleId] = {{"#,
302302
if use_require || module_cache {
303303
header.push(
304304
format!(
305-
"// The module cache\nvar {} = {{}};\n",
305+
r#"// The module cache
306+
var {} = {{}};
307+
"#,
306308
runtime_template.render_runtime_variable(&RuntimeVariable::ModuleCache)
307309
)
308310
.into(),
@@ -314,23 +316,37 @@ var module = ({}[moduleId] = {{"#,
314316
// (see MakeDeferredNamespaceObjectRuntimeModule)
315317
// This requires all deferred imports to a module can get the module export object before the module
316318
// is evaluated.
317-
header.push("// The deferred module cache\nvar __rspack_deferred_exports = {};\n".into());
319+
header.push(
320+
r#"// The deferred module cache
321+
var __rspack_deferred_exports = {};
322+
"#
323+
.into(),
324+
);
318325
}
319326

320327
if use_require {
321328
header.push(
322329
format!(
323-
"// The require function\nfunction {}(moduleId) {{\n",
330+
r#"// The require function
331+
function {}(moduleId) {{
332+
"#,
324333
runtime_template.render_runtime_globals(&RuntimeGlobals::REQUIRE)
325334
)
326335
.into(),
327336
);
328337
header.extend(Self::render_require(chunk_ukey, compilation));
329-
header.push("\n}\n".into());
338+
header.push(
339+
r#"
340+
}
341+
"#
342+
.into(),
343+
);
330344
} else if require_scope_used {
331345
header.push(
332346
format!(
333-
"// The require scope\nvar {} = {{}};\n",
347+
r#"// The require scope
348+
var {} = {{}};
349+
"#,
334350
runtime_template.render_runtime_globals(&RuntimeGlobals::REQUIRE)
335351
)
336352
.into(),
@@ -341,7 +357,9 @@ var module = ({}[moduleId] = {{"#,
341357
{
342358
header.push(
343359
format!(
344-
"// expose the modules object ({modules})\n{module_factories} = {modules};\n",
360+
r#"// expose the modules object ({modules})
361+
{module_factories} = {modules};
362+
"#,
345363
modules = runtime_template.render_runtime_variable(&RuntimeVariable::Modules),
346364
module_factories =
347365
runtime_template.render_runtime_globals(&RuntimeGlobals::MODULE_FACTORIES)
@@ -353,7 +371,9 @@ var module = ({}[moduleId] = {{"#,
353371
if runtime_requirements.contains(RuntimeGlobals::MODULE_CACHE) {
354372
header.push(
355373
format!(
356-
"// expose the module cache\n{} = {};\n",
374+
r#"// expose the module cache
375+
{} = {};
376+
"#,
357377
runtime_template.render_runtime_globals(&RuntimeGlobals::MODULE_CACHE),
358378
runtime_template.render_runtime_variable(&RuntimeVariable::ModuleCache),
359379
)
@@ -364,7 +384,9 @@ var module = ({}[moduleId] = {{"#,
364384
if intercept_module_execution {
365385
header.push(
366386
format!(
367-
"// expose the module execution interceptor\n{} = [];\n",
387+
r#"// expose the module execution interceptor
388+
{} = [];
389+
"#,
368390
runtime_template.render_runtime_globals(&RuntimeGlobals::INTERCEPT_MODULE_EXECUTION)
369391
)
370392
.into(),
@@ -581,7 +603,9 @@ var module = ({}[moduleId] = {{"#,
581603
allow_inline_startup = false;
582604
header.push(
583605
format!(
584-
"// the startup function\n{} = {};\n",
606+
r#"// the startup function
607+
{} = {};
608+
"#,
585609
runtime_template.render_runtime_globals(&RuntimeGlobals::STARTUP),
586610
compilation.runtime_template.basic_function(
587611
"",
@@ -610,7 +634,9 @@ var module = ({}[moduleId] = {{"#,
610634
} else if runtime_requirements.contains(RuntimeGlobals::STARTUP) {
611635
header.push(
612636
format!(
613-
"// the startup function\n// It's empty as no entry modules are in this chunk\n{} = function(){{}};",
637+
r#"// the startup function
638+
// It's empty as no entry modules are in this chunk
639+
{} = function(){{}};"#,
614640
runtime_template.render_runtime_globals(&RuntimeGlobals::STARTUP)
615641
)
616642
.into(),
@@ -619,7 +645,9 @@ var module = ({}[moduleId] = {{"#,
619645
} else if runtime_requirements.contains(RuntimeGlobals::STARTUP) {
620646
header.push(
621647
format!(
622-
"// the startup function\n// It's empty as some runtime module handles the default behavior\n{} = function(){{}};",
648+
r#"// the startup function
649+
// It's empty as some runtime module handles the default behavior
650+
{} = function(){{}};"#,
623651
runtime_template.render_runtime_globals(&RuntimeGlobals::STARTUP)
624652
)
625653
.into(),

0 commit comments

Comments
 (0)