Skip to content

Commit

Permalink
feat: render runtime template with dojang (#9447)
Browse files Browse the repository at this point in the history
* feat: render runtime template with dojang

* chore: remove unused files

* test: update snapshot
  • Loading branch information
GiveMe-A-Name authored Feb 26, 2025
1 parent 912f2b3 commit 9e378b1
Show file tree
Hide file tree
Showing 116 changed files with 357 additions and 744 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ impl RuntimeModule for ESMModuleDecoratorRuntimeModule {
self.id
}

fn generate(&self, _compilation: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(RawStringSource::from_static(include_str!("runtime/esm_module_decorator.js")).boxed())
fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/esm_module_decorator.ejs").to_string(),
)]
}

fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
let source = compilation.runtime_template.render(&self.id, None)?;

Ok(RawStringSource::from(source).boxed())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule {
self.id
}

fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/get_chunk_filename.ejs").to_string(),
)]
}

fn dependent_hash(&self) -> bool {
true
}
Expand Down Expand Up @@ -344,25 +351,17 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule {
}
}
}
Ok(
RawStringSource::from(format!(
"// This function allow to reference chunks
{} = function (chunkId) {{
// return url for filenames not based on template
{}
// return url for filenames based on template
return {};
}};
",
self.global,
static_urls
.iter()
.map(|(filename, chunk_ids)| stringify_static_chunk_map(filename, chunk_ids))
.join("\n"),
dynamic_url.unwrap_or_else(|| format!("\"\" + chunkId + \".{}\"", self.content_type))
))
.boxed(),
)

let source = compilation.runtime_template.render(&self.id, Some(serde_json::json!({
"_global": self.global,
"_static_urls": static_urls
.iter()
.map(|(filename, chunk_ids)| stringify_static_chunk_map(filename, chunk_ids))
.join("\n"),
"_dynamic_url": dynamic_url.unwrap_or_else(|| format!("\"\" + chunkId + \".{}\"", self.content_type))
})))?;

Ok(RawStringSource::from(source).boxed())
}

fn attach(&mut self, chunk: ChunkUkey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ impl RuntimeModule for GetChunkUpdateFilenameRuntimeModule {
fn name(&self) -> Identifier {
self.id
}

fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/get_chunk_update_filename.ejs").to_string(),
)]
}

fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
if let Some(chunk_ukey) = self.chunk {
let chunk = compilation.chunk_by_ukey.expect_get(&chunk_ukey);
Expand All @@ -49,17 +57,15 @@ impl RuntimeModule for GetChunkUpdateFilenameRuntimeModule {
.runtime(chunk.runtime().as_str()),
)
.always_ok();
Ok(
RawStringSource::from(format!(
"{} = function (chunkId) {{
return '{}';
}};
",
RuntimeGlobals::GET_CHUNK_UPDATE_SCRIPT_FILENAME,
filename
))
.boxed(),
)

let source = compilation.runtime_template.render(
&self.id,
Some(serde_json::json!({
"_filename": format!("'{}'", filename),
})),
)?;

Ok(RawStringSource::from(source).boxed())
} else {
unreachable!("should attach chunk for get_main_filename")
}
Expand Down
24 changes: 15 additions & 9 deletions crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use cow_utils::CowUtils;
use rspack_collections::Identifier;
use rspack_core::{
impl_runtime_module,
Expand All @@ -23,15 +22,22 @@ impl RuntimeModule for GetFullHashRuntimeModule {
self.id
}

fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/get_full_hash.ejs").to_string(),
)]
}

fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(
RawStringSource::from(
include_str!("runtime/get_full_hash.js")
.cow_replace("$HASH$", compilation.get_hash().unwrap_or("XXXX"))
.into_owned(),
)
.boxed(),
)
let source = compilation.runtime_template.render(
&self.id,
Some(serde_json::json!({
"_hash": format!("\"{}\"", compilation.get_hash().unwrap_or("XXXX"))
})),
)?;

Ok(RawStringSource::from(source).boxed())
}

