Skip to content

Commit 9e378b1

Browse files
feat: render runtime template with dojang (#9447)
* feat: render runtime template with dojang * chore: remove unused files * test: update snapshot
1 parent 912f2b3 commit 9e378b1

File tree

116 files changed

+357
-744
lines changed

Some content is hidden

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

116 files changed

+357
-744
lines changed

crates/rspack_plugin_runtime/src/runtime_module/esm_module_decorator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ impl RuntimeModule for ESMModuleDecoratorRuntimeModule {
2222
self.id
2323
}
2424

25-
fn generate(&self, _compilation: &Compilation) -> rspack_error::Result<BoxSource> {
26-
Ok(RawStringSource::from_static(include_str!("runtime/esm_module_decorator.js")).boxed())
25+
fn template(&self) -> Vec<(String, String)> {
26+
vec![(
27+
self.id.to_string(),
28+
include_str!("runtime/esm_module_decorator.ejs").to_string(),
29+
)]
30+
}
31+
32+
fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
33+
let source = compilation.runtime_template.render(&self.id, None)?;
34+
35+
Ok(RawStringSource::from(source).boxed())
2736
}
2837
}

crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule {
7878
self.id
7979
}
8080

81+
fn template(&self) -> Vec<(String, String)> {
82+
vec![(
83+
self.id.to_string(),
84+
include_str!("runtime/get_chunk_filename.ejs").to_string(),
85+
)]
86+
}
87+
8188
fn dependent_hash(&self) -> bool {
8289
true
8390
}
@@ -344,25 +351,17 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule {
344351
}
345352
}
346353
}
347-
Ok(
348-
RawStringSource::from(format!(
349-
"// This function allow to reference chunks
350-
{} = function (chunkId) {{
351-
// return url for filenames not based on template
352-
{}
353-
// return url for filenames based on template
354-
return {};
355-
}};
356-
",
357-
self.global,
358-
static_urls
359-
.iter()
360-
.map(|(filename, chunk_ids)| stringify_static_chunk_map(filename, chunk_ids))
361-
.join("\n"),
362-
dynamic_url.unwrap_or_else(|| format!("\"\" + chunkId + \".{}\"", self.content_type))
363-
))
364-
.boxed(),
365-
)
354+
355+
let source = compilation.runtime_template.render(&self.id, Some(serde_json::json!({
356+
"_global": self.global,
357+
"_static_urls": static_urls
358+
.iter()
359+
.map(|(filename, chunk_ids)| stringify_static_chunk_map(filename, chunk_ids))
360+
.join("\n"),
361+
"_dynamic_url": dynamic_url.unwrap_or_else(|| format!("\"\" + chunkId + \".{}\"", self.content_type))
362+
})))?;
363+
364+
Ok(RawStringSource::from(source).boxed())
366365
}
367366

368367
fn attach(&mut self, chunk: ChunkUkey) {

crates/rspack_plugin_runtime/src/runtime_module/get_chunk_update_filename.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ impl RuntimeModule for GetChunkUpdateFilenameRuntimeModule {
2727
fn name(&self) -> Identifier {
2828
self.id
2929
}
30+
31+
fn template(&self) -> Vec<(String, String)> {
32+
vec![(
33+
self.id.to_string(),
34+
include_str!("runtime/get_chunk_update_filename.ejs").to_string(),
35+
)]
36+
}
37+
3038
fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
3139
if let Some(chunk_ukey) = self.chunk {
3240
let chunk = compilation.chunk_by_ukey.expect_get(&chunk_ukey);
@@ -49,17 +57,15 @@ impl RuntimeModule for GetChunkUpdateFilenameRuntimeModule {
4957
.runtime(chunk.runtime().as_str()),
5058
)
5159
.always_ok();
52-
Ok(
53-
RawStringSource::from(format!(
54-
"{} = function (chunkId) {{
55-
return '{}';
56-
}};
57-
",
58-
RuntimeGlobals::GET_CHUNK_UPDATE_SCRIPT_FILENAME,
59-
filename
60-
))
61-
.boxed(),
62-
)
60+
61+
let source = compilation.runtime_template.render(
62+
&self.id,
63+
Some(serde_json::json!({
64+
"_filename": format!("'{}'", filename),
65+
})),
66+
)?;
67+
68+
Ok(RawStringSource::from(source).boxed())
6369
} else {
6470
unreachable!("should attach chunk for get_main_filename")
6571
}

crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use cow_utils::CowUtils;
21
use rspack_collections::Identifier;
32
use rspack_core::{
43
impl_runtime_module,
@@ -23,15 +22,22 @@ impl RuntimeModule for GetFullHashRuntimeModule {
2322
self.id
2423
}
2524

25+
fn template(&self) -> Vec<(String, String)> {
26+
vec![(
27+
self.id.to_string(),
28+
include_str!("runtime/get_full_hash.ejs").to_string(),
29+
)]
30+
}
31+
2632
fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
27-
Ok(
28-
RawStringSource::from(
29-
include_str!("runtime/get_full_hash.js")
30-
.cow_replace("$HASH$", compilation.get_hash().unwrap_or("XXXX"))
31-
.into_owned(),
32-
)
33-
.boxed(),
34-
)
33+
let source = compilation.runtime_template.render(
34+
&self.id,
35+
Some(serde_json::json!({
36+
"_hash": format!("\"{}\"", compilation.get_hash().unwrap_or("XXXX"))
37+
})),
38+
)?;
39+
40+
Ok(RawStringSource::from(source).boxed())
3541
}
3642

3743
fn full_hash(&self) -> bool {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%- ESM_MODULE_DECORATOR %> = <%- basicFunction("module") %> {
2+
module = Object.create(module);
3+
if (!module.children) module.children = [];
4+
Object.defineProperty(module, 'exports', {
5+
enumerable: true,
6+
set: <%- basicFunction("") %> {
7+
throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
8+
}
9+
});
10+
return module;
11+
};

crates/rspack_plugin_runtime/src/runtime_module/runtime/esm_module_decorator.js

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This function allow to reference chunks
2+
<%- _global %> = <%- basicFunction("chunkId") %> {
3+
// return url for filenames not based on template
4+
<%- _static_urls %>
5+
// return url for filenames based on template
6+
return <%- _dynamic_url %>
7+
}

crates/rspack_plugin_runtime/src/runtime_module/runtime/get_chunk_filename.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- GET_CHUNK_UPDATE_SCRIPT_FILENAME %> = <%- returningFunction(_filename, "chunkId") %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- GET_FULL_HASH %> = <%- returningFunction(_hash, "") %>

0 commit comments

Comments
 (0)