From 62c2479684a7219e795eeedaa871c41c4974129c Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Fri, 16 Sep 2022 21:30:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 8 + crates/node_binding/Cargo.lock | 229 ++++++++++--------- crates/rspack_core/Cargo.toml | 18 +- crates/rspack_core/src/utils/mod.rs | 2 + crates/rspack_core/src/utils/tree_shaking.rs | 179 +++++++++++++++ 5 files changed, 322 insertions(+), 114 deletions(-) create mode 100644 crates/rspack_core/src/utils/tree_shaking.rs diff --git a/Cargo.lock b/Cargo.lock index 3980b886fb72..bcbcdfd7a027 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2137,6 +2137,7 @@ dependencies = [ name = "rspack_core" version = "0.1.0" dependencies = [ + "ahash", "anyhow", "async-trait", "crossbeam", @@ -2151,9 +2152,14 @@ dependencies = [ "rspack_loader_runner", "serde", "sugar_path", + "swc", "swc_common 0.28.7", "swc_css 0.118.2", "swc_ecma_ast 0.91.7", + "swc_ecma_codegen 0.124.13", + "swc_ecma_parser 0.119.11", + "swc_ecma_transforms", + "swc_ecma_visit 0.77.7", "tokio", "tracing", "tracing-chrome", @@ -2950,6 +2956,7 @@ checksum = "44e620ded71d80237149d26699daaebf69c14051e5de8104e3a41bd6f1ec3407" dependencies = [ "ahash", "ast_node", + "atty", "better_scoped_tls", "cfg-if", "debug_unreachable", @@ -2966,6 +2973,7 @@ dependencies = [ "swc_atoms 0.4.14", "swc_eq_ignore_macros", "swc_visit", + "termcolor", "tracing", "unicode-width", "url", diff --git a/crates/node_binding/Cargo.lock b/crates/node_binding/Cargo.lock index bc323c7abd54..d43060054288 100644 --- a/crates/node_binding/Cargo.lock +++ b/crates/node_binding/Cargo.lock @@ -83,9 +83,9 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" [[package]] name = "ast_node" -version = "0.8.2" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a36288803cd1605bc4f0e3189970a0db8e602bb01a39f8133889f35ece7ddde" +checksum = "cf94863c5fdfee166d0907c44e5fee970123b2b7307046d35d1e671aa93afbba" dependencies = [ "darling", "pmutil", @@ -1819,6 +1819,7 @@ dependencies = [ name = "rspack_core" version = "0.1.0" dependencies = [ + "ahash", "anyhow", "async-trait", "crossbeam", @@ -1833,9 +1834,13 @@ dependencies = [ "rspack_loader_runner", "serde", "sugar_path", - "swc_common 0.28.3", + "swc", + "swc_common 0.28.8", "swc_css 0.118.2", - "swc_ecma_ast 0.91.3", + "swc_ecma_ast 0.91.8", + "swc_ecma_codegen 0.124.14", + "swc_ecma_transforms", + "swc_ecma_visit 0.77.3", "tokio", "tracing", "tracing-chrome", @@ -1951,8 +1956,8 @@ dependencies = [ "serde", "serde_json", "sugar_path", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css 0.125.5", "swc_css_prefixer", "tokio", @@ -1974,8 +1979,8 @@ dependencies = [ "serde", "serde_json", "sha2", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_html", "swc_html_minifier", ] @@ -1996,9 +2001,9 @@ dependencies = [ "serde", "serde_json", "swc", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_parser 0.119.7", "swc_ecma_preset_env", "swc_ecma_transforms", @@ -2433,12 +2438,12 @@ dependencies = [ "serde", "serde_json", "sourcemap", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_cached 0.3.10", - "swc_common 0.28.3", + "swc_common 0.28.8", "swc_config", - "swc_ecma_ast 0.91.3", - "swc_ecma_codegen 0.124.8", + "swc_ecma_ast 0.91.8", + "swc_ecma_codegen 0.124.14", "swc_ecma_ext_transforms", "swc_ecma_lints", "swc_ecma_loader", @@ -2473,9 +2478,9 @@ dependencies = [ [[package]] name = "swc_atoms" -version = "0.4.14" +version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1106fb9b9fde9bd37b9e36ecc05bc1fa9f8ace6f425391ff2ec1141e041e8bd2" +checksum = "7f18e15aaf62701ebc4aabf01746571c2ab54b91c94b81de23e435d6fb2e2567" dependencies = [ "once_cell", "rustc-hash", @@ -2512,7 +2517,7 @@ dependencies = [ "once_cell", "regex", "serde", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", ] [[package]] @@ -2561,7 +2566,7 @@ dependencies = [ "serde", "siphasher", "string_cache", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_eq_ignore_macros", "swc_visit", "tracing", @@ -2571,12 +2576,13 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.28.3" +version = "0.28.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f22d127a374f2a34d7e78e0bc4e20cbeaac8ee433bf8c774e61edb591ffd0a" +checksum = "54a4f584c7b203c555c2ef23ede09ae38456305cd28989bf5f807b93316cbc57" dependencies = [ "ahash", "ast_node", + "atty", "better_scoped_tls", "cfg-if", "debug_unreachable", @@ -2590,9 +2596,10 @@ dependencies = [ "siphasher", "sourcemap", "string_cache", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_eq_ignore_macros", "swc_visit", + "termcolor", "tracing", "unicode-width", "url", @@ -2672,7 +2679,7 @@ dependencies = [ "is-macro", "serde", "string_enum", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_common 0.27.13", ] @@ -2685,8 +2692,8 @@ dependencies = [ "is-macro", "serde", "string_enum", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", ] [[package]] @@ -2715,7 +2722,7 @@ dependencies = [ "bitflags", "rustc-hash", "serde", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_common 0.27.13", "swc_css_ast 0.105.3", "swc_css_codegen_macros", @@ -2731,8 +2738,8 @@ dependencies = [ "bitflags", "rustc-hash", "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css_ast 0.112.2", "swc_css_codegen_macros", ] @@ -2787,7 +2794,7 @@ dependencies = [ "bitflags", "lexical", "serde", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_common 0.27.13", "swc_css_ast 0.105.3", ] @@ -2801,8 +2808,8 @@ dependencies = [ "bitflags", "lexical", "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css_ast 0.112.2", ] @@ -2816,8 +2823,8 @@ dependencies = [ "preset_env_base", "serde", "serde_json", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css_ast 0.112.2", "swc_css_utils 0.109.2", "swc_css_visit 0.111.2", @@ -2847,7 +2854,7 @@ dependencies = [ "once_cell", "serde", "serde_json", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_common 0.27.13", "swc_css_ast 0.105.3", "swc_css_visit 0.104.2", @@ -2862,8 +2869,8 @@ dependencies = [ "once_cell", "serde", "serde_json", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css_ast 0.112.2", "swc_css_visit 0.111.2", ] @@ -2888,7 +2895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a43d7093a4323b5d4bda6e16a38848771b2aafbb9c713e80dbb8b8dd5957a433" dependencies = [ "serde", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_common 0.27.13", "swc_css_ast 0.105.3", "swc_visit", @@ -2901,8 +2908,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26ccea582d5579aa8830f79c5f8f9dfee435d19dda76e0f7b2450dffdb5ddff9" dependencies = [ "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_css_ast 0.112.2", "swc_visit", ] @@ -2926,9 +2933,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.91.3" +version = "0.91.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645ede33220fe9c3c35c8c7aa419d23fd4ae4a7e4834c020ba8101b3b32c71ed" +checksum = "7df6d5ece8b32be2c4501b11d917a449d4e15a0cef935f61bd3583f277443914" dependencies = [ "bitflags", "is-macro", @@ -2936,8 +2943,8 @@ dependencies = [ "scoped-tls", "serde", "string_enum", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "unicode-id", ] @@ -2962,9 +2969,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "0.124.8" +version = "0.124.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee7b2f3b7f14c52cde042c2fb5e6b33179b192ec65a6e674fe46585afb3af363" +checksum = "e6155f5a65fe66e4d870d8755af60de5b2a4ab1d2464da640cd56618d3bee50c" dependencies = [ "memchr", "num-bigint", @@ -2972,9 +2979,9 @@ dependencies = [ "rustc-hash", "serde", "sourcemap", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_codegen_macros", "tracing", ] @@ -2999,9 +3006,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef8759fd2276cd1a44d54e3bbb4f5e1ad5ef02714c366d9586c75224413433fa" dependencies = [ "phf", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_utils 0.102.7", "swc_ecma_visit 0.77.3", ] @@ -3019,10 +3026,10 @@ dependencies = [ "rayon", "regex", "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_config", - "swc_ecma_ast 0.91.3", + "swc_ecma_ast 0.91.8", "swc_ecma_utils 0.102.7", "swc_ecma_visit 0.77.3", ] @@ -3045,7 +3052,7 @@ dependencies = [ "serde", "serde_json", "swc_cached 0.3.10", - "swc_common 0.28.3", + "swc_common 0.28.8", "tracing", ] @@ -3101,12 +3108,12 @@ dependencies = [ "rustc-hash", "serde", "serde_json", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_cached 0.3.10", - "swc_common 0.28.3", + "swc_common 0.28.8", "swc_config", - "swc_ecma_ast 0.91.3", - "swc_ecma_codegen 0.124.8", + "swc_ecma_ast 0.91.8", + "swc_ecma_codegen 0.124.14", "swc_ecma_parser 0.119.7", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_optimization 0.162.14", @@ -3147,9 +3154,9 @@ dependencies = [ "num-bigint", "serde", "smallvec", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "tracing", "typed-arena", ] @@ -3171,9 +3178,9 @@ dependencies = [ "serde_json", "st-map", "string_enum", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms", "swc_ecma_utils 0.102.7", "swc_ecma_visit 0.77.3", @@ -3185,9 +3192,9 @@ version = "0.193.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c58c5fa78c2079f5fd2faa639f997e144e9159d16875d822d9fbf0d14454c6d" dependencies = [ - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_compat", "swc_ecma_transforms_module", @@ -3236,9 +3243,9 @@ dependencies = [ "rustc-hash", "serde", "smallvec", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_parser 0.119.7", "swc_ecma_utils 0.102.7", "swc_ecma_visit 0.77.3", @@ -3251,9 +3258,9 @@ version = "0.97.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a39af3a2c5c34174c5ced5f1663483515722d5d00e3dedcc7da991bf786b197b" dependencies = [ - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms_base 0.108.9", "swc_ecma_utils 0.102.7", "swc_ecma_visit 0.77.3", @@ -3273,10 +3280,10 @@ dependencies = [ "ordered-float", "serde", "smallvec", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_config", - "swc_ecma_ast 0.91.3", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_classes", "swc_ecma_transforms_macros", @@ -3315,10 +3322,10 @@ dependencies = [ "pathdiff", "regex", "serde", - "swc_atoms 0.4.14", + "swc_atoms 0.4.15", "swc_cached 0.3.10", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_loader", "swc_ecma_parser 0.119.7", "swc_ecma_transforms_base 0.108.9", @@ -3363,9 +3370,9 @@ dependencies = [ "petgraph", "rustc-hash", "serde_json", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_parser 0.119.7", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_macros", @@ -3384,9 +3391,9 @@ dependencies = [ "either", "serde", "smallvec", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_classes", "swc_ecma_transforms_macros", @@ -3409,10 +3416,10 @@ dependencies = [ "serde", "sha-1 0.10.0", "string_enum", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_config", - "swc_ecma_ast 0.91.3", + "swc_ecma_ast 0.91.8", "swc_ecma_parser 0.119.7", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_macros", @@ -3427,9 +3434,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5d9657d45801907f37fe1e98fc869499baec99624661f259c7f174f76b4284a" dependencies = [ "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_transforms_base 0.108.9", "swc_ecma_transforms_react", "swc_ecma_utils 0.102.7", @@ -3461,9 +3468,9 @@ dependencies = [ "indexmap", "num_cpus", "once_cell", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_ecma_visit 0.77.3", "tracing", "unicode-id", @@ -3491,9 +3498,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ae3e2d13a22d8adaac9f38a5255431868da077da945ab2167720ad1fd091f31" dependencies = [ "num-bigint", - "swc_atoms 0.4.14", - "swc_common 0.28.3", - "swc_ecma_ast 0.91.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", + "swc_ecma_ast 0.91.8", "swc_visit", "tracing", ] @@ -3520,7 +3527,7 @@ dependencies = [ "miette", "once_cell", "parking_lot", - "swc_common 0.28.3", + "swc_common 0.28.8", ] [[package]] @@ -3532,7 +3539,7 @@ dependencies = [ "ahash", "indexmap", "petgraph", - "swc_common 0.28.3", + "swc_common 0.28.8", ] [[package]] @@ -3569,8 +3576,8 @@ dependencies = [ "is-macro", "serde", "string_enum", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", ] [[package]] @@ -3598,8 +3605,8 @@ dependencies = [ "auto_impl", "bitflags", "rustc-hash", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_html_ast 0.25.3", "swc_html_codegen_macros", "swc_html_utils 0.13.3", @@ -3665,8 +3672,8 @@ version = "0.30.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8630d10d0d29f1ac9eca90d74049ea8ea713ad339c00533d0457c3a14b8c75e4" dependencies = [ - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_html_ast 0.25.3", "swc_html_utils 0.13.3", ] @@ -3692,7 +3699,7 @@ dependencies = [ "once_cell", "serde", "serde_json", - "swc_common 0.28.3", + "swc_common 0.28.8", ] [[package]] @@ -3715,8 +3722,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39dd6f5736101054e5ad4ac2952d979ac7ba057c333a028d4912889a24dfa607" dependencies = [ "serde", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", "swc_html_ast 0.25.3", "swc_visit", ] @@ -3741,8 +3748,8 @@ checksum = "f812ad0846f5c7eda018d510b7e6e12d6a7dfdb28c123d545ef5b4291909392c" dependencies = [ "ahash", "dashmap", - "swc_atoms 0.4.14", - "swc_common 0.28.3", + "swc_atoms 0.4.15", + "swc_common 0.28.8", ] [[package]] diff --git a/crates/rspack_core/Cargo.toml b/crates/rspack_core/Cargo.toml index 932e52e32db5..1076136dcbe7 100644 --- a/crates/rspack_core/Cargo.toml +++ b/crates/rspack_core/Cargo.toml @@ -26,9 +26,21 @@ regex = "1.6.0" rspack_error = {path = "../rspack_error"} rspack_loader_runner = {path = "../loader_runner"} serde = '1' -swc_common = {version = "0.28.3", features = ["concurrent"]} +swc_common = { version = "0.28.3", features = ["concurrent", "tty-emitter"] } +swc = "0.226.16" +swc_ecma_codegen = "0.124.13" + tracing-chrome = "0.6.0" -tracing-subscriber = {version = "0.3.9", features = ["env-filter"]} +tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } url = "2.2.2" -xxhash-rust = {version = "0.8.5", features = ["xxh3"]} +xxhash-rust = { version = "0.8.5", features = ["xxh3"] } +swc_ecma_visit = "0.77.3" +ahash = "0.7.6" +swc_ecma_transforms = { version = "0.193.14", features = [ + "swc_ecma_transforms_module", + "swc_ecma_transforms_react", +] } + +[dev-dependencies] +swc_ecma_parser = "0.119.7" \ No newline at end of file diff --git a/crates/rspack_core/src/utils/mod.rs b/crates/rspack_core/src/utils/mod.rs index ab3d5f7cf78c..ae44326c8db8 100644 --- a/crates/rspack_core/src/utils/mod.rs +++ b/crates/rspack_core/src/utils/mod.rs @@ -14,6 +14,8 @@ pub use hooks::*; mod hash; pub use hash::*; +mod tree_shaking; +pub use tree_shaking::*; pub static PATH_START_BYTE_POS_MAP: Lazy>> = Lazy::new(|| Arc::new(DashMap::new())); diff --git a/crates/rspack_core/src/utils/tree_shaking.rs b/crates/rspack_core/src/utils/tree_shaking.rs new file mode 100644 index 000000000000..f78a47338bf4 --- /dev/null +++ b/crates/rspack_core/src/utils/tree_shaking.rs @@ -0,0 +1,179 @@ +use std::sync::Arc; + +use dashmap::DashMap; +use swc::SwcComments; +use swc_common::{ + comments::{self, Comment}, + BytePos, Globals, Mark, SourceMap, SyntaxContext, DUMMY_SP, GLOBALS, +}; +use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; +use swc_ecma_transforms::resolver; +use swc_ecma_visit::{noop_visit_mut_type, swc_ecma_ast::Module, VisitMut, VisitMutWith}; + +struct Noop { + trailing: Arc, ahash::RandomState>>, +} +impl Noop {} +impl VisitMut for Noop { + noop_visit_mut_type!(); + + fn visit_mut_ident(&mut self, n: &mut swc_ecma_visit::swc_ecma_ast::Ident) { + let ctxt = n.span.ctxt; + let hi = n.span.hi; + if SyntaxContext::empty() != ctxt { + match self.trailing.entry(hi) { + dashmap::mapref::entry::Entry::Occupied(mut value) => { + value.get_mut().insert( + 1, + Comment { + kind: comments::CommentKind::Block, + span: DUMMY_SP, + text: format!("#{}", ctxt.as_u32()).into(), + }, + ); + } + dashmap::mapref::entry::Entry::Vacant(entry) => { + entry.insert(vec![Comment { + kind: comments::CommentKind::Block, + span: DUMMY_SP, + text: format!("#{}", ctxt.as_u32()).into(), + }]); + } + }; + } + } +} + +pub fn dump_ast_with_ctxt( + module: &mut Module, + global: &Globals, + comment: SwcComments, + cm: Arc, +) -> String { + GLOBALS.set(global, || { + module.visit_mut_with(&mut resolver(Mark::new(), Mark::new(), false)); + let mut noop = Noop { + trailing: comment.trailing.clone(), + }; + noop.visit_mut_module(module); + }); + let code = { + let mut buf = vec![]; + + { + let mut emitter = Emitter { + cfg: swc_ecma_codegen::Config { + ..Default::default() + }, + cm: cm.clone(), + comments: Some(&comment), + wr: JsWriter::new(cm, "\n", &mut buf, None), + }; + + emitter.emit_module(module).unwrap(); + } + + String::from_utf8_lossy(&buf).to_string() + }; + code +} +#[cfg(test)] +mod test_tree_shaking { + use std::{path::Path, sync::Arc}; + + use super::*; + use once_cell::sync::Lazy; + use swc::{config::IsModule, Compiler, SwcComments}; + use swc_common::{ + errors::{ColorConfig, Handler}, + FileName, FilePathMapping, Globals, SourceMap, + }; + + use swc_ecma_parser::{EsConfig, Syntax, TsConfig}; + + pub static COMPILER: Lazy> = Lazy::new(|| { + let cm = Arc::new(SourceMap::new(FilePathMapping::empty())); + Arc::new(Compiler::new(cm)) + }); + + pub fn parse_file( + source_code: String, + filename: &str, + comments: &SwcComments, + ) -> Result { + let compiler = COMPILER.clone(); + let fm = compiler + .cm + .new_source_file(FileName::Custom(filename.to_owned()), source_code); + let handler = + Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(compiler.cm.clone())); + let p = Path::new(filename); + let ext = p.extension().and_then(|ext| ext.to_str()).unwrap_or("js"); + let syntax = if ext == "ts" || ext == "tsx" { + Syntax::Typescript(TsConfig { + decorators: false, + tsx: ext == "tsx", + ..Default::default() + }) + } else { + Syntax::Es(EsConfig { + private_in_object: true, + import_assertions: true, + jsx: ext == "jsx", + export_default_from: true, + decorators_before_export: true, + decorators: true, + fn_bind: true, + ..Default::default() + }) + }; + compiler + .parse_js( + fm, + &handler, + swc_ecma_visit::swc_ecma_ast::EsVersion::Es2022, + syntax, + IsModule::Bool(true), + Some(&comments), + ) + .map(|prog| prog.expect_module()) + } + #[test] + fn test_tree_shaking_dump() { + let source = r#" +import { test } from "./test.js"; +test; +{ + var b = 1000; +} +b; +// err = 3; +let a = 10; + +function another() { + let a = 100; +} + +another; + "#; + let expected = r#" +import { test/*#1*/ } from "./test.js"; +test /*#1*/ ; +{ + var b /*#1*/ = 1000; +}b /*#1*/ ; +// err = 3; +let a /*#1*/ = 10; +function another() { + let a /*#2*/ = 100; +} +another /*#1*/ ; +"#; + let comment = SwcComments::default(); + let globals = Globals::default(); + let mut module = parse_file(source.to_string(), "dojo.js", &comment).unwrap(); + let code = dump_ast_with_ctxt(&mut module, &globals, comment, COMPILER.cm.clone()); + + assert_eq!(code.trim(), expected.trim()); + } +} From d6c9462662e5341ee375476fbded90f1ef08079b Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Mon, 19 Sep 2022 13:14:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20gagit=20cz?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cr issues --- .vscode/settings.json | 1 + Cargo.lock | 1 - crates/node_binding/Cargo.lock | 1 - crates/rspack_core/Cargo.toml | 11 +++++------ crates/rspack_core/src/utils/tree_shaking.rs | 19 +++++++++---------- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8e374336dff2..d5abf49504d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,6 +33,7 @@ "crates/node_binding/Cargo.toml" ], "cSpell.words": [ + "Ctxt", "Deque", "napi", "postcss", diff --git a/Cargo.lock b/Cargo.lock index bcbcdfd7a027..181ceb53a694 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2137,7 +2137,6 @@ dependencies = [ name = "rspack_core" version = "0.1.0" dependencies = [ - "ahash", "anyhow", "async-trait", "crossbeam", diff --git a/crates/node_binding/Cargo.lock b/crates/node_binding/Cargo.lock index d43060054288..593a883ca2a0 100644 --- a/crates/node_binding/Cargo.lock +++ b/crates/node_binding/Cargo.lock @@ -1819,7 +1819,6 @@ dependencies = [ name = "rspack_core" version = "0.1.0" dependencies = [ - "ahash", "anyhow", "async-trait", "crossbeam", diff --git a/crates/rspack_core/Cargo.toml b/crates/rspack_core/Cargo.toml index 1076136dcbe7..9b649eb04351 100644 --- a/crates/rspack_core/Cargo.toml +++ b/crates/rspack_core/Cargo.toml @@ -16,15 +16,15 @@ async-trait = "0.1.53" nodejs-resolver = "0.0.39" once_cell = "1" sugar_path = "0.0.5" -tokio = {version = "1.17.0", features = ["full"]} +tokio = { version = "1.17.0", features = ["full"] } tracing = "0.1.34" # rspack_sources = "0.0.3" -hashbrown = {version = "0.12.1", features = ["rayon", "serde"]} +hashbrown = { version = "0.12.1", features = ["rayon", "serde"] } petgraph = "0.6.0" rayon = "1.5.3" regex = "1.6.0" -rspack_error = {path = "../rspack_error"} -rspack_loader_runner = {path = "../loader_runner"} +rspack_error = { path = "../rspack_error" } +rspack_loader_runner = { path = "../loader_runner" } serde = '1' swc_common = { version = "0.28.3", features = ["concurrent", "tty-emitter"] } swc = "0.226.16" @@ -36,11 +36,10 @@ tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } url = "2.2.2" xxhash-rust = { version = "0.8.5", features = ["xxh3"] } swc_ecma_visit = "0.77.3" -ahash = "0.7.6" swc_ecma_transforms = { version = "0.193.14", features = [ "swc_ecma_transforms_module", "swc_ecma_transforms_react", ] } [dev-dependencies] -swc_ecma_parser = "0.119.7" \ No newline at end of file +swc_ecma_parser = "0.119.7" diff --git a/crates/rspack_core/src/utils/tree_shaking.rs b/crates/rspack_core/src/utils/tree_shaking.rs index f78a47338bf4..90a91ecf0c7b 100644 --- a/crates/rspack_core/src/utils/tree_shaking.rs +++ b/crates/rspack_core/src/utils/tree_shaking.rs @@ -1,27 +1,26 @@ use std::sync::Arc; -use dashmap::DashMap; use swc::SwcComments; use swc_common::{ comments::{self, Comment}, - BytePos, Globals, Mark, SourceMap, SyntaxContext, DUMMY_SP, GLOBALS, + Globals, Mark, SourceMap, SyntaxContext, DUMMY_SP, GLOBALS, }; use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_transforms::resolver; use swc_ecma_visit::{noop_visit_mut_type, swc_ecma_ast::Module, VisitMut, VisitMutWith}; -struct Noop { - trailing: Arc, ahash::RandomState>>, +struct CtxtDumpVisitor { + comments: SwcComments, } -impl Noop {} -impl VisitMut for Noop { +impl CtxtDumpVisitor {} +impl VisitMut for CtxtDumpVisitor { noop_visit_mut_type!(); fn visit_mut_ident(&mut self, n: &mut swc_ecma_visit::swc_ecma_ast::Ident) { let ctxt = n.span.ctxt; let hi = n.span.hi; if SyntaxContext::empty() != ctxt { - match self.trailing.entry(hi) { + match self.comments.trailing.entry(hi) { dashmap::mapref::entry::Entry::Occupied(mut value) => { value.get_mut().insert( 1, @@ -52,10 +51,10 @@ pub fn dump_ast_with_ctxt( ) -> String { GLOBALS.set(global, || { module.visit_mut_with(&mut resolver(Mark::new(), Mark::new(), false)); - let mut noop = Noop { - trailing: comment.trailing.clone(), + let mut visitor = CtxtDumpVisitor { + comments: comment.clone(), }; - noop.visit_mut_module(module); + visitor.visit_mut_module(module); }); let code = { let mut buf = vec![];