deno/cli/tools/bundle/transform.rs
Nathan Whitaker 19d08d12d3
fix(bundle): transform import.meta.main (#29830)
For entrypoints, it remains untouched. For non-entrypoints it becomes
`false`.
2025-06-23 20:51:24 +00:00

44 lines
1.2 KiB
Rust

// Copyright 2018-2025 the Deno authors. MIT license.
use deno_ast::swc;
use deno_ast::swc::ast::Bool;
use deno_ast::swc::ecma_visit::VisitMut;
use deno_ast::swc::ecma_visit::VisitMutWith;
pub struct BundleImportMetaMainTransform {
is_entrypoint: bool,
}
impl BundleImportMetaMainTransform {
pub fn new(is_entrypoint: bool) -> Self {
Self { is_entrypoint }
}
}
impl VisitMut for BundleImportMetaMainTransform {
fn visit_mut_expr(&mut self, node: &mut swc::ast::Expr) {
// if entrypoint to bundle:
// import.meta.main => import.meta.main
// else:
// import.meta.main => false
if let swc::ast::Expr::Member(member) = node {
if let swc::ast::Expr::MetaProp(meta_prop) = &mut *member.obj {
if meta_prop.kind == swc::ast::MetaPropKind::ImportMeta
&& member.prop.is_ident_with("main")
{
if self.is_entrypoint {
return;
} else {
let span = member.span;
*node = swc::ast::Expr::Lit(swc::ast::Lit::Bool(Bool {
span,
value: false,
}));
return;
}
}
}
}
node.visit_mut_children_with(self);
}
}