Skip to content

Commit

Permalink
feat: render runtime template with dojang
Browse files Browse the repository at this point in the history
- render get_trusted_types_policy  with dojang
- render global  with dojang
- render has_own_property  with dojang
- render rspack_version with dojang
  • Loading branch information
GiveMe-A-Name committed Feb 25, 2025
1 parent fd867cf commit 48f9aab
Show file tree
Hide file tree
Showing 26 changed files with 114 additions and 126 deletions.
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 Down Expand Up @@ -29,6 +28,13 @@ impl RuntimeModule for GetTrustedTypesPolicyRuntimeModule {
self.id
}

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

fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
let trusted_types = compilation
.options
Expand All @@ -45,10 +51,6 @@ impl RuntimeModule for GetTrustedTypesPolicyRuntimeModule {
OnPolicyCreationFailure::Continue
);

let result = include_str!("runtime/get_trusted_types_policy.js").cow_replace(
"$policyName$",
&trusted_types.policy_name.clone().unwrap_or_default(),
);
let mut policy_content: Vec<String> = Vec::new();
if create_script {
policy_content.push(
Expand Down Expand Up @@ -88,22 +90,18 @@ impl RuntimeModule for GetTrustedTypesPolicyRuntimeModule {
} else {
"".to_string()
};
Ok(
RawStringSource::from(
result
.cow_replace("$policyContent$", policy_content.join(",\n").as_ref())
.cow_replace(
"$wrapPolicyCreationTryCatchStart$",
wrap_policy_creation_try_catch_start,
)
.cow_replace(
"$wrapPolicyCreationTryCatchEnd$",
&wrap_policy_creation_try_catch_end,
)
.into_owned(),
)
.boxed(),
)

let source = compilation.runtime_template.render(
&self.id,
Some(serde_json::json!({
"_policy_content": policy_content.join(",\n"),
"_wrap_policy_creation_try_catch_start": wrap_policy_creation_try_catch_start,
"_warp_policy_creation_try_catch_end": wrap_policy_creation_try_catch_end,
"_policy_name": &trusted_types.policy_name.clone().unwrap_or_default(),
})),
)?;

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

fn attach(&mut self, chunk: ChunkUkey) {
Expand Down
13 changes: 11 additions & 2 deletions crates/rspack_plugin_runtime/src/runtime_module/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ impl RuntimeModule for GlobalRuntimeModule {
self.id
}

fn generate(&self, _compilation: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(RawStringSource::from_static(include_str!("runtime/global.js")).boxed())
fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/global.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 @@ -22,7 +22,16 @@ impl RuntimeModule for HasOwnPropertyRuntimeModule {
self.id
}

fn generate(&self, _compilation: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(RawStringSource::from_static(include_str!("runtime/has_own_property.js")).boxed())
fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/has_own_property.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())
}
}
26 changes: 16 additions & 10 deletions crates/rspack_plugin_runtime/src/runtime_module/rspack_version.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 @@ -24,14 +23,21 @@ impl RuntimeModule for RspackVersionRuntimeModule {
self.id
}

fn generate(&self, _: &Compilation) -> rspack_error::Result<BoxSource> {
Ok(
RawStringSource::from(
include_str!("runtime/get_version.js")
.cow_replace("$VERSION$", &self.version)
.into_owned(),
)
.boxed(),
)
fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/get_version.ejs").to_string(),
)]
}

