Skip to content

Commit

Permalink
fix(webpack/ast): Fix amd support (#2959)
Browse files Browse the repository at this point in the history
swc_webpack_ast:
 - `ast_reducer`: Preserve array literals in `define` calls.
  • Loading branch information
kdy1 authored Dec 3, 2021
1 parent 1eb62dc commit eda514b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_webpack_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2018"
license = "Apache-2.0"
name = "swc_webpack_ast"
repository = "https://github.com/swc-project/swc.git"
version = "0.3.1"
version = "0.3.2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
22 changes: 16 additions & 6 deletions crates/swc_webpack_ast/src/reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ struct ReduceAst {

can_remove_pat: bool,
preserve_fn: bool,
preserve_lit: bool,
is_string_lit_important: bool,

changed: bool,
}
Expand Down Expand Up @@ -600,11 +600,11 @@ impl VisitMut for ReduceAst {
// We should preserve the arguments if the callee is not invalid.
let old_preserver_fn = self.preserve_fn;
self.preserve_fn = !callee.is_fn_expr() && !callee.is_arrow();
let old_preserve_lit = self.preserve_lit;
self.preserve_lit |= is_string_lit_important;
let old_preserve_lit = self.is_string_lit_important;
self.is_string_lit_important |= is_string_lit_important;
e.visit_mut_children_with(self);

self.preserve_lit = old_preserve_lit;
self.is_string_lit_important = old_preserve_lit;
self.preserve_fn = old_preserver_fn;

return;
Expand All @@ -614,11 +614,21 @@ impl VisitMut for ReduceAst {
_ => {}
}

if self.preserve_lit && e.is_lit() {
if self.is_string_lit_important && (e.is_lit() || e.is_array()) {
return;
}

e.visit_mut_children_with(self);
match e {
Expr::Fn(..) | Expr::Arrow(..) => {
let old = self.is_string_lit_important;
self.is_string_lit_important = false;
e.visit_mut_children_with(self);
self.is_string_lit_important = old;
}
_ => {
e.visit_mut_children_with(self);
}
}

match e {
Expr::Seq(seq) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export function foo() {
define("require", function(r) {
define([
"require"
], function(r) {
r("lodash");
});
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
if (null) module.exports;
else if (define, define.amd) define(null, null);
else if (define, define.amd) define([], null);
5 changes: 4 additions & 1 deletion crates/swc_webpack_ast/tests/fixture/react-dom/dev/output.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module, require("react"), define, define.amd, define("react", null);
module, require("react"), define, define.amd, define([
"exports",
"react"
], null);
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module, require("react"), define, define.amd, define("react", null);
module, require("react"), define, define.amd, define([
"exports",
"react"
], null);

1 comment on commit eda514b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: eda514b Previous: 1eb62dc Ratio
base_tr_fixer 30817 ns/iter (± 3384) 33716 ns/iter (± 4445) 0.91
base_tr_resolver_and_hygiene 174115 ns/iter (± 25639) 173622 ns/iter (± 47450) 1.00
codegen_es2015 64197 ns/iter (± 4858) 62811 ns/iter (± 10950) 1.02
codegen_es2016 63929 ns/iter (± 7418) 69858 ns/iter (± 7885) 0.92
codegen_es2017 63590 ns/iter (± 5718) 60423 ns/iter (± 11081) 1.05
codegen_es2018 64525 ns/iter (± 6126) 65746 ns/iter (± 19792) 0.98
codegen_es2019 63781 ns/iter (± 5990) 73247 ns/iter (± 16557) 0.87
codegen_es2020 66863 ns/iter (± 6074) 69042 ns/iter (± 17133) 0.97
codegen_es3 64546 ns/iter (± 6737) 61508 ns/iter (± 13659) 1.05
codegen_es5 63889 ns/iter (± 6062) 60908 ns/iter (± 11748) 1.05
full_es2015 209270302 ns/iter (± 16131892) 205761718 ns/iter (± 27519501) 1.02
full_es2016 166224894 ns/iter (± 7299143) 168266838 ns/iter (± 23595267) 0.99
full_es2017 174488950 ns/iter (± 15584276) 175846208 ns/iter (± 27283896) 0.99
full_es2018 176481173 ns/iter (± 14887664) 170820891 ns/iter (± 27370619) 1.03
full_es2019 172072402 ns/iter (± 13311778) 160944621 ns/iter (± 31638358) 1.07
full_es2020 170193180 ns/iter (± 11153886) 163738848 ns/iter (± 38978174) 1.04
full_es3 243457134 ns/iter (± 52542565) 223713636 ns/iter (± 59028215) 1.09
full_es5 223986045 ns/iter (± 23463641) 206034713 ns/iter (± 31914899) 1.09
parser 816471 ns/iter (± 82159) 747448 ns/iter (± 108515) 1.09
ser_ast_node 173 ns/iter (± 19) 160 ns/iter (± 27) 1.08
ser_serde 171 ns/iter (± 15) 169 ns/iter (± 46) 1.01
emit_colors 11320693 ns/iter (± 6901475) 12220510 ns/iter (± 15382264) 0.93
emit_large 113095163 ns/iter (± 173563688) 69531607 ns/iter (± 98899020) 1.63
base_clone 2822484 ns/iter (± 227511) 2651690 ns/iter (± 361545) 1.06
fold_span 4674553 ns/iter (± 649098) 4409327 ns/iter (± 660800) 1.06
fold_span_panic 4913896 ns/iter (± 358791) 4777787 ns/iter (± 1049759) 1.03
visit_mut_span 3345630 ns/iter (± 328514) 3513389 ns/iter (± 740428) 0.95
visit_mut_span_panic 3356693 ns/iter (± 372984) 3459958 ns/iter (± 503433) 0.97
ast_clone 25417 ns/iter (± 2508) 21856 ns/iter (± 5309) 1.16
ast_clone_to_stable 76620 ns/iter (± 7382) 77047 ns/iter (± 17657) 0.99
ast_clone_to_stable_then_to_unstable 136865 ns/iter (± 22473) 139600 ns/iter (± 20718) 0.98
json_deserialize 2664261 ns/iter (± 453303) 2581640 ns/iter (± 353527) 1.03
json_serialize 111835 ns/iter (± 18128) 94624 ns/iter (± 15796) 1.18
boxing_boxed 151 ns/iter (± 15) 139 ns/iter (± 20) 1.09
boxing_boxed_clone 85 ns/iter (± 11) 78 ns/iter (± 12) 1.09
boxing_unboxed 143 ns/iter (± 17) 124 ns/iter (± 29) 1.15
boxing_unboxed_clone 73 ns/iter (± 9) 69 ns/iter (± 20) 1.06
time_10 380 ns/iter (± 38) 325 ns/iter (± 48) 1.17
time_15 811 ns/iter (± 68) 682 ns/iter (± 105) 1.19
time_20 1455 ns/iter (± 169) 1511 ns/iter (± 295) 0.96
time_40 5396 ns/iter (± 806) 5731 ns/iter (± 1116) 0.94
time_5 124 ns/iter (± 12) 106 ns/iter (± 19) 1.17
time_60 11701 ns/iter (± 1015) 10706 ns/iter (± 2187) 1.09

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.