rspack_core/dependency/
const_dependency.rs1use rspack_cacheable::{cacheable, cacheable_dyn, with::AsRefStr};
2use rspack_util::ext::DynHash;
3
4use super::DependencyRange;
5use crate::{
6 Compilation, DependencyCodeGeneration, DependencyTemplate, DependencyTemplateType, RuntimeSpec,
7 TemplateContext, TemplateReplaceSource,
8};
9
10#[cacheable]
11#[derive(Debug, Clone)]
12pub struct ConstDependency {
13 pub range: DependencyRange,
14 #[cacheable(with=AsRefStr)]
15 pub content: Box<str>,
16}
17
18impl ConstDependency {
19 pub fn new(range: DependencyRange, content: Box<str>) -> Self {
20 Self { range, content }
21 }
22}
23
24#[cacheable_dyn]
25impl DependencyCodeGeneration for ConstDependency {
26 fn dependency_template(&self) -> Option<DependencyTemplateType> {
27 Some(ConstDependencyTemplate::template_type())
28 }
29
30 fn update_hash(
31 &self,
32 hasher: &mut dyn std::hash::Hasher,
33 _compilation: &Compilation,
34 _runtime: Option<&RuntimeSpec>,
35 ) {
36 self.range.dyn_hash(hasher);
37 self.content.dyn_hash(hasher);
38 }
39}
40
41#[cacheable]
42#[derive(Debug, Clone, Default)]
43pub struct ConstDependencyTemplate;
44
45impl ConstDependencyTemplate {
46 pub fn template_type() -> DependencyTemplateType {
47 DependencyTemplateType::Custom("ConstDependency")
48 }
49}
50
51impl DependencyTemplate for ConstDependencyTemplate {
52 fn render(
53 &self,
54 dep: &dyn DependencyCodeGeneration,
55 source: &mut TemplateReplaceSource,
56 _code_generatable_context: &mut TemplateContext,
57 ) {
58 let dep = dep
59 .as_any()
60 .downcast_ref::<ConstDependency>()
61 .expect("ConstDependencyTemplate should be used for ConstDependency");
62
63 source.replace(
64 dep.range.start,
65 dep.range.end,
66 dep.content.to_string(),
67 None,
68 );
69 }
70}