fn generate(&self, compilation: &Compilation) -> rspack_error::Result<BoxSource> {
let source = compilation.runtime_template.render(
&self.id,
Some(serde_json::json!({
"_version": format!("\"{}\"", &self.version),
})),
)?;

Ok(RawStringSource::from(source).boxed())
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var policy;
__webpack_require__.tt = function () {
<%- GET_TRUSTED_TYPES_POLICY %> = <%- basicFunction("") %> {
// Create Trusted Type policy if Trusted Types are available and the policy doesn't exist yet.
if (policy === undefined) {
policy = {
$policyContent$
<%- _policy_content %>
};
if (typeof trustedTypes !== "undefined" && trustedTypes.createPolicy) {
$wrapPolicyCreationTryCatchStart$
policy = trustedTypes.createPolicy("$policyName$", policy);
$wrapPolicyCreationTryCatchEnd$
<%- _wrap_policy_creation_try_catch_start %>
policy = trustedTypes.createPolicy("<%- _policy_name %>", policy);
<%- _warp_policy_creation_try_catch_end %>
}
}
return policy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- RSPACK_VERSION %> = <%- returningFunction(_version, "") %>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__webpack_require__.g = (function () {
<%- GLOBAL %> = (<%- basicFunction("") %> {
if (typeof globalThis === 'object') return globalThis;
try {
return this || new Function('return this')();
} catch (e) {
if (typeof window === 'object') return window;
}
})();
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- HAS_OWN_PROPERTY %> = <%- returningFunction("Object.prototype.hasOwnProperty.call(obj, prop)", "obj, prop") %>

This file was deleted.

15 changes: 3 additions & 12 deletions packages/rspack-test-tools/tests/__snapshots__/Config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down Expand Up @@ -629,10 +626,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down Expand Up @@ -711,10 +705,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down Expand Up @@ -629,10 +626,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down Expand Up @@ -711,10 +705,7 @@ __webpack_require__.d = function(exports, definition) {
})();
// webpack/runtime/has_own_property
(() => {
__webpack_require__.o = function (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
})();
/************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ runtime modules xx KiB
webpack/runtime/auto_public_path xx KiB {909} [code generated]
[no exports]
[used exports unknown]
webpack/runtime/global 223 bytes {909} [code generated]
webpack/runtime/global 216 bytes {909} [code generated]
[no exports]
[used exports unknown]
Rspack compiled successfully (ea70de85332c1294)
Rspack compiled successfully (27eddb11e03f28d2)
`;

exports[`new code splitting stats output new code splitting stats output/builtin-swc-loader-parse-error should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
Expand Down Expand Up @@ -113,7 +113,7 @@ Rspack x.x.x compiled successfully in X s
`;
exports[`new code splitting stats output new code splitting stats output/ignore-plugin should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
runtime modules 107 bytes 1 module
runtime modules 88 bytes 1 module
./index.js 103 bytes [built] [code generated]
<TEST_TOOLS_ROOT>/tests/statsOutputCases/ignore-plugin/locals|sync|/^\\.\\/.*$/ 160 bytes [built] [code generated]
./locals/en.js 30 bytes [built] [code generated]
Expand Down Expand Up @@ -199,7 +199,7 @@ DEBUG LOG from TestLoader|<TEST_TOOLS_ROOT>/tests/statsOutputCases/logging-loade
`;
exports[`new code splitting stats output new code splitting stats output/match-resource-data-url should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
runtime modules 677 bytes 3 modules
runtime modules 658 bytes 3 modules
./index.js 150 bytes [built] [code generated]
./a.js!=!data:javascript,var __looooooooo.. 98 bytes [built] [code generated]
`;
Expand Down Expand Up @@ -414,7 +414,7 @@ Rspack x.x.x compiled with 1 error in X s
exports[`new code splitting stats output new code splitting stats output/runtime-modules should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
./index.js 19 bytes [built] [code generated]
webpack/runtime/define_property_getters 290 bytes [code generated]
webpack/runtime/has_own_property 107 bytes [code generated]
webpack/runtime/has_own_property 88 bytes [code generated]
webpack/runtime/make_namespace_object 280 bytes [code generated]
`;
Expand Down Expand Up @@ -445,14 +445,14 @@ Rspack x.x.x compiled successfully in X s
exports[`new code splitting stats output new code splitting stats output/simple-export should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
asset bundle.js xx KiB [emitted] (name: main)
runtime modules 677 bytes 3 modules
runtime modules 658 bytes 3 modules
./index.js 26 bytes [built] [code generated]
Rspack x.x.x compiled successfully in X s
`;
exports[`new code splitting stats output new code splitting stats output/simple-module-source should print correct stats for: NewCodeSplittingStatsOutput 1`] = `
asset bundle.js xx KiB [emitted] (name: main)
runtime modules 661 bytes 3 modules
asset bundle.js 2 KiB [emitted] (name: main)
runtime modules 642 bytes 3 modules
orphan modules 1 bytes [orphan] 1 module
cacheable modules 82 bytes
./index.js 75 bytes [built] [code generated]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Object {
isOverSizeLimit: false,
name: main.js,
related: Array [],
size: 441,
size: 426,
type: asset,
},
],
Expand All @@ -330,7 +330,7 @@ Object {
main.js,
],
filteredModules: undefined,
hash: 2dd20186ade39fda,
hash: 4f28a602c91cd225,
id: 909,
idHints: Array [],
initial: true,
Expand Down Expand Up @@ -688,7 +688,7 @@ Object {
size: 192,
sizes: Object {
javascript: 192,
runtime: 677,
runtime: 658,
},
type: chunk,
},
Expand All @@ -698,10 +698,10 @@ Object {
assets: Array [
Object {
name: main.js,
size: 441,
size: 426,
},
],
assetsSize: 441,
assetsSize: 426,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand All @@ -718,7 +718,7 @@ Object {
errorsCount: 0,
filteredAssets: undefined,
filteredModules: undefined,
hash: 869653cdb9b09f07,
hash: 58ca6a70cbcadf27,
modules: Array [
Object {
assets: Array [],
Expand Down Expand Up @@ -1399,9 +1399,9 @@ Object {
preOrderIndex: undefined,
providedExports: Array [],
reasons: Array [],
size: 107,
size: 88,
sizes: Object {
runtime: 107,
runtime: 88,
},
type: module,
usedExports: null,
Expand Down Expand Up @@ -1455,10 +1455,10 @@ Object {
assets: Array [
Object {
name: main.js,
size: 441,
size: 426,
},
],
assetsSize: 441,
assetsSize: 426,
auxiliaryAssets: Array [],
auxiliaryAssetsSize: 0,
childAssets: Object {},
Expand Down
Loading

0 comments on commit 48f9aab

Please sign in to comment.