|
| 1 | +use anyhow::Result; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use swc_core::{ |
| 4 | + common::DUMMY_SP, |
| 5 | + ecma::ast::{Expr, Ident, KeyValueProp, ObjectLit, PropName, PropOrSpread}, |
| 6 | + quote, |
| 7 | +}; |
| 8 | +use turbo_rcstr::rcstr; |
| 9 | +use turbo_tasks::{NonLocalValue, ResolvedVc, Vc, debug::ValueDebugFormat, trace::TraceRawVcs}; |
| 10 | +use turbopack_core::chunk::ChunkingContext; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + chunk::{EcmascriptChunkPlaceable, EcmascriptExports}, |
| 14 | + code_gen::{CodeGen, CodeGeneration}, |
| 15 | + create_visitor, magic_identifier, |
| 16 | + references::AstPath, |
| 17 | +}; |
| 18 | + |
| 19 | +/// Responsible for initializing the `ExportsInfoBinding` object binding, so that it may be |
| 20 | +/// referenced in the the file. |
| 21 | +/// |
| 22 | +/// There can be many references, and they appear at any nesting in the file. But we must only |
| 23 | +/// initialize the binding a single time. |
| 24 | +/// |
| 25 | +/// This singleton behavior must be enforced by the caller! |
| 26 | +#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] |
| 27 | +pub struct ExportsInfoBinding {} |
| 28 | + |
| 29 | +impl ExportsInfoBinding { |
| 30 | + #[allow(clippy::new_without_default)] |
| 31 | + pub fn new() -> Self { |
| 32 | + ExportsInfoBinding {} |
| 33 | + } |
| 34 | + |
| 35 | + pub async fn code_generation( |
| 36 | + &self, |
| 37 | + chunking_context: Vc<Box<dyn ChunkingContext>>, |
| 38 | + module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, |
| 39 | + exports: ResolvedVc<EcmascriptExports>, |
| 40 | + ) -> Result<CodeGeneration> { |
| 41 | + let export_usage_info = chunking_context |
| 42 | + .module_export_usage(*ResolvedVc::upcast(module)) |
| 43 | + .await?; |
| 44 | + let export_usage_info = export_usage_info.export_usage.await?; |
| 45 | + |
| 46 | + let props = if let EcmascriptExports::EsmExports(exports) = &*exports.await? { |
| 47 | + exports |
| 48 | + .await? |
| 49 | + .exports |
| 50 | + .keys() |
| 51 | + .map(|e| { |
| 52 | + let used: Expr = export_usage_info.is_export_used(e).into(); |
| 53 | + PropOrSpread::Prop(Box::new(swc_core::ecma::ast::Prop::KeyValue( |
| 54 | + KeyValueProp { |
| 55 | + key: PropName::Str(e.as_str().into()), |
| 56 | + value: quote!("{ used: $v }" as Box<Expr>, v: Expr = used), |
| 57 | + }, |
| 58 | + ))) |
| 59 | + }) |
| 60 | + .collect() |
| 61 | + } else { |
| 62 | + vec![] |
| 63 | + }; |
| 64 | + |
| 65 | + let data = Expr::Object(ObjectLit { |
| 66 | + props, |
| 67 | + span: DUMMY_SP, |
| 68 | + }); |
| 69 | + |
| 70 | + Ok(CodeGeneration::hoisted_stmt( |
| 71 | + rcstr!("__webpack_exports_info__"), |
| 72 | + quote!( |
| 73 | + "const $name = $data;" as Stmt, |
| 74 | + name = exports_ident(), |
| 75 | + data: Expr = data |
| 76 | + ), |
| 77 | + )) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl From<ExportsInfoBinding> for CodeGen { |
| 82 | + fn from(val: ExportsInfoBinding) -> Self { |
| 83 | + CodeGen::ExportsInfoBinding(val) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Handles rewriting `__webpack_exports_info__` references into the injected binding created by |
| 88 | +/// ExportsInfoBinding. |
| 89 | +/// |
| 90 | +/// There can be many references, and they appear at any nesting in the file. But all references |
| 91 | +/// refer to the same mutable object. |
| 92 | +#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] |
| 93 | +pub struct ExportsInfoRef { |
| 94 | + ast_path: AstPath, |
| 95 | +} |
| 96 | + |
| 97 | +impl ExportsInfoRef { |
| 98 | + pub fn new(ast_path: AstPath) -> Self { |
| 99 | + ExportsInfoRef { ast_path } |
| 100 | + } |
| 101 | + |
| 102 | + pub async fn code_generation( |
| 103 | + &self, |
| 104 | + _chunking_context: Vc<Box<dyn ChunkingContext>>, |
| 105 | + ) -> Result<CodeGeneration> { |
| 106 | + let visitor = create_visitor!(self.ast_path, visit_mut_expr, |expr: &mut Expr| { |
| 107 | + *expr = Expr::Ident(exports_ident()); |
| 108 | + }); |
| 109 | + |
| 110 | + Ok(CodeGeneration::visitors(vec![visitor])) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl From<ExportsInfoRef> for CodeGen { |
| 115 | + fn from(val: ExportsInfoRef) -> Self { |
| 116 | + CodeGen::ExportsInfoRef(val) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +fn exports_ident() -> Ident { |
| 121 | + Ident::new( |
| 122 | + magic_identifier::mangle("__webpack_exports_info__").into(), |
| 123 | + DUMMY_SP, |
| 124 | + Default::default(), |
| 125 | + ) |
| 126 | +} |
0 commit comments