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};
6use turbopack_core::chunk::ChunkingContext;
7
8use crate::{
9 ast_path_trie::{AstPathId, AstPathTrie},
10 code_gen::{CodeGen, CodeGeneration},
11 create_visitor,
12};
13
14#[derive(PartialEq, Eq, ValueDebugFormat, NonLocalValue, Debug, Hash, Encode, Decode)]
15pub struct IdentReplacement {
16 value: RcStr,
17 path: AstPathId,
18}
19
20impl IdentReplacement {
21 pub fn new(value: RcStr, path: AstPathId) -> Self {
22 IdentReplacement { value, path }
23 }
24
25 pub async fn code_generation(
26 &self,
27 trie: &AstPathTrie,
28 _chunking_context: Vc<Box<dyn ChunkingContext>>,
29 ) -> Result<CodeGeneration> {
30 let value = self.value.clone();
31
32 let visitor = create_visitor!(trie, 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}