|
| 1 | +use swc_ecma_ast::*; |
| 2 | +use swc_ecma_hooks::VisitMutHook; |
| 3 | +use swc_ecma_utils::private_ident; |
| 4 | + |
| 5 | +use crate::{utils::normalize_module_export_name, TraverseCtx}; |
| 6 | + |
| 7 | +pub fn hook() -> impl VisitMutHook<TraverseCtx> { |
| 8 | + ExportNamespaceFromPass |
| 9 | +} |
| 10 | + |
| 11 | +struct ExportNamespaceFromPass; |
| 12 | + |
| 13 | +impl ExportNamespaceFromPass { |
| 14 | + fn transform_export_namespace_from(&mut self, items: &mut Vec<ModuleItem>) { |
| 15 | + let count = items |
| 16 | + .iter() |
| 17 | + .filter(|m| { |
| 18 | + matches!(m, ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport { |
| 19 | + specifiers, |
| 20 | + src: Some(..), |
| 21 | + type_only: false, |
| 22 | + .. |
| 23 | + })) if specifiers.iter().any(|s| s.is_namespace())) |
| 24 | + }) |
| 25 | + .count(); |
| 26 | + |
| 27 | + if count == 0 { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + let mut stmts = Vec::<ModuleItem>::with_capacity(items.len() + count); |
| 32 | + |
| 33 | + for item in items.drain(..) { |
| 34 | + match item { |
| 35 | + ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport { |
| 36 | + span, |
| 37 | + specifiers, |
| 38 | + src: Some(src), |
| 39 | + type_only: false, |
| 40 | + with, |
| 41 | + })) if specifiers.iter().any(|s| s.is_namespace()) => { |
| 42 | + let mut origin_specifiers = Vec::new(); |
| 43 | + |
| 44 | + let mut import_specifiers = Vec::new(); |
| 45 | + let mut export_specifiers = Vec::new(); |
| 46 | + |
| 47 | + for s in specifiers.into_iter() { |
| 48 | + match s { |
| 49 | + ExportSpecifier::Namespace(ExportNamespaceSpecifier { span, name }) => { |
| 50 | + let local_bridge = private_ident!(format!( |
| 51 | + "_{}", |
| 52 | + normalize_module_export_name(&name) |
| 53 | + )); |
| 54 | + |
| 55 | + import_specifiers.push(ImportSpecifier::Namespace( |
| 56 | + ImportStarAsSpecifier { |
| 57 | + span, |
| 58 | + local: local_bridge.clone(), |
| 59 | + }, |
| 60 | + )); |
| 61 | + export_specifiers.push(ExportSpecifier::Named( |
| 62 | + ExportNamedSpecifier { |
| 63 | + span, |
| 64 | + orig: local_bridge.into(), |
| 65 | + exported: Some(name), |
| 66 | + is_type_only: false, |
| 67 | + }, |
| 68 | + )) |
| 69 | + } |
| 70 | + ExportSpecifier::Default(..) | ExportSpecifier::Named(..) => { |
| 71 | + origin_specifiers.push(s); |
| 72 | + } |
| 73 | + #[cfg(swc_ast_unknown)] |
| 74 | + _ => panic!("unable to access unknown nodes"), |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + stmts.push( |
| 79 | + ImportDecl { |
| 80 | + span, |
| 81 | + specifiers: import_specifiers, |
| 82 | + src: src.clone(), |
| 83 | + type_only: false, |
| 84 | + with: with.clone(), |
| 85 | + phase: Default::default(), |
| 86 | + } |
| 87 | + .into(), |
| 88 | + ); |
| 89 | + |
| 90 | + stmts.push( |
| 91 | + NamedExport { |
| 92 | + span, |
| 93 | + specifiers: export_specifiers, |
| 94 | + src: None, |
| 95 | + type_only: false, |
| 96 | + with: None, |
| 97 | + } |
| 98 | + .into(), |
| 99 | + ); |
| 100 | + |
| 101 | + if !origin_specifiers.is_empty() { |
| 102 | + stmts.push( |
| 103 | + NamedExport { |
| 104 | + span, |
| 105 | + specifiers: origin_specifiers, |
| 106 | + src: Some(src), |
| 107 | + type_only: false, |
| 108 | + with, |
| 109 | + } |
| 110 | + .into(), |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + _ => { |
| 115 | + stmts.push(item); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + *items = stmts; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl VisitMutHook<TraverseCtx> for ExportNamespaceFromPass { |
| 125 | + fn exit_program(&mut self, node: &mut Program, _: &mut TraverseCtx) { |
| 126 | + let Program::Module(module) = node else { |
| 127 | + return; |
| 128 | + }; |
| 129 | + |
| 130 | + self.transform_export_namespace_from(&mut module.body); |
| 131 | + } |
| 132 | +} |
0 commit comments