use rspack_collections::Identifier;
use rspack_core::{
Compilation, RuntimeGlobals, RuntimeModule, RuntimeTemplate, impl_runtime_module,
};
#[impl_runtime_module]
#[derive(Debug)]
pub struct NonceRuntimeModule {
id: Identifier,
}
impl NonceRuntimeModule {
pub fn new(runtime_template: &RuntimeTemplate) -> Self {
Self::with_default(Identifier::from(format!(
"{}nonce",
runtime_template.runtime_module_prefix()
)))
}
}
#[async_trait::async_trait]
impl RuntimeModule for NonceRuntimeModule {
fn name(&self) -> Identifier {
self.id
}
async fn generate(&self, compilation: &Compilation) -> rspack_error::Result<String> {
Ok(format!(
"{} = undefined;",
compilation
.runtime_template
.render_runtime_globals(&RuntimeGlobals::SCRIPT_NONCE)
))
}
}