diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eaa715fbd29..f2979e31c71 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -169,4 +169,4 @@ jobs: echo 'debug = false' >> Cargo.toml - name: Run test - run: IS_NEW_TREESHAKING=1 cargo test --workspace -- --nocapture + run: cargo test --workspace -- --nocapture diff --git a/crates/rspack/tests/check.js b/crates/rspack/tests/check.js index 634f965a1bb..622be8493bd 100644 --- a/crates/rspack/tests/check.js +++ b/crates/rspack/tests/check.js @@ -19,11 +19,17 @@ const filteredList = dirList.filter((dir) => { console.log(`total: ${filteredList.length}`) +const falsePositiveList = [ + 'cjs-export-computed-property', // This one is false positive because webpack will not counted a esm export as unsed in an entry module, the previous implementation follows the esbuild behavior , see https://gist.github.com/IWANABETHATGUY/b41d0f80a558580010276a44b310a473 +] const failedList = filteredList.filter(item => { + if (falsePositiveList.includes(item)) { + return false; + } const abPath = path.join(currentDir, item, "snapshot", "snap.diff") return fs.existsSync(abPath) }) console.log(`failed: ${failedList.length}`) console.log(`passed: ${filteredList.length - failedList.length}`) -console.log(failedList) +console.log('failed list:\n', failedList) diff --git a/crates/rspack/tests/fixtures.rs b/crates/rspack/tests/fixtures.rs index e498a59bcdd..b5df1152582 100644 --- a/crates/rspack/tests/fixtures.rs +++ b/crates/rspack/tests/fixtures.rs @@ -23,10 +23,9 @@ fn samples(fixture_path: PathBuf) { #[fixture("tests/tree-shaking/*", exclude("node_modules"))] fn tree_shaking(fixture_path: PathBuf) { - std::env::set_var("IS_NEW_TREESHAKING", "1"); // For each test case // First test is old version tree shaking snapshot test - test_fixture(&fixture_path, Box::new(|_, _| {}), None); + // test_fixture(&fixture_path, Box::new(|_, _| {}), None); // second test is webpack based tree shaking IS_NEW_TREESHAKING.store(true, Ordering::SeqCst); test_fixture( diff --git a/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap index 1a73b1bc292..e0582015ed7 100644 --- a/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap @@ -6,9 +6,6 @@ source: crates/rspack_testing/src/run_fixture.rs "./a.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); -__webpack_require__.d(__webpack_exports__, { - 'c': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_.c; } -}); /* harmony import */var _c_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./c.js */"./c.js"); __webpack_require__.es(_c_js__WEBPACK_IMPORTED_MODULE_1_, __webpack_exports__); /* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./b.js */"./b.js"); diff --git a/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff index 724c55f1d9f..8500dbdd2cb 100644 --- a/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff @@ -1,13 +1,10 @@ --- expected +++ actual -@@ -6,9 +6,12 @@ +@@ -6,9 +6,9 @@ "./a.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); -/* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./b.js */"./b.js"); -+__webpack_require__.d(__webpack_exports__, { -+ 'c': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_.c; } -+}); /* harmony import */var _c_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./c.js */"./c.js"); __webpack_require__.es(_c_js__WEBPACK_IMPORTED_MODULE_1_, __webpack_exports__); +/* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./b.js */"./b.js"); diff --git a/crates/rspack/tests/tree-shaking/export_star/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/export_star/snapshot/new_treeshaking.snap index 99613de3738..c1d1ab86524 100644 --- a/crates/rspack/tests/tree-shaking/export_star/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/export_star/snapshot/new_treeshaking.snap @@ -6,9 +6,6 @@ source: crates/rspack_testing/src/run_fixture.rs "./bar.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); -__webpack_require__.d(__webpack_exports__, { - 'bar': function() { return _foo__WEBPACK_IMPORTED_MODULE_0_; } -}); /* harmony import */var _foo__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./foo */"./foo.js"); /* harmony import */var _result__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./result */"./result.js"); __webpack_require__.es(_result__WEBPACK_IMPORTED_MODULE_1_, __webpack_exports__); diff --git a/crates/rspack/tests/tree-shaking/export_star/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/export_star/snapshot/snap.diff index 45d4a1ce593..388d75dd7cd 100644 --- a/crates/rspack/tests/tree-shaking/export_star/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/export_star/snapshot/snap.diff @@ -1,6 +1,16 @@ --- expected +++ actual -@@ -19,9 +19,6 @@ +@@ -6,9 +6,6 @@ + "./bar.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { + 'use strict'; + __webpack_require__.r(__webpack_exports__); +-__webpack_require__.d(__webpack_exports__, { +- 'bar': function() { return _foo__WEBPACK_IMPORTED_MODULE_0_; } +-}); + /* harmony import */var _foo__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./foo */"./foo.js"); + /* harmony import */var _result__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./result */"./result.js"); + __webpack_require__.es(_result__WEBPACK_IMPORTED_MODULE_1_, __webpack_exports__); +@@ -19,9 +16,6 @@ "./foo.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); @@ -10,7 +20,7 @@ /* harmony import */var _bar__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./bar */"./bar.js"); __webpack_require__.es(_bar__WEBPACK_IMPORTED_MODULE_0_, __webpack_exports__); /* harmony import */var _result__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./result */"./result.js"); -@@ -42,9 +39,6 @@ +@@ -42,9 +36,6 @@ "./result.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); diff --git a/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/new_treeshaking.snap index 97e31c6bdc2..c0a468c61d1 100644 --- a/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/new_treeshaking.snap @@ -18,7 +18,6 @@ class Something { 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'Layout': function() { return _layout_export__WEBPACK_IMPORTED_MODULE_0_["default"]; }, 'Sider': function() { return _Something__WEBPACK_IMPORTED_MODULE_1_["default"]; } }); /* harmony import */var _layout_export__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./layout_export */"./layout_export.js"); diff --git a/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/snap.diff index 131b600fff4..d620f3725f4 100644 --- a/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/import-var-assign-side-effects/snapshot/snap.diff @@ -18,18 +18,15 @@ class Something { } }, -@@ -16,8 +18,10 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); +@@ -18,6 +20,7 @@ __webpack_require__.d(__webpack_exports__, { -+ 'Layout': function() { return _layout_export__WEBPACK_IMPORTED_MODULE_0_["default"]; }, 'Sider': function() { return _Something__WEBPACK_IMPORTED_MODULE_1_["default"]; } }); +/* harmony import */var _layout_export__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./layout_export */"./layout_export.js"); /* harmony import */var _Something__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./Something */"./Something.js"); -@@ -30,6 +34,17 @@ +@@ -30,6 +33,17 @@ (0, _export__WEBPACK_IMPORTED_MODULE_0_.Sider)(); }, diff --git a/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/new_treeshaking.snap index 09fab6f8765..6ecff837ed4 100644 --- a/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/new_treeshaking.snap @@ -31,8 +31,7 @@ _lib__WEBPACK_IMPORTED_MODULE_0_.a; 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'a': function() { return _a_js__WEBPACK_IMPORTED_MODULE_0_["default"]; }, - 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_1_["default"]; } + 'a': function() { return _a_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _a_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./a.js */"./a.js"); /* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./b.js */"./b.js"); diff --git a/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/snap.diff index 96d627e8475..8859c05281b 100644 --- a/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/prune-bailout-module/snapshot/snap.diff @@ -24,13 +24,8 @@ "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); -@@ -22,9 +31,11 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'a': function() { return _a_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } -+ 'a': function() { return _a_js__WEBPACK_IMPORTED_MODULE_0_["default"]; }, -+ 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_1_["default"]; } +@@ -25,6 +34,7 @@ + 'a': function() { return _a_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _a_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./a.js */"./a.js"); +/* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./b.js */"./b.js"); diff --git a/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/new_treeshaking.snap index 6fc712822cc..e23d57f53ab 100644 --- a/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/new_treeshaking.snap @@ -7,8 +7,7 @@ source: crates/rspack_testing/src/run_fixture.rs 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_["default"]; }, - 'c': function() { return _c_js__WEBPACK_IMPORTED_MODULE_1_["default"]; } + 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./b.js */"./b.js"); /* harmony import */var _c_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./c.js */"./c.js"); diff --git a/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/snap.diff index 95848703fd8..c918505b027 100644 --- a/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/reexport_entry_elimination/snapshot/snap.diff @@ -1,12 +1,7 @@ --- expected +++ actual -@@ -7,18 +7,17 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } -+ 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_["default"]; }, -+ 'c': function() { return _c_js__WEBPACK_IMPORTED_MODULE_1_["default"]; } +@@ -10,15 +10,13 @@ + 'b': function() { return _b_js__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _b_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./b.js */"./b.js"); +/* harmony import */var _c_js__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./c.js */"./c.js"); diff --git a/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/new_treeshaking.snap index 7862ba9701b..f166c73a967 100644 --- a/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/new_treeshaking.snap @@ -7,7 +7,6 @@ source: crates/rspack_testing/src/run_fixture.rs 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'a': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_.answer; }, 'q': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_.question; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); diff --git a/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/snap.diff index ba5b5c84f5c..b84c7524799 100644 --- a/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/rename-export-from-import/snapshot/snap.diff @@ -1,14 +1,6 @@ --- expected +++ actual -@@ -7,6 +7,7 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -+ 'a': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_.answer; }, - 'q': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_.question; } - }); - /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); -@@ -23,9 +24,6 @@ +@@ -23,9 +23,6 @@ "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); diff --git a/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/new_treeshaking.snap index f3b5f1f1447..afc1386b762 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/new_treeshaking.snap @@ -7,8 +7,7 @@ source: crates/rspack_testing/src/run_fixture.rs 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, - 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } + 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); /* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); diff --git a/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/snap.diff index 7c415aca8ca..ea4909df0e0 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/side-effects-analyzed/snapshot/snap.diff @@ -1,19 +1,14 @@ --- expected +++ actual -@@ -7,9 +7,11 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } -+ 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, -+ 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } +@@ -10,6 +10,7 @@ + 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); +/* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); }, -@@ -24,13 +26,20 @@ +@@ -24,13 +25,20 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { diff --git a/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/new_treeshaking.snap index f3b5f1f1447..afc1386b762 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/new_treeshaking.snap @@ -7,8 +7,7 @@ source: crates/rspack_testing/src/run_fixture.rs 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, - 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } + 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); /* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); diff --git a/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/snap.diff index 1b416716cf3..e04feddfff2 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/side-effects-flagged-only/snapshot/snap.diff @@ -1,16 +1,6 @@ --- expected +++ actual -@@ -7,7 +7,8 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } -+ 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, -+ 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } - }); - /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); - /* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); -@@ -25,7 +26,7 @@ +@@ -25,7 +25,7 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { diff --git a/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/new_treeshaking.snap index 8c1ff80c839..7fca6a907a9 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/new_treeshaking.snap @@ -7,8 +7,7 @@ source: crates/rspack_testing/src/run_fixture.rs 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, - 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } + 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); /* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); diff --git a/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/snap.diff index 1dffbccc59c..4953c035f97 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/side-effects-two/snapshot/snap.diff @@ -1,19 +1,14 @@ --- expected +++ actual -@@ -7,9 +7,11 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } -+ 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; }, -+ 'b': function() { return _src_a__WEBPACK_IMPORTED_MODULE_1_["default"]; } +@@ -10,6 +10,7 @@ + 'something': function() { return _lib__WEBPACK_IMPORTED_MODULE_0_["default"]; } }); /* harmony import */var _lib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./lib */"./lib.js"); +/* harmony import */var _src_a__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./src/a */"./src/a.js"); // export { // result as test -@@ -26,13 +28,20 @@ +@@ -26,13 +27,20 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { diff --git a/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/new_treeshaking.snap index 21f8bed113c..1e2d5450a5b 100644 --- a/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/new_treeshaking.snap @@ -41,8 +41,7 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - '_': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; }, - '_ts_generator': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; } + '_': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; } }); /* harmony import */var tslib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* tslib */"../../../../../node_modules/tslib/tslib.es6.js"); diff --git a/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/snap.diff index 0e1aa172d28..bc740092764 100644 --- a/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/ts-target-es5/snapshot/snap.diff @@ -1,16 +1,6 @@ --- expected +++ actual -@@ -41,7 +41,8 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- '_': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; } -+ '_': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; }, -+ '_ts_generator': function() { return tslib__WEBPACK_IMPORTED_MODULE_0_.__generator; } - }); - /* harmony import */var tslib__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* tslib */"../../../../../node_modules/tslib/tslib.es6.js"); - -@@ -49,9 +50,6 @@ +@@ -49,9 +49,6 @@ "../../../../../node_modules/tslib/tslib.es6.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); @@ -20,7 +10,7 @@ /****************************************************************************** Copyright (c) Microsoft Corporation. -@@ -482,9 +480,6 @@ +@@ -482,9 +479,6 @@ "./index.ts": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); diff --git a/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/new_treeshaking.snap index d2f8a0e0300..3cf8df94141 100644 --- a/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/new_treeshaking.snap @@ -50,7 +50,6 @@ var z = "z"; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, - 'y': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.y; }, 'z': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.z; }, 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } }); diff --git a/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/snap.diff index 3be0ae50528..a4a9c3d8af5 100644 --- a/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/webpack-side-effects-all-used/snapshot/snap.diff @@ -1,20 +1,12 @@ --- expected +++ actual -@@ -50,6 +50,7 @@ - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { - 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, -+ 'y': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.y; }, - 'z': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.z; }, - 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } - }); -@@ -82,8 +83,8 @@ +@@ -82,8 +82,8 @@ "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); --/* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); - /* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); -+/* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); ++/* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); + /* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); +-/* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); diff --git a/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/new_treeshaking.snap index 9087e70cd9a..97bb9b0f6c0 100644 --- a/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/new_treeshaking.snap @@ -36,7 +36,6 @@ var z = "z"; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, - 'y': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.y; }, 'z': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.z; }, 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } }); diff --git a/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/snap.diff index 37e7ca1e2c2..315398d9f1d 100644 --- a/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/webpack-side-effects-simple-unused/snapshot/snap.diff @@ -1,20 +1,12 @@ --- expected +++ actual -@@ -36,6 +36,7 @@ - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { - 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, -+ 'y': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.y; }, - 'z': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.z; }, - 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } - }); -@@ -66,8 +67,8 @@ +@@ -66,8 +66,8 @@ "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); --/* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); - /* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); -+/* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); ++/* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); + /* harmony import */var pmodule_tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* pmodule/tracker */"../node_modules/pmodule/tracker.js"); +-/* harmony import */var pmodule__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* pmodule */"../node_modules/pmodule/index.js"); diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 91666e45292..3bb674d37b0 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -404,8 +404,8 @@ impl ExportsInfo { } /// only used for old version tree shaking - pub fn old_get_used_exports(&self) -> HashSet<&JsWord> { - self.exports.keys().collect::>() + pub fn old_get_used_exports(&self) -> HashSet { + self.exports.keys().cloned().collect::>() } pub fn get_used( diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs index 5ce11799b57..903236a996a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs @@ -4,7 +4,7 @@ use rspack_core::{ DependencyCondition, DependencyId, DependencyTemplate, DependencyType, ErrorSpan, ExportInfoId, ExportInfoProvided, ExportsType, ExtendedReferencedExport, HarmonyExportInitFragment, ModuleDependency, ModuleGraph, ModuleIdentifier, RuntimeSpec, TemplateContext, - TemplateReplaceSource, UsageState, + TemplateReplaceSource, UsageState, UsedName, }; use rustc_hash::FxHashSet as HashSet; use swc_core::ecma::atoms::JsWord; @@ -398,6 +398,28 @@ impl DependencyTemplate for HarmonyExportImportedSpecifierDependency { .get_exports_info(&module.identifier()) .old_get_used_exports(), ) + } else if is_new_tree_shaking { + let exports_info_id = compilation + .module_graph + .get_exports_info(&module.identifier()) + .id; + let res = self + .ids + .iter() + .filter_map(|(local, _)| { + // TODO: runtime opt + exports_info_id.get_used_name( + &compilation.module_graph, + None, + UsedName::Str(local.clone()), + ) + }) + .map(|item| match item { + UsedName::Str(name) => name, + UsedName::Vec(_) => todo!(), + }) + .collect::>(); + Some(res) } else { None };