rspack_plugin_runtime/
runtime_module_from_js.rs1use std::sync::Arc;
2
3use derive_more::Debug;
4use futures::future::BoxFuture;
5use rspack_cacheable::with::Unsupported;
6use rspack_collections::Identifier;
7use rspack_core::{
8 Compilation, RuntimeModule, RuntimeModuleStage, RuntimeTemplate, impl_runtime_module,
9};
10
11type GenerateFn = Arc<dyn Fn() -> BoxFuture<'static, rspack_error::Result<String>> + Send + Sync>;
12
13#[impl_runtime_module]
14#[derive(Debug)]
15pub struct RuntimeModuleFromJs {
16 pub id: Identifier,
17 #[debug(skip)]
18 #[cacheable(with=Unsupported)]
19 pub generator: GenerateFn,
20 pub full_hash: bool,
21 pub dependent_hash: bool,
22 pub isolate: bool,
23 pub stage: RuntimeModuleStage,
24}
25
26impl RuntimeModuleFromJs {
27 pub fn new(
28 runtime_template: &RuntimeTemplate,
29 name: &str,
30 generator: GenerateFn,
31 full_hash: bool,
32 dependent_hash: bool,
33 isolate: bool,
34 stage: RuntimeModuleStage,
35 ) -> Self {
36 Self::with_default(
37 Identifier::from(format!(
38 "{}{}",
39 runtime_template.runtime_module_prefix(),
40 name
41 )),
42 generator,
43 full_hash,
44 dependent_hash,
45 isolate,
46 stage,
47 )
48 }
49}
50
51#[async_trait::async_trait]
52impl RuntimeModule for RuntimeModuleFromJs {
53 fn name(&self) -> Identifier {
54 self.id
55 }
56
57 async fn generate(&self, _: &Compilation) -> rspack_error::Result<String> {
58 let res = (self.generator)().await?;
59 Ok(res)
60 }
61
62 fn full_hash(&self) -> bool {
63 self.full_hash
64 }
65
66 fn dependent_hash(&self) -> bool {
67 self.dependent_hash
68 }
69
70 fn should_isolate(&self) -> bool {
71 self.isolate
72 }
73
74 fn stage(&self) -> RuntimeModuleStage {
75 self.stage.clone()
76 }
77}