fn full_hash(&self) -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%- ESM_MODULE_DECORATOR %> = <%- basicFunction("module") %> {
module = Object.create(module);
if (!module.children) module.children = [];
Object.defineProperty(module, 'exports', {
enumerable: true,
set: <%- basicFunction("") %> {
throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
}
});
return module;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This function allow to reference chunks
<%- _global %> = <%- basicFunction("chunkId") %> {
// return url for filenames not based on template
<%- _static_urls %>
// return url for filenames based on template
return <%- _dynamic_url %>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- GET_CHUNK_UPDATE_SCRIPT_FILENAME %> = <%- returningFunction(_filename, "chunkId") %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- GET_FULL_HASH %> = <%- returningFunction(_hash, "") %>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```html title=output.html
<!doctype html><html><head><meta charset="UTF-8"><title>Rspack App</title><script crossorigin integrity="sha512-x+DXHTR9Gf+81RiZZ/+tJ6EeuaSnCkhVh3+31XahWhYsovvVWNBj5p4+59s55fwpu6SnEoFQyHVz1dTpln6Qlg==" src="/runtime.js"></script><script crossorigin integrity="sha512-ni0pw7g4IWa/Ru5Zg1h1mveKp+Dexgx8hE9eE4lmEtO3cvHT4jgQpnvMcxWjc4BtqtUfCNBuJhKkseCWrSr/tQ==" src="/index.js"></script><link crossorigin href="/index.css" integrity="sha512-vjhejkvTX8D8jzzC5Lc9ToCtcEDb0/NqtYmEHOid+ycq64hvW+JTG7Dw/ACFP6rApgiYwVMbEt6Zfb8j4EoZTA==" rel="stylesheet"></head><body></body></html>
<!doctype html><html><head><meta charset="UTF-8"><title>Rspack App</title><script crossorigin integrity="sha512-3wcZZCrQT5XQlyTBc3GLEmZCyPuS1J1oZ6UhzGs9T57rcSh6Fwgh+CBvWM+bpj/hVcKLG8Dm9JVZio0yxmj+ag==" src="/runtime.js"></script><script crossorigin integrity="sha512-ni0pw7g4IWa/Ru5Zg1h1mveKp+Dexgx8hE9eE4lmEtO3cvHT4jgQpnvMcxWjc4BtqtUfCNBuJhKkseCWrSr/tQ==" src="/index.js"></script><link crossorigin href="/index.css" integrity="sha512-vjhejkvTX8D8jzzC5Lc9ToCtcEDb0/NqtYmEHOid+ycq64hvW+JTG7Dw/ACFP6rApgiYwVMbEt6Zfb8j4EoZTA==" rel="stylesheet"></head><body></body></html>
```
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ module.exports = {
Array [
Object {
path: a.js,
size: 4745,
size: 4682,
},
Object {
path: b.js,
size: 4745,
size: 4682,
},
Object {
path: c_js.js,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Asset Files
- Bundle: bundle.js
- Manifest: main.LAST_HASH.hot-update.json, size: 28
- Update: main.LAST_HASH.hot-update.js, size: 681
- Update: main.LAST_HASH.hot-update.js, size: 662

## Manifest

Expand Down Expand Up @@ -47,10 +47,7 @@ __webpack_require__.d(__webpack_exports__, {
},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- Manifest: main.LAST_HASH.hot-update.json, size: 51
- Update: chunk2_js.LAST_HASH.hot-update.js, size: 288
- Update: chunk_js.LAST_HASH.hot-update.js, size: 287
- Update: main.LAST_HASH.hot-update.js, size: 201
- Update: main.LAST_HASH.hot-update.js, size: 182

## Manifest

Expand Down Expand Up @@ -92,10 +92,7 @@ var value = 2;
self["webpackHotUpdate"]('main', {},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- Manifest: main.LAST_HASH.hot-update.json, size: 51
- Update: chunk2_js.LAST_HASH.hot-update.js, size: 288
- Update: chunk_js.LAST_HASH.hot-update.js, size: 287
- Update: main.LAST_HASH.hot-update.js, size: 201
- Update: main.LAST_HASH.hot-update.js, size: 182

## Manifest

Expand Down Expand Up @@ -92,10 +92,7 @@ var value = 2;
self["webpackHotUpdate"]('main', {},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Asset Files
- Bundle: bundle.js
- Manifest: main.LAST_HASH.hot-update.json, size: 39
- Update: main.LAST_HASH.hot-update.js, size: 249
- Update: main.LAST_HASH.hot-update.js, size: 230

## Manifest

Expand Down Expand Up @@ -40,10 +40,7 @@ module.exports = 2;
},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Asset Files
- Bundle: bundle.js
- Manifest: main.LAST_HASH.hot-update.json, size: 28
- Update: main.LAST_HASH.hot-update.js, size: 511
- Update: main.LAST_HASH.hot-update.js, size: 492

## Manifest

Expand Down Expand Up @@ -47,10 +47,7 @@ module.exports = __webpack_require__.p + "98396dbfd5c74c34.png";
},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Asset Files
- Bundle: bundle.js
- Manifest: main.LAST_HASH.hot-update.json, size: 39
- Update: main.LAST_HASH.hot-update.js, size: 249
- Update: main.LAST_HASH.hot-update.js, size: 230

## Manifest

Expand Down Expand Up @@ -40,10 +40,7 @@ module.exports = 4;
},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Bundle: vendors-node_modules_vue_js.chunk.CURRENT_HASH.js
- Manifest: main.LAST_HASH.hot-update.json, size: 94
- Update: file_js.LAST_HASH.hot-update.js, size: 433
- Update: main.LAST_HASH.hot-update.js, size: 700
- Update: main.LAST_HASH.hot-update.js, size: 681

## Manifest

Expand Down Expand Up @@ -78,10 +78,7 @@ function test(count) {
},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Update: a.LAST_HASH.hot-update.js, size: 96
- Update: b.LAST_HASH.hot-update.js, size: 96
- Update: main.LAST_HASH.hot-update.js, size: 99
- Update: runtime.LAST_HASH.hot-update.js, size: 204
- Update: runtime.LAST_HASH.hot-update.js, size: 185

## Manifest

Expand Down Expand Up @@ -107,10 +107,7 @@ module.exports = 2;
self["webpackHotUpdate"]('runtime', {},function(__webpack_require__) {
// webpack/runtime/get_full_hash
(() => {
__webpack_require__.h = function () {
return "CURRENT_HASH";
};

__webpack_require__.h = () => ("CURRENT_HASH")
})();

}
Expand Down
Loading

2 comments on commit 9e378b1

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 9e378b1 Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ❌ failure
rsbuild ✅ success
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 9e378b1 Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-02-26 50bdb20) Current Change
10000_big_production-mode_disable-minimize + exec 36.8 s ± 476 ms 37.7 s ± 1.08 s +2.50 %
10000_development-mode + exec 1.7 s ± 20 ms 1.74 s ± 46 ms +2.21 %
10000_development-mode_hmr + exec 674 ms ± 19 ms 678 ms ± 26 ms +0.69 %
10000_production-mode + exec 2.21 s ± 66 ms 2.15 s ± 52 ms -2.98 %
10000_production-mode_persistent-cold + exec 2.34 s ± 55 ms 2.31 s ± 122 ms -1.19 %
10000_production-mode_persistent-hot + exec 1.62 s ± 60 ms 1.62 s ± 28 ms +0.17 %
arco-pro_development-mode + exec 1.71 s ± 122 ms 1.73 s ± 132 ms +0.74 %
arco-pro_development-mode_hmr + exec 375 ms ± 2.1 ms 376 ms ± 4.7 ms +0.32 %
arco-pro_production-mode + exec 3.55 s ± 173 ms 3.53 s ± 223 ms -0.59 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.64 s ± 178 ms 3.63 s ± 199 ms -0.28 %
arco-pro_production-mode_persistent-cold + exec 3.66 s ± 57 ms 3.56 s ± 158 ms -2.70 %
arco-pro_production-mode_persistent-hot + exec 2.24 s ± 125 ms 2.32 s ± 185 ms +3.48 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.61 s ± 181 ms 3.5 s ± 135 ms -3.02 %
large-dyn-imports_development-mode + exec 1.96 s ± 22 ms 1.95 s ± 14 ms -0.35 %
large-dyn-imports_production-mode + exec 2.02 s ± 29 ms 2.03 s ± 65 ms +0.34 %
threejs_development-mode_10x + exec 1.45 s ± 43 ms 1.46 s ± 21 ms +1.23 %
threejs_development-mode_10x_hmr + exec 786 ms ± 8.4 ms 816 ms ± 26 ms +3.84 %
threejs_production-mode_10x + exec 5.04 s ± 169 ms 5.15 s ± 374 ms +2.28 %
threejs_production-mode_10x_persistent-cold + exec 5.15 s ± 294 ms 5.15 s ± 167 ms -0.01 %
threejs_production-mode_10x_persistent-hot + exec 4.46 s ± 215 ms 4.46 s ± 97 ms -0.17 %
10000_big_production-mode_disable-minimize + rss memory 8667 MiB ± 41.1 MiB 8692 MiB ± 89.2 MiB +0.29 %
10000_development-mode + rss memory 655 MiB ± 19.2 MiB 673 MiB ± 11.5 MiB +2.86 %
10000_development-mode_hmr + rss memory 1201 MiB ± 64.3 MiB 1368 MiB ± 131 MiB +13.94 %
10000_production-mode + rss memory 617 MiB ± 6.87 MiB 649 MiB ± 18.3 MiB +5.07 %
10000_production-mode_persistent-cold + rss memory 732 MiB ± 6 MiB 746 MiB ± 15.2 MiB +1.89 %
10000_production-mode_persistent-hot + rss memory 699 MiB ± 31.9 MiB 740 MiB ± 37.3 MiB +5.87 %
arco-pro_development-mode + rss memory 571 MiB ± 31 MiB 587 MiB ± 19.4 MiB +2.80 %
arco-pro_development-mode_hmr + rss memory 650 MiB ± 51.6 MiB 677 MiB ± 51.9 MiB +4.22 %
arco-pro_production-mode + rss memory 709 MiB ± 41.3 MiB 722 MiB ± 8.92 MiB +1.85 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 725 MiB ± 15.4 MiB 723 MiB ± 28.4 MiB -0.17 %
arco-pro_production-mode_persistent-cold + rss memory 804 MiB ± 25.7 MiB 790 MiB ± 28.7 MiB -1.78 %
arco-pro_production-mode_persistent-hot + rss memory 655 MiB ± 23.2 MiB 654 MiB ± 36.3 MiB -0.13 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 731 MiB ± 23.4 MiB 720 MiB ± 28.9 MiB -1.59 %
large-dyn-imports_development-mode + rss memory 650 MiB ± 5.99 MiB 660 MiB ± 5.48 MiB +1.54 %
large-dyn-imports_production-mode + rss memory 529 MiB ± 6.89 MiB 540 MiB ± 5.98 MiB +1.99 %
threejs_development-mode_10x + rss memory 571 MiB ± 14.4 MiB 571 MiB ± 22.5 MiB +0.09 %
threejs_development-mode_10x_hmr + rss memory 1212 MiB ± 166 MiB 1186 MiB ± 216 MiB -2.13 %
threejs_production-mode_10x + rss memory 858 MiB ± 32.6 MiB 854 MiB ± 32.3 MiB -0.48 %
threejs_production-mode_10x_persistent-cold + rss memory 963 MiB ± 58.5 MiB 950 MiB ± 47.8 MiB -1.38 %
threejs_production-mode_10x_persistent-hot + rss memory 815 MiB ± 40.3 MiB 785 MiB ± 35.4 MiB -3.79 %

Please sign in to comment.