diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType_es5.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType_es5.2.minified.js index 97bc33b28ff..897cad67ab9 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithBooleanType_es5.2.minified.js @@ -19,7 +19,7 @@ var objA = new A(); void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(!0), swcHelpers.typeOf({ x: !0, y: !1 -}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(!0), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(void 0 === BOOLEAN ? "undefined" : swcHelpers.typeOf(BOOLEAN)), swcHelpers.typeOf(!0), void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(!0), swcHelpers.typeOf(!0), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n); +}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(void 0 === BOOLEAN ? "undefined" : swcHelpers.typeOf(BOOLEAN)), swcHelpers.typeOf(!0), void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN), swcHelpers.typeOf(foo()), swcHelpers.typeOf(!0), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n); z: void 0 === BOOLEAN || swcHelpers.typeOf(BOOLEAN); r: swcHelpers.typeOf(foo); z: swcHelpers.typeOf(!0); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithNumberType_es5.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithNumberType_es5.2.minified.js index 26b1e6bed25..98c8bb6509c 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithNumberType_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithNumberType_es5.2.minified.js @@ -28,7 +28,7 @@ void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcH y: function(n) { return n; } -}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(NUMBER1[0]), swcHelpers.typeOf(1), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(NUMBER + NUMBER), swcHelpers.typeOf(void 0 === NUMBER ? "undefined" : swcHelpers.typeOf(NUMBER)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(NUMBER + NUMBER))), swcHelpers.typeOf(1), void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcHelpers.typeOf(1), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(objA.a), M.n; +}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(NUMBER1[0]), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(NUMBER + NUMBER), swcHelpers.typeOf(void 0 === NUMBER ? "undefined" : swcHelpers.typeOf(NUMBER)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(NUMBER + NUMBER))), swcHelpers.typeOf(1), void 0 === NUMBER || swcHelpers.typeOf(NUMBER), swcHelpers.typeOf(NUMBER1), swcHelpers.typeOf(foo()), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(objA.a), M.n; z: void 0 === NUMBER || swcHelpers.typeOf(NUMBER); x: swcHelpers.typeOf(NUMBER1); r: swcHelpers.typeOf(foo); diff --git a/crates/swc/tests/tsc-references/typeofOperatorWithStringType_es5.2.minified.js b/crates/swc/tests/tsc-references/typeofOperatorWithStringType_es5.2.minified.js index 33475fb745f..d91baa2d932 100644 --- a/crates/swc/tests/tsc-references/typeofOperatorWithStringType_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/typeofOperatorWithStringType_es5.2.minified.js @@ -28,7 +28,7 @@ void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcH y: function(s) { return s; } -}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(STRING1[0]), swcHelpers.typeOf("abc"), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(STRING + STRING), swcHelpers.typeOf(STRING.charAt(0)), swcHelpers.typeOf(void 0 === STRING ? "undefined" : swcHelpers.typeOf(STRING)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(STRING + STRING))), swcHelpers.typeOf(""), void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcHelpers.typeOf("abc"), swcHelpers.typeOf(objA.a), M.n; +}), swcHelpers.typeOf(objA.a), swcHelpers.typeOf(M.n), swcHelpers.typeOf(STRING1[0]), swcHelpers.typeOf(foo()), swcHelpers.typeOf(A.foo()), swcHelpers.typeOf(STRING + STRING), swcHelpers.typeOf(STRING.charAt(0)), swcHelpers.typeOf(void 0 === STRING ? "undefined" : swcHelpers.typeOf(STRING)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(STRING + STRING))), swcHelpers.typeOf(""), void 0 === STRING || swcHelpers.typeOf(STRING), swcHelpers.typeOf(STRING1), swcHelpers.typeOf(foo()), swcHelpers.typeOf(objA.a), M.n; z: void 0 === STRING || swcHelpers.typeOf(STRING); x: swcHelpers.typeOf(STRING1); r: swcHelpers.typeOf(foo); diff --git a/crates/swc/tests/tsc-references/variadicTuples1_es2015.2.minified.js b/crates/swc/tests/tsc-references/variadicTuples1_es2015.2.minified.js index 782a15826c5..e0de275d085 100644 --- a/crates/swc/tests/tsc-references/variadicTuples1_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/variadicTuples1_es2015.2.minified.js @@ -8,13 +8,6 @@ function curry(f, ...a) { return (...b)=>f(...a, ...b) ; } -function curry2(f, t, u) { - return f(...t, ...u); -} -function callApi(method) { - return (...args)=>method(...args, {}) - ; -} concat([], []), concat([ 'hello' ], [ @@ -52,22 +45,26 @@ concat([], []), concat([ [ 'def' ] -]), curry((a, b, c, d)=>0 -), curry((a, b, c, d)=>0 -, 1), curry((a, b, c, d)=>0 -, 1, 'abc'), curry((a, b, c, d)=>0 -, 1, 'abc', !0), curry((a, b, c, d)=>0 -, 1, 'abc', !0, [ +]); +const fn1 = (a, b, c, d)=>0 +; +curry(fn1), curry(fn1, 1), curry(fn1, 1, 'abc'), curry(fn1, 1, 'abc', !0), curry(fn1, 1, 'abc', !0, [ 'x', 'y' -]), curry((x, b, ...args)=>0 -), curry((x, b, ...args)=>0 -, 1), curry((x, b, ...args)=>0 -, 1, !0), curry((x, b, ...args)=>0 -, 1, !0, 'abc', 'def'), curry((...args)=>0 -), curry((...args)=>0 -, 'abc', 'def'), curry((...args)=>0 -, ...sa), curry2(fn10, [ +]); +const fn2 = (x, b, ...args)=>0 +; +curry(fn2), curry(fn2, 1), curry(fn2, 1, !0), curry(fn2, 1, !0, 'abc', 'def'); +const fn3 = (...args)=>0 +; +function curry2(f, t, u) { + return f(...t, ...u); +} +function callApi(method) { + return (...args)=>method(...args, {}) + ; +} +curry(fn3), curry(fn3, 'abc', 'def'), curry(fn3, ...sa), curry2(fn10, [ 'hello', 42 ], [ diff --git a/crates/swc_ecma_minifier/scripts/next/start2.sh b/crates/swc_ecma_minifier/scripts/next/start2.sh new file mode 100755 index 00000000000..af4b2b81189 --- /dev/null +++ b/crates/swc_ecma_minifier/scripts/next/start2.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# +# Usage: +# +# From your clone of next.js, you can invoke this script like +# +# /absolute/path/to/this/script.sh examples/foo +# +# This script will +# +# - build native binary (`next-swc`) +# - install dependnecies using `yarn`` +# - remove some dependencies (`next`, `react`, `react-dom`) +# - yarn next build examples/foo +# - yarn next start examples/foo +set -eu + +SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +dir="$1" +nextJsDir="$(pwd)" + +# Ensure that next-swc is up to date +echo "----- ⚠️ Building next-swc" +(cd ./packages/next-swc && yarn build-native) + +# Install dependencies +echo "----- ⚠️ Installing dependencies" +if [ ! -d "$dir/node_modules" ]; then + if test -f "$dir/yarn.lock"; then + echo " Using yarn" + # (cd $dir && yarn) + else + echo " Using yarn" + # (cd $dir && npm ci) + fi +fi + + +echo "----- ⚠️ Removing cache" +(cd $dir && rm -rf .next) + +echo "----- ⚠️ Replacing swc binary" +mv packages/next-swc/native/*.node $dir/../node_modules/@next/swc-*/ +ls -alh $dir/../node_modules/@next/swc-*/ + +# Build and start +echo "----- ⚠️ Building the app using next" +(cd $dir && npx next build) +(cd $dir && npx next start) \ No newline at end of file diff --git a/crates/swc_ecma_minifier/src/compress/optimize/inline.rs b/crates/swc_ecma_minifier/src/compress/optimize/inline.rs index 1d7a8856d26..a27aa416793 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/inline.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/inline.rs @@ -157,7 +157,10 @@ where op: op!("!"), arg, .. }) => arg.is_lit(), Expr::This(..) => usage.is_fn_local, - Expr::Arrow(arr) => is_arrow_simple_enough_for_copy(arr), + Expr::Arrow(arr) => { + !(usage.used_as_arg && usage.ref_count > 1) + && is_arrow_simple_enough_for_copy(arr) + } _ => false, } { @@ -251,7 +254,7 @@ where } if usage.used_as_arg && !usage.is_fn_local { - if let Expr::Fn(..) = &**init { + if let Expr::Fn(..) | Expr::Arrow(..) = &**init { return; } } @@ -466,7 +469,12 @@ where if let Some(usage) = self.data.vars.get(&i.to_id()) { if usage.declared_as_catch_param { - log_abort!("inline: [x] Declared as a catch parameter"); + log_abort!("inline: Declared as a catch parameter"); + return; + } + + if usage.used_as_arg && usage.ref_count > 1 { + log_abort!("inline: Used as an arugment"); return; } diff --git a/crates/swc_ecma_minifier/tests/TODO.txt b/crates/swc_ecma_minifier/tests/TODO.txt index 67947a0f71c..1d09d5972be 100644 --- a/crates/swc_ecma_minifier/tests/TODO.txt +++ b/crates/swc_ecma_minifier/tests/TODO.txt @@ -1,23 +1,13 @@ asm/asm_mixed/input.js collapse_vars/collapse_vars_do_while/input.js -collapse_vars/collapse_vars_lvalues/input.js -collapse_vars/collapse_vars_lvalues_drop_assign/input.js collapse_vars/collapse_vars_misc1/input.js -collapse_vars/collapse_vars_short_circuited_conditions/input.js -collapse_vars/collapse_vars_side_effects_2/input.js collapse_vars/issue_2497/input.js -conditionals/ifs_5/input.js drop_unused/keep_assign/input.js drop_unused/reassign_const/input.js drop_unused/var_catch_toplevel/input.js evaluate/issue_2535_1/input.js harmony/array_literal_with_spread_4a/input.js -harmony/default_assign/input.js -if_return/if_var_return/input.js inline/dont_inline_funcs_into_default_param_2/input.js -inline/inline_into_scope_conflict/input.js -issue_1034/non_hoisted_function_after_return_2a/input.js -issue_1034/non_hoisted_function_after_return_2b/input.js issue_1052/defun_else_if_return/input.js issue_1052/defun_if_return/input.js issue_281/inner_var_for_in_1/input.js diff --git a/crates/swc_ecma_minifier/tests/golden.txt b/crates/swc_ecma_minifier/tests/golden.txt index 84615840c3d..63badc4165f 100644 --- a/crates/swc_ecma_minifier/tests/golden.txt +++ b/crates/swc_ecma_minifier/tests/golden.txt @@ -532,6 +532,7 @@ expression/pow_with_parentheses/input.js expression/pow_with_unary_between_brackets/input.js functions/avoid_generating_duplicate_functions_compared_together/input.js functions/avoid_generating_duplicate_functions_compared_together_2/input.js +functions/avoid_generating_duplicate_functions_compared_together_3/input.js functions/avoid_generating_duplicate_functions_compared_together_4/input.js functions/deduplicate_parenthesis/input.js functions/drop_lone_use_strict/input.js diff --git a/crates/swc_ecma_minifier/tests/postponed.txt b/crates/swc_ecma_minifier/tests/postponed.txt index e49d43c4a47..c172daf0816 100644 --- a/crates/swc_ecma_minifier/tests/postponed.txt +++ b/crates/swc_ecma_minifier/tests/postponed.txt @@ -39,11 +39,15 @@ collapse_vars/collapse_vars_assignment/input.js collapse_vars/collapse_vars_constants/input.js collapse_vars/collapse_vars_do_while_drop_assign/input.js collapse_vars/collapse_vars_if/input.js +collapse_vars/collapse_vars_lvalues/input.js +collapse_vars/collapse_vars_lvalues_drop_assign/input.js collapse_vars/collapse_vars_object/input.js collapse_vars/collapse_vars_repeated/input.js collapse_vars/collapse_vars_self_reference/input.js collapse_vars/collapse_vars_short_circuit/input.js +collapse_vars/collapse_vars_short_circuited_conditions/input.js collapse_vars/collapse_vars_side_effects_1/input.js +collapse_vars/collapse_vars_side_effects_2/input.js collapse_vars/collapse_vars_switch/input.js collapse_vars/collapse_vars_unary/input.js collapse_vars/cond_branch_1/input.js @@ -91,6 +95,7 @@ collapse_vars/var_defs/input.js collapse_vars/var_side_effects_2/input.js conditionals/equality_conditionals_false/input.js conditionals/hoist_decl/input.js +conditionals/ifs_5/input.js conditionals/ifs_6/input.js conditionals/ifs_same_consequent/input.js conditionals/issue_1154/input.js @@ -210,7 +215,6 @@ export/name_cache_import_star_as_name_from_module/input.js export/name_cache_mangle_export_default_class/input.js export/name_cache_mangle_export_default_function/input.js export/name_cache_mangle_local_import_and_export_aliases/input.js -functions/avoid_generating_duplicate_functions_compared_together_3/input.js functions/duplicate_arg_var/input.js functions/empty_body/input.js functions/hoist_funs/input.js @@ -280,6 +284,7 @@ harmony/array_spread_of_sequence/input.js harmony/class_name_can_be_mangled/input.js harmony/class_name_can_be_preserved_with_reserved/input.js harmony/classes_extending_classes_out_of_pure_iifes/input.js +harmony/default_assign/input.js harmony/expansion/input.js harmony/fat_arrow_as_param/input.js harmony/import_statement_mangling/input.js @@ -335,14 +340,18 @@ ie8/issue_2254_1/input.js ie8/issue_2254_2/input.js ie8/reduce_vars/input.js if_return/if_return_same_value/input.js +if_return/if_var_return/input.js if_return/issue_2747/input.js if_return/issue_512/input.js inline/do_not_repeat_when_variable_larger_than_inlined_node/input.js inline/inline_annotation/input.js +inline/inline_into_scope_conflict/input.js inline/inline_within_extends_1/input.js inline/inline_within_extends_2/input.js issue_1034/non_hoisted_function_after_return/input.js +issue_1034/non_hoisted_function_after_return_2a/input.js issue_1034/non_hoisted_function_after_return_2a_strict/input.js +issue_1034/non_hoisted_function_after_return_2b/input.js issue_1034/non_hoisted_function_after_return_2b_strict/input.js issue_1034/non_hoisted_function_after_return_strict/input.js issue_1052/deeply_nested/input.js diff --git a/crates/swc_ecma_minifier/tests/terser_exec.rs b/crates/swc_ecma_minifier/tests/terser_exec.rs index e0bf9a404c2..635bea893f5 100644 --- a/crates/swc_ecma_minifier/tests/terser_exec.rs +++ b/crates/swc_ecma_minifier/tests/terser_exec.rs @@ -34,7 +34,6 @@ use testing::assert_eq; #[testing::fixture( "tests/terser/compress/**/input.js", exclude( - "functions/avoid_generating_duplicate_functions_compared_together_3/", // We don't care about ie8 "ie8", // tests with infinite loops