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 ecb69e8169c..47d7edd5c33 100644 --- a/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/bb/snapshot/new_treeshaking.snap @@ -30,6 +30,9 @@ __webpack_require__.d(__webpack_exports__, { "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'ccc': function() { return ccc; } +}); const ccc = 30; }, "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { diff --git a/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff index 59fa831735a..e652df29d17 100644 --- a/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/bb/snapshot/snap.diff @@ -10,13 +10,3 @@ /* 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"); __webpack_require__.es(_c_js__WEBPACK_IMPORTED_MODULE_1_, __webpack_exports__); -@@ -27,9 +30,6 @@ - "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'ccc': function() { return ccc; } --}); - const ccc = 30; - }, - "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { diff --git a/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/new_treeshaking.snap index 640365ce899..7e25db3d68b 100644 --- a/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/new_treeshaking.snap @@ -53,6 +53,9 @@ __webpack_require__(/* ./answer */"./answer.js"); "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'myanswer': function() { return myanswer; } +}); const myanswer = 'anyser'; }, diff --git a/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/snap.diff index 8037312974b..a6430de07d2 100644 --- a/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/cjs-tree-shaking-basic/snapshot/snap.diff @@ -34,13 +34,3 @@ const answer = 42; }, -@@ -32,9 +53,6 @@ - "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'myanswer': function() { return myanswer; } --}); - const myanswer = 'anyser'; - }, - diff --git a/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/new_treeshaking.snap index 8de2c371ee3..a312beb8439 100644 --- a/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/new_treeshaking.snap @@ -48,6 +48,9 @@ _containers__WEBPACK_IMPORTED_MODULE_0_.usePlatform; "./src/containers/platform-container/index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'usePlatform': function() { return usePlatform; } +}); const usePlatform = 3; const PlatformProvider = 1000; }, diff --git a/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/snap.diff index f861150f333..d2d7a788ca2 100644 --- a/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/cyclic-reference-export-all/snapshot/snap.diff @@ -9,7 +9,7 @@ }, -@@ -36,13 +38,16 @@ +@@ -36,12 +38,18 @@ }, @@ -23,10 +23,10 @@ "./src/containers/platform-container/index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { + __webpack_require__.d(__webpack_exports__, { - 'usePlatform': function() { return usePlatform; }, - 'PlatformProvider': function() { return PlatformProvider; } --}); ++ 'usePlatform': function() { return usePlatform; } + }); const usePlatform = 3; const PlatformProvider = 1000; - }, diff --git a/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/new_treeshaking.snap index 64528fd1cec..bd579c58e56 100644 --- a/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/new_treeshaking.snap @@ -11,6 +11,9 @@ __webpack_require__.r(__webpack_exports__); "./baz.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'a': function() { return a; } +}); const a = 'baz'; }, "./foo.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { diff --git a/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/snap.diff deleted file mode 100644 index 074fc1f59ba..00000000000 --- a/crates/rspack/tests/tree-shaking/explicit_named_export_higher_priority_2/snapshot/snap.diff +++ /dev/null @@ -1,12 +0,0 @@ ---- expected -+++ actual -@@ -11,9 +11,6 @@ - "./baz.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'a': function() { return a; } --}); - const a = 'baz'; - }, - "./foo.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { diff --git a/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/new_treeshaking.snap index a85308807b4..f6b34955491 100644 --- a/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/new_treeshaking.snap @@ -11,6 +11,9 @@ __webpack_require__.r(__webpack_exports__); "./bar.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'c': function() { return c; } +}); const a = 'bar'; const c = 'bar'; }, diff --git a/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/snap.diff index b10a0a5deb4..8ddaf3b9fb0 100644 --- a/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/inherit_export_map_should_lookup_in_dfs_order/snapshot/snap.diff @@ -1,6 +1,6 @@ --- expected +++ actual -@@ -6,9 +6,6 @@ +@@ -6,14 +6,14 @@ "./a.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); @@ -10,7 +10,15 @@ const c = 'a'; }, "./bar.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { -@@ -34,9 +31,6 @@ + 'use strict'; + __webpack_require__.r(__webpack_exports__); ++__webpack_require__.d(__webpack_exports__, { ++ 'c': function() { return c; } ++}); + const a = 'bar'; + const c = 'bar'; + }, +@@ -34,9 +34,6 @@ "./foo.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); diff --git a/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/new_treeshaking.snap index ea1b9a9cb4f..aba7c01a3bf 100644 --- a/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/new_treeshaking.snap @@ -22,6 +22,9 @@ __webpack_require__.d(__webpack_exports__, { "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'cccc': function() { return cccc; } +}); function cccc() {} }, "./export.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { diff --git a/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/snap.diff deleted file mode 100644 index e5ffb0a33f5..00000000000 --- a/crates/rspack/tests/tree-shaking/named-export-decl-with-src-eval/snapshot/snap.diff +++ /dev/null @@ -1,12 +0,0 @@ ---- expected -+++ actual -@@ -22,9 +22,6 @@ - "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'cccc': function() { return cccc; } --}); - function cccc() {} - }, - "./export.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 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 30772feef30..6fc712822cc 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 @@ -25,6 +25,9 @@ var __WEBPACK_DEFAULT_EXPORT__ = 2000 + _c_js__WEBPACK_IMPORTED_MODULE_0_["defau "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } +}); var __WEBPACK_DEFAULT_EXPORT__ = 10; }, "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 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 ff1e3a953a2..95848703fd8 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 @@ -22,13 +22,3 @@ /* harmony import */var _c_js__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./c.js */"./c.js"); var __WEBPACK_DEFAULT_EXPORT__ = 2000 + _c_js__WEBPACK_IMPORTED_MODULE_0_["default"]; -@@ -26,9 +25,6 @@ - "./c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } --}); - var __WEBPACK_DEFAULT_EXPORT__ = 10; - }, - "./index.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 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 f30553cc4df..f3b5f1f1447 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 @@ -25,6 +25,9 @@ __webpack_require__.r(__webpack_exports__); "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'something': function() { return something; } +}); const secret = "888"; const result = 20000; const something = function() {}; 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 e185ae7797b..7c415aca8ca 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 @@ -13,13 +13,13 @@ }, -@@ -23,14 +25,18 @@ - "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { +@@ -24,13 +26,20 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { + __webpack_require__.d(__webpack_exports__, { - 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } --}); ++ 'something': function() { return something; } + }); const secret = "888"; const result = 20000; const something = function() {}; 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 f30553cc4df..f3b5f1f1447 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 @@ -25,6 +25,9 @@ __webpack_require__.r(__webpack_exports__); "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'something': function() { return something; } +}); const secret = "888"; const result = 20000; const something = function() {}; 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 a8f1dc798d2..1b416716cf3 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 @@ -10,13 +10,12 @@ }); /* 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,9 +25,6 @@ - "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { +@@ -25,7 +26,7 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { + __webpack_require__.d(__webpack_exports__, { - 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } --}); ++ 'something': function() { return something; } + }); const secret = "888"; const result = 20000; - const something = function() {}; diff --git a/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/new_treeshaking.snap b/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/new_treeshaking.snap index 4dbef1807bb..dffc8e633d7 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/new_treeshaking.snap +++ b/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/new_treeshaking.snap @@ -32,6 +32,9 @@ __webpack_require__.r(__webpack_exports__); "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'something': function() { return something; } +}); const secret = "888"; const result = 20000; const something = function() {}; diff --git a/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/snap.diff b/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/snap.diff index 50316c9cd0c..6c92388ec30 100644 --- a/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/snap.diff +++ b/crates/rspack/tests/tree-shaking/side-effects-prune/snapshot/snap.diff @@ -21,14 +21,7 @@ // export { // result as test -@@ -23,13 +32,50 @@ - "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'something': function() { return something; } --}); - const secret = "888"; +@@ -30,6 +39,46 @@ const result = 20000; const something = function() {}; }, 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 e44fe5db447..8c1ff80c839 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 @@ -27,6 +27,9 @@ __webpack_require__.r(__webpack_exports__); "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'something': function() { return something; } +}); const secret = "888"; const result = 20000; const something = function() {}; 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 d4b294baedb..1dffbccc59c 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 @@ -13,13 +13,13 @@ // export { // result as test -@@ -25,14 +27,18 @@ - "./lib.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { +@@ -26,13 +28,20 @@ 'use strict'; __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { + __webpack_require__.d(__webpack_exports__, { - 'default': function() { return __WEBPACK_DEFAULT_EXPORT__; } --}); ++ 'something': function() { return something; } + }); const secret = "888"; const result = 20000; const something = function() {}; 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 0e552f932e1..86dbf936ed2 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 @@ -6,6 +6,9 @@ source: crates/rspack_testing/src/run_fixture.rs "../node_modules/pmodule/a.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'a': function() { return a; } +}); /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); var a = "a"; var b = "b"; @@ -18,7 +21,8 @@ var c = "c"; 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; } + 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; }, + 'x': function() { return x; } }); /* harmony import */var _c__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./c */"../node_modules/pmodule/c.js"); /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); @@ -32,6 +36,9 @@ var y = "y"; "../node_modules/pmodule/c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'z': function() { return z; } +}); /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); var z = "z"; 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 f424ef4f502..12e3e7d7fa1 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,36 +1,6 @@ --- expected +++ actual -@@ -6,9 +6,6 @@ - "../node_modules/pmodule/a.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'a': function() { return a; } --}); - /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); - var a = "a"; - var b = "b"; -@@ -21,8 +18,7 @@ - 'use strict'; - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; }, -- 'x': function() { return x; } -+ 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; } - }); - /* harmony import */var _c__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./c */"../node_modules/pmodule/c.js"); - /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); -@@ -36,9 +32,6 @@ - "../node_modules/pmodule/c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'z': function() { return z; } --}); - /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); - var z = "z"; - -@@ -50,6 +43,7 @@ +@@ -50,6 +50,7 @@ __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, 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 871b7bc537c..4192d94eace 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 @@ -18,7 +18,8 @@ var c = "c"; 'use strict'; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { - 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; } + 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; }, + 'x': function() { return x; } }); /* harmony import */var _c__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./c */"../node_modules/pmodule/c.js"); /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); @@ -32,6 +33,9 @@ var y = "y"; "../node_modules/pmodule/c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + 'z': function() { return z; } +}); /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); var z = "z"; 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 49706d21142..69f970ed91f 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,6 +1,6 @@ --- expected +++ actual -@@ -3,12 +3,22 @@ +@@ -3,6 +3,17 @@ --- ```js title=main.js (self['webpackChunkwebpack'] = self['webpackChunkwebpack'] || []).push([["main"], { @@ -18,24 +18,7 @@ "../node_modules/pmodule/b.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { 'use strict'; __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { -- 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; }, -- 'x': function() { return x; } -+ 'z': function() { return _c__WEBPACK_IMPORTED_MODULE_0_.z; } - }); - /* harmony import */var _c__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./c */"../node_modules/pmodule/c.js"); - /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_1_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); -@@ -22,9 +32,6 @@ - "../node_modules/pmodule/c.js": function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - 'use strict'; - __webpack_require__.r(__webpack_exports__); --__webpack_require__.d(__webpack_exports__, { -- 'z': function() { return z; } --}); - /* harmony import */var _tracker__WEBPACK_IMPORTED_MODULE_0_ = __webpack_require__(/* ./tracker */"../node_modules/pmodule/tracker.js"); - var z = "z"; - -@@ -36,9 +43,12 @@ +@@ -36,9 +47,12 @@ __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { 'x': function() { return _b__WEBPACK_IMPORTED_MODULE_1_.x; }, diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 6d31cc79b77..91666e45292 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -758,9 +758,19 @@ impl ExportInfo { } } - // TODO pub fn get_used(&self, _runtime: Option<&RuntimeSpec>) -> UsageState { - UsageState::Unused + if !self.has_use_in_runtime_info { + return UsageState::NoInfo; + } + if let Some(global_used) = self.global_used { + return global_used; + } + if self.used_in_runtime.is_none() { + UsageState::Unused + } else { + // TODO: runtime optimization + todo!() + } } /// Webpack returns `false | string`, we use `Option` to avoid declare a redundant enum 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 a7338e84c83..2766499e876 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 @@ -43,7 +43,8 @@ impl HarmonyExportImportedSpecifierDependency { pub fn active_exports<'a>(&self, module_graph: &'a ModuleGraph) -> &'a HashSet { let build_info = module_graph - .module_graph_module_by_dependency_id(&self.id) + .parent_module_by_dependency_id(&self.id) + .and_then(|ident| module_graph.module_graph_module_by_identifier(&ident)) .expect("should have mgm") .build_info .as_ref() @@ -51,6 +52,7 @@ impl HarmonyExportImportedSpecifierDependency { &build_info.harmony_named_exports } + /// FIXME: this should use parent module id pub fn all_star_exports<'a>(&self, module_graph: &'a ModuleGraph) -> &'a Vec { let build_info = module_graph .module_graph_module_by_dependency_id(&self.id) @@ -164,7 +166,6 @@ impl HarmonyExportImportedSpecifierDependency { ignored_exports, hidden, } = self.get_star_reexports(module_graph, runtime, imported_module_identifier); - if let Some(exports) = exports { if exports.is_empty() { let mut export_mode = ExportMode::new(ExportModeType::EmptyStar); @@ -255,6 +256,7 @@ impl HarmonyExportImportedSpecifierDependency { .parent_module_by_dependency_id(&self.id) .expect("should have parent module"); let exports_info = module_graph.get_exports_info(&parent_module); + if no_extra_imports { for export_info_id in exports_info.get_ordered_exports() { let export_info = module_graph @@ -262,11 +264,17 @@ impl HarmonyExportImportedSpecifierDependency { .get(export_info_id) .expect("should have export info"); let export_name = export_info.name.clone().unwrap_or_default(); + // dbg!( + // &export_info.get_used(runtime), + // &ignored_exports, + // &export_name + // ); if ignored_exports.contains(&export_name) || matches!(export_info.get_used(runtime), UsageState::Unused) { continue; } + let imported_export_info = imported_exports_info .id .get_read_only_export_info(&export_name, module_graph); diff --git a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs index ff9f9bfcb7f..bf5b8d14fe2 100644 --- a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs @@ -85,12 +85,12 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> { .module_graph_module_by_identifier(&module_id) .expect("should have module graph module"); let dep_id_list = mgm.dependencies.clone(); - // dbg!(&module_id); for dep_id in dep_id_list.into_iter() { let connection = self .compilation .module_graph .connection_by_dependency(&dep_id); + let connection = if let Some(connection) = connection { connection } else { @@ -98,6 +98,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> { }; let active_state = connection.get_active_state(&self.compilation.module_graph, runtime.as_ref()); + match active_state { ConnectionState::Bool(false) => { continue; @@ -120,11 +121,6 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> { continue; }; - // dbg!( - // &connection, - // dep.as_module_dependency().unwrap().dependency_type() - // ); - // dbg!(&referenced_exports, &old_referenced_exports); if old_referenced_exports.is_none() || matches!(old_referenced_exports.as_ref().expect("should be some"), ProcessModuleReferencedExports::ExtendRef(v) if is_no_exports_referenced(v)) || is_exports_object_referenced(&referenced_exports) diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs index 65c2c0a9997..2cfb189fe6f 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs @@ -134,6 +134,13 @@ impl Visit for HarmonyImportDependencyScanner<'_> { ); if importer_info.exports_all { self.build_info.all_star_exports.push(dependency.id); + self + .dependencies + .push(Box::new(HarmonyExportImportedSpecifierDependency::new( + request.clone(), + vec![], + None, + ))); } self.dependencies.push(Box::new(dependency)); } @@ -260,13 +267,6 @@ impl Visit for HarmonyImportDependencyScanner<'_> { fn visit_export_all(&mut self, export_all: &ExportAll) { let key = (export_all.src.value.clone(), DependencyType::EsmExport); - // self - // .dependencies - // .push(Box::new(HarmonyExportImportedSpecifierDependency::new( - // key.0.clone(), - // vec![], - // None, - // ))); if let Some(importer_info) = self.imports.get_mut(&key) { importer_info.exports_all = true; } else {