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