use rspack_core::{
Compilation, RuntimeGlobals, RuntimeModule, RuntimeModuleGenerateContext, RuntimeTemplate,
impl_runtime_module,
};
#[impl_runtime_module]
#[derive(Debug)]
pub struct CreateScriptUrlRuntimeModule {}
impl CreateScriptUrlRuntimeModule {
pub fn new(runtime_template: &RuntimeTemplate) -> Self {
Self::with_default(runtime_template)
}
}
#[async_trait::async_trait]
impl RuntimeModule for CreateScriptUrlRuntimeModule {
fn template(&self) -> Vec<(String, String)> {
vec![(
self.id.to_string(),
include_str!("runtime/create_script_url.ejs").to_string(),
)]
}
async fn generate(
&self,
context: &RuntimeModuleGenerateContext<'_>,
) -> rspack_error::Result<String> {
let compilation = context.compilation;
let source = context.runtime_template.render(
&self.id,
Some(serde_json::json!({
"_trusted_types": compilation.options.output.trusted_types.is_some(),
})),
)?;
Ok(source)
}
fn additional_runtime_requirements(&self, compilation: &Compilation) -> RuntimeGlobals {
if compilation.options.output.trusted_types.is_some() {
RuntimeGlobals::GET_TRUSTED_TYPES_POLICY
} else {
RuntimeGlobals::default()
}
}
}