Skip to content

Commit

Permalink
fix(visitor): handle script strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Apr 25, 2023
1 parent 028621e commit 2d9b08a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
6 changes: 4 additions & 2 deletions crates/rspack_plugin_javascript/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub fn run_after_pass(
.expect("should have module graph module");
let need_tree_shaking = mgm.used;
let build_meta = mgm.build_meta.as_ref().expect("should have build meta");
let build_info = mgm.build_info.as_ref().expect("should have build info");

let DependencyCodeGenerationVisitors {
visitors,
root_visitors,
Expand Down Expand Up @@ -262,7 +264,7 @@ pub fn run_after_pass(
Some(ModuleConfig::CommonJs(CommonjsConfig {
ignore_dynamic: true,
// here will remove `use strict`
strict_mode: false,
strict_mode: build_info.strict,
import_interop: // if build_meta.strict_harmony_module {
// Some(ImportInterop::Node)
// } else
Expand All @@ -271,7 +273,7 @@ pub fn run_after_pass(
} else {
Some(ImportInterop::None)
},
allow_top_level_this: !build_meta.esm,
allow_top_level_this: !build_info.strict,
..Default::default()
})),
comments,
Expand Down
11 changes: 5 additions & 6 deletions crates/rspack_plugin_javascript/src/visitors/strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ impl<'a> VisitMut for StrictModeVisitor<'a> {
self.build_info.strict = true;
self.build_meta.esm = true;
}
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt { expr, .. })) = module_item {
if let Expr::Lit(Lit::Str(Str { ref value, .. })) = **expr {
if value == "use strict" {
self.build_info.strict = true;
}
}

fn visit_mut_stmt(&mut self, stmt: &mut Stmt) {
if let Stmt::Expr(ExprStmt { box expr, .. }) = stmt && let Expr::Lit(Lit::Str(Str { ref value, .. })) = expr && value == "use strict" {
self.build_info.strict = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::sync::Arc;
use once_cell::sync::Lazy;
use rspack_core::{ModuleType, ReactOptions};
use swc_core::common::{comments::SingleThreadedComments, Mark, SourceMap};
use swc_core::ecma::ast::{
CallExpr, Callee, Expr, Ident, Module, ModuleItem, Program, Script, Stmt,
};
use swc_core::ecma::ast::{CallExpr, Callee, Expr, Ident, ModuleItem, Program, Script, Stmt};
use swc_core::ecma::transforms::react::RefreshOptions;
use swc_core::ecma::transforms::react::{react as swc_react, Options};
use swc_core::ecma::visit::{noop_visit_type, Fold, Visit, VisitWith};
Expand Down

0 comments on commit 2d9b08a

Please sign in to comment.