Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace typeof before DCE #7788

Merged
merged 2 commits into from Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,4 +24,22 @@ if (process.env.NODE_ENV !== 'test') {
require('./true-alternate');
}

if (typeof require === "function") {
require('./true-consequent');
} else {
require('./false-alternate');
}

if (typeof exports === "object") {
require('./true-consequent');
} else {
require('./false-alternate');
}

if (typeof module === "object") {
require('./true-consequent');
} else {
require('./false-alternate');
}

module.exports = 2;
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)
}
}