Skip to content

Commit

Permalink
Replace typeof before DCE
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Mar 3, 2022
1 parent a19ed25 commit 1514c67
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
25 changes: 0 additions & 25 deletions packages/transformers/js/core/src/hoist.rs
Expand Up @@ -693,31 +693,6 @@ impl<'a> Fold for Hoist<'a> {
}
}
}
Expr::Unary(ref unary) => {
// typeof require -> "function"
// typeof module -> "object"
if unary.op == UnaryOp::TypeOf {
if let Expr::Ident(ident) = &*unary.arg {
if ident.sym == js_word!("require") && !self.collect.decls.contains(&id!(ident)) {
return Expr::Lit(Lit::Str(Str {
kind: StrKind::Synthesized,
has_escape: false,
span: unary.span,
value: js_word!("function"),
}));
}

if ident.sym == js_word!("module") && !self.collect.decls.contains(&id!(ident)) {
return Expr::Lit(Lit::Str(Str {
kind: StrKind::Synthesized,
has_escape: false,
span: unary.span,
value: js_word!("object"),
}));
}
}
}
}
_ => {}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/transformers/js/core/src/lib.rs
Expand Up @@ -18,6 +18,7 @@ mod fs;
mod global_replacer;
mod hoist;
mod modules;
mod typeof_replacer;
mod utils;

use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -49,6 +50,7 @@ use fs::inline_fs;
use global_replacer::GlobalReplacer;
use hoist::{hoist, CollectResult, HoistResult};
use modules::esm2cjs;
use typeof_replacer::*;
use utils::{CodeHighlight, Diagnostic, DiagnosticSeverity, SourceLocation, SourceType};

use crate::hoist::Collect;
Expand Down Expand Up @@ -321,6 +323,10 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
let mut diagnostics = vec![];
let module = {
let mut passes = chain!(
Optional::new(
TypeofReplacer { decls: &decls },
config.source_type != SourceType::Script
),
// Inline process.env and process.browser
Optional::new(
EnvReplacer {
Expand Down
52 changes: 52 additions & 0 deletions packages/transformers/js/core/src/typeof_replacer.rs
@@ -0,0 +1,52 @@
use std::collections::HashSet;

use swc_atoms::JsWord;
use swc_ecmascript::ast::{Expr, Lit, Str, StrKind, UnaryOp};
use swc_ecmascript::visit::{Fold, FoldWith};

use crate::id;
use crate::utils::IdentId;

pub struct TypeofReplacer<'a> {
pub decls: &'a HashSet<IdentId>,
}

impl<'a> Fold for TypeofReplacer<'a> {
fn fold_expr(&mut self, node: Expr) -> Expr {
if let Expr::Unary(ref unary) = node {
// typeof require -> "function"
// typeof module -> "object"
if unary.op == UnaryOp::TypeOf {
if let Expr::Ident(ident) = &*unary.arg {
if ident.sym == js_word!("require") && !self.decls.contains(&id!(ident)) {
return Expr::Lit(Lit::Str(Str {
kind: StrKind::Synthesized,
has_escape: false,
span: unary.span,
value: js_word!("function"),
}));
}
let exports: JsWord = "exports".into();
if ident.sym == exports && !self.decls.contains(&id!(ident)) {
return Expr::Lit(Lit::Str(Str {
kind: StrKind::Synthesized,
has_escape: false,
span: unary.span,
value: js_word!("object"),
}));
}

if ident.sym == js_word!("module") && !self.decls.contains(&id!(ident)) {
return Expr::Lit(Lit::Str(Str {
kind: StrKind::Synthesized,
has_escape: false,
span: unary.span,
value: js_word!("object"),
}));
}
}
}
}
node.fold_children_with(self)
}
}

0 comments on commit 1514c67

Please sign in to comment.