Skip to content

Commit 3a60a03

Browse files
feat: render runtime template with dojang (#9487)
1 parent a6324a0 commit 3a60a03

File tree

17 files changed

+71
-84
lines changed

17 files changed

+71
-84
lines changed

crates/rspack_plugin_runtime/src/runtime_module/public_path.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use cow_utils::CowUtils;
21
use rspack_collections::Identifier;
32
use rspack_core::{
43
has_hash_placeholder, impl_runtime_module,
54
rspack_sources::{BoxSource, RawStringSource, SourceExt},
6-
Compilation, Filename, PublicPath, RuntimeModule,
5+
Compilation, Filename, PublicPath, RuntimeGlobals, RuntimeModule,
76
};
87

98
#[impl_runtime_module]
@@ -26,14 +25,11 @@ impl RuntimeModule for PublicPathRuntimeModule {
2625

2726
fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
2827
Ok(
29-
RawStringSource::from(
30-
include_str!("runtime/public_path.js")
31-
.cow_replace(
32-
"__PUBLIC_PATH_PLACEHOLDER__",
33-
&PublicPath::render_filename(compilation, &self.public_path),
34-
)
35-
.into_owned(),
36-
)
28+
RawStringSource::from(format!(
29+
"{} = \"{}\";",
30+
RuntimeGlobals::PUBLIC_PATH.name(),
31+
&PublicPath::render_filename(compilation, &self.public_path)
32+
))
3733
.boxed(),
3834
)
3935
}

crates/rspack_plugin_runtime/src/runtime_module/relative_url.rs

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

25-
fn generate(&self, _: &Compilation) -> rspack_error::Result<BoxSource> {
26-
Ok(RawStringSource::from_static(include_str!("runtime/relative_url.js")).boxed())
25+
fn template(&self) -> Vec<(String, String)> {
26+
vec![(
27+
self.id.to_string(),
28+
include_str!("runtime/relative_url.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/runtime/define_property_getters.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/rspack_plugin_runtime/src/runtime_module/runtime/relative_url.js renamed to crates/rspack_plugin_runtime/src/runtime_module/runtime/relative_url.ejs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
__webpack_require__.U = function RelativeURL(url) {
1+
<%- RELATIVE_URL %> = function RelativeURL(url) {
22
var realUrl = new URL(url, "x:/");
33
var values = {};
44
for (var key in realUrl) values[key] = realUrl[key];
55
values.href = url;
66
values.pathname = url.replace(/[?#].*/, "");
77
values.origin = values.protocol = "";
8-
values.toString = values.toJSON = function () {
9-
return url;
10-
};
8+
values.toString = values.toJSON = <%- returningFunction("url", "") %>
119
for (var key in values) Object.defineProperty(this, key, { enumerable: true, configurable: true, value: values[key] });
1210
};
13-
__webpack_require__.U.prototype = URL.prototype;
11+
<%- RELATIVE_URL %>.prototype = URL.prototype;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var next = <%- STARTUP %>
2+
<%- STARTUP %> = <%- basicFunction("") %> {
3+
<%- _body %>
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%- STARTUP_ENTRYPOINT %> = <%- basicFunction("result, chunkIds, fn") %> {
2+
// arguments: chunkIds, moduleId are deprecated
3+
var moduleId = chunkIds;
4+
if (!fn) chunkIds = result, fn = <%- returningFunction("__webpack_require__(__webpack_require__.s = moduleId)", "") %>
5+
chunkIds.map(<%- ENSURE_CHUNK %>, <%- REQUIRE %>)
6+
var r = fn();
7+
return r === undefined ? result : r;
8+
}

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)