rspack_plugin_runtime 0.100.1

rspack runtime plugin
Documentation
use rspack_core::{
  Compilation, RuntimeGlobals, RuntimeModule, RuntimeModuleGenerateContext, RuntimeTemplate,
  impl_runtime_module,
};

#[impl_runtime_module]
#[derive(Debug)]
pub struct CreateScriptRuntimeModule {}

impl CreateScriptRuntimeModule {
  pub fn new(runtime_template: &RuntimeTemplate) -> Self {
    Self::with_default(runtime_template)
  }
}

#[async_trait::async_trait]
impl RuntimeModule for CreateScriptRuntimeModule {
  fn template(&self) -> Vec<(String, String)> {
    vec![(
      self.id.to_string(),
      include_str!("runtime/create_script.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()
    }
  }
}