turbopack_ecmascript/references/
ident.rs1use anyhow::Result;
2use bincode::{Decode, Encode};
3use swc_core::{ecma::ast::Expr, quote};
4use turbo_rcstr::RcStr;
5use turbo_tasks::{NonLocalValue, Vc, debug::ValueDebugFormat, trace::TraceRawVcs};
6use turbopack_core::chunk::ChunkingContext;
7
8use crate::{
9 code_gen::{CodeGen, CodeGeneration},
10 create_visitor,
11 references::AstPath,
12};
13
14#[derive(
15 PartialEq, Eq, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash, Encode, Decode,
16)]
17pub struct IdentReplacement {
18 value: RcStr,
19 path: AstPath,
20}
21
22impl IdentReplacement {
23 pub fn new(value: RcStr, path: AstPath) -> Self {
24 IdentReplacement { value, path }
25 }
26
27 pub async fn code_generation(
28 &self,
29 _chunking_context: Vc<Box<dyn ChunkingContext>>,
30 ) -> Result<CodeGeneration> {
31 let value = self.value.clone();
32
33 let visitor = create_visitor!(self.path, visit_mut_expr, |expr: &mut Expr| {
34 let id = Expr::Ident((&*value).into());
35 *expr = quote!("(\"TURBOPACK ident replacement\", $e)" as Expr, e: Expr = id);
36 });
37
38 Ok(CodeGeneration::visitors(vec![visitor]))
39 }
40}
41
42impl From<IdentReplacement> for CodeGen {
43 fn from(val: IdentReplacement) -> Self {
44 CodeGen::IdentReplacement(val)
45 }
46}