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 8a25954
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 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
5 changes: 4 additions & 1 deletion crates/rspack_plugin_javascript/src/visitors/strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ 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 {
}

fn visit_mut_stmt(&mut self, stmt: &mut Stmt) {
if let 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;
Expand Down

0 comments on commit 8a25954

Please sign in to comment.