From 8881c413a38a3dc0844f4a74f5bfed25b63fd0e7 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 19 Sep 2023 15:32:52 +0200 Subject: [PATCH] Fix useState function initialiser case for `optimize_server_react` transform (#55551) This addresses the case mentioned in https://github.com/vercel/next.js/pull/54925#pullrequestreview-1616315332. We can't optimize the case where we can't statically determine the type of the `useState` argument as it can be a function: `useState(() => ...)`. --- Cargo.lock | 653 +++++++++++++++++- .../crates/core/src/optimize_server_react.rs | 74 +- .../fixture/optimize_server_react/3/output.js | 4 +- 3 files changed, 693 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a93a39019086a..56e77b9f28941 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,6 +202,23 @@ dependencies = [ "backtrace", ] +[[package]] +name = "arbitrary" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db55d72333851e17d572bec876e390cd3b11eb1ef53ae821dd9f3b653d2b4569" + +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + [[package]] name = "arrayref" version = "0.3.7" @@ -213,6 +230,9 @@ name = "arrayvec" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +dependencies = [ + "serde", +] [[package]] name = "assert-json-diff" @@ -489,6 +509,46 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "av-metrics" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "996ce95bbdb0203e5b91d4a0c9b81c0d67d11c80f884482a0c1ea19e732e3530" +dependencies = [ + "crossbeam", + "itertools", + "lab", + "num-traits", + "rayon", + "thiserror", + "v_frame", +] + +[[package]] +name = "av1-grain" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6ca6f0c18c02c2fbfc119df551b8aeb8a385f6d5980f1475ba0255f1e97f1e" +dependencies = [ + "anyhow", + "arrayvec", + "itertools", + "log", + "nom", + "num-rational", + "serde", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876c75a42f6364451a033496a14c44bffe41f5f4a8236f697391f11024e596d2" +dependencies = [ + "arrayvec", +] + [[package]] name = "axum" version = "0.6.12" @@ -600,6 +660,29 @@ dependencies = [ "serde", ] +[[package]] +name = "bindgen" +version = "0.59.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8" +dependencies = [ + "bitflags 1.3.2", + "cexpr", + "clang-sys", + "clap 2.34.0", + "env_logger 0.9.3", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "which", +] + [[package]] name = "binding_macros" version = "0.57.10" @@ -642,6 +725,12 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "bitstream-io" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82704769cb85a22df2c54d6bdd6a158b7931d256cf3248a07d6ecbe9d58b31d7" + [[package]] name = "bitvec" version = "1.0.1" @@ -726,6 +815,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "built" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b9c056b9ed43aee5e064b683aa1ec783e19c6acec7559e3ae931b7490472fbe" +dependencies = [ + "cargo-lock", + "git2", +] + [[package]] name = "bumpalo" version = "3.13.0" @@ -792,7 +891,7 @@ checksum = "031718ddb8f78aa5def78a09e90defe30151d1f6c672f937af4dd916429ed996" dependencies = [ "semver 1.0.18", "serde", - "toml", + "toml 0.5.11", "url", ] @@ -847,6 +946,25 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-expr" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b40ccee03b5175c18cde8f37e7d2a33bcef6f8ec8f7cc0d81090d1bb380949c9" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -966,6 +1084,32 @@ dependencies = [ "half", ] +[[package]] +name = "clang-sys" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags 1.3.2", + "strsim 0.8.0", + "textwrap 0.11.0", + "unicode-width", + "vec_map", +] + [[package]] name = "clap" version = "3.2.23" @@ -997,7 +1141,17 @@ dependencies = [ "anstream", "anstyle", "clap_lex 0.5.1", - "strsim", + "strsim 0.10.0", + "terminal_size 0.2.6", +] + +[[package]] +name = "clap_complete" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4110a1e6af615a9e6d0a36f805d5c99099f8bab9b8042f5bc1fa220a4a89e36f" +dependencies = [ + "clap 4.4.2", ] [[package]] @@ -1083,6 +1237,19 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.45.0", +] + [[package]] name = "console-api" version = "0.4.0" @@ -1155,6 +1322,12 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" +[[package]] +name = "const_fn_assert" +version = "0.1.3+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c773f3d54b8826857668aafa3bfe14bd179911a5b571b241e192cac1ec1c0b5" + [[package]] name = "const_format" version = "0.2.30" @@ -1391,6 +1564,20 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" +[[package]] +name = "crossbeam" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + [[package]] name = "crossbeam-channel" version = "0.5.8" @@ -1425,6 +1612,16 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.16" @@ -1611,7 +1808,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", + "strsim 0.10.0", "syn 1.0.109", ] @@ -1678,6 +1875,35 @@ dependencies = [ "matches", ] +[[package]] +name = "dav1d" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7284148338177cb1cd0d0cdd7bf26440f8326999063eed294aa7d77b46a7e263" +dependencies = [ + "dav1d-sys", +] + +[[package]] +name = "dav1d-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88e40c4c77d141a3b70113ee45a1502b9c80e24f176958d39a8361abcf30c883" +dependencies = [ + "bindgen", + "system-deps", +] + +[[package]] +name = "dcv-color-primitives" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1457f4dd8395fef9f61996b5783b82ed7b234b4b55e1843d04e07fded0538005" +dependencies = [ + "paste", + "wasm-bindgen", +] + [[package]] name = "debugid" version = "0.8.0" @@ -1815,6 +2041,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.32" @@ -1885,6 +2117,32 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "env_logger" version = "0.10.0" @@ -1946,6 +2204,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" +[[package]] +name = "fallible_collections" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a88c69768c0a15262df21899142bc6df9b9b823546d4b4b9a7bc2d6c448ec6fd" +dependencies = [ + "hashbrown 0.13.2", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -1970,6 +2237,15 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "fern" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee" +dependencies = [ + "log", +] + [[package]] name = "filetime" version = "0.2.22" @@ -2285,6 +2561,19 @@ version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" +[[package]] +name = "git2" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2994bee4a3a6a51eb90c218523be382fd7ea09b16380b9312e9dbe955ff7c7d1" +dependencies = [ + "bitflags 1.3.2", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glob" version = "0.3.1" @@ -2501,7 +2790,7 @@ dependencies = [ "base64 0.21.4", "clap 4.4.2", "crossbeam-utils", - "env_logger", + "env_logger 0.10.0", "form_urlencoded", "futures-util", "hyper", @@ -2661,14 +2950,25 @@ dependencies = [ "bytemuck", "byteorder", "color_quant", + "dav1d", + "dcv-color-primitives", "gif", "jpeg-decoder", + "mp4parse", "num-rational", "num-traits", "png", + "ravif", + "rgb", "webp", ] +[[package]] +name = "imgref" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2cf49df1085dcfb171460e4592597b84abe50d900fb83efb6e41b20fefd6c2c" + [[package]] name = "include_dir" version = "0.7.3" @@ -2745,6 +3045,17 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + [[package]] name = "io-lifetimes" version = "1.0.11" @@ -2849,6 +3160,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "ivf" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fb01c64361a3a67b511439f0dcd54fa3aa5581c861a17e2ede76e46b9c5b7e2" +dependencies = [ + "bitstream-io", +] + [[package]] name = "jni" version = "0.21.1" @@ -2929,6 +3249,12 @@ dependencies = [ "log", ] +[[package]] +name = "lab" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf36173d4167ed999940f804952e6b08197cae5ad5d572eb4db150ce8ad5d58f" + [[package]] name = "lazy-regex" version = "3.0.1" @@ -3064,6 +3390,28 @@ version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +[[package]] +name = "libfuzzer-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcf184a4b6b274f82a5df6b357da6055d3e82272327bba281c28bbba6f1664ef" +dependencies = [ + "arbitrary", + "cc", +] + +[[package]] +name = "libgit2-sys" +version = "0.14.2+1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f3d95f6b51075fe9810a7ae22c7095f12b98005ab364d8544797a825ce946a4" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libloading" version = "0.7.4" @@ -3206,6 +3554,15 @@ dependencies = [ "value-bag", ] +[[package]] +name = "loop9" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a703804431e5927454bcaf2b2a162595e95db931130c2728c18d050090f69940" +dependencies = [ + "imgref", +] + [[package]] name = "lru" version = "0.10.0" @@ -3269,6 +3626,16 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if 1.0.0", + "rayon", +] + [[package]] name = "md4" version = "0.10.2" @@ -3354,7 +3721,7 @@ dependencies = [ "supports-color", "supports-hyperlinks", "supports-unicode", - "terminal_size", + "terminal_size 0.1.17", "textwrap 0.15.2", "thiserror", "unicode-width", @@ -3515,6 +3882,21 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7843ec2de400bcbc6a6328c958dc38e5359da6e93e72e37bc5246bf1ae776389" +[[package]] +name = "mp4parse" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d189404bad70963b8848d9619032b02a115093f1fe9fb3d8ddf858e9b69ee9" +dependencies = [ + "bitreader", + "byteorder", + "env_logger 0.8.4", + "fallible_collections", + "log", + "num-traits", + "static_assertions", +] + [[package]] name = "napi" version = "2.12.5" @@ -3575,6 +3957,15 @@ dependencies = [ "libloading", ] +[[package]] +name = "nasm-rs" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe4d98d0065f4b1daf164b3eafb11974c94662e5e2396cf03f32d0bb5c17da51" +dependencies = [ + "rayon", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -3886,6 +4277,12 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + [[package]] name = "normpath" version = "0.2.0" @@ -3935,6 +4332,28 @@ dependencies = [ "serde", ] +[[package]] +name = "num-derive" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "num-derive" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e6a0fd4f737c707bd9086cc16c925f294943eb62eb71499e9fd4cf71f8b9f4e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -3952,6 +4371,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" dependencies = [ "autocfg", + "num-bigint", "num-integer", "num-traits", ] @@ -4247,6 +4667,12 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "percent-encoding" version = "2.3.0" @@ -4668,6 +5094,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quote" version = "1.0.33" @@ -4747,6 +5179,70 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rav1e" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "277898094f0d03c6a609e491324102daf5080e71c06b4b25e5acf8b89d26c945" +dependencies = [ + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-metrics", + "av1-grain", + "bitstream-io", + "built", + "cc", + "cfg-if 1.0.0", + "clap 4.4.2", + "clap_complete", + "console", + "const_fn_assert", + "fern", + "interpolate_name", + "itertools", + "ivf", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "nasm-rs", + "new_debug_unreachable", + "nom", + "noop_proc_macro", + "num-derive 0.3.3", + "num-traits", + "once_cell", + "paste", + "rand 0.8.5", + "rand_chacha", + "rust_hawktracer", + "rustc_version 0.4.0", + "scan_fmt", + "signal-hook", + "simd_helpers", + "system-deps", + "thiserror", + "v_frame", + "wasm-bindgen", + "y4m", +] + +[[package]] +name = "ravif" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cd36aa2bc280b60619e0c4386a73ff2ac343551dcf400168562ce08cc0c32e0" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error", + "rav1e", + "rayon", + "rgb", +] + [[package]] name = "raw-window-handle" version = "0.5.1" @@ -4959,6 +5455,15 @@ dependencies = [ "winreg", ] +[[package]] +name = "rgb" +version = "0.8.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20ec2d3e3fc7a92ced357df9cebd5a10b6fb2aa1ee797bf7e9ce2f17dffc8f59" +dependencies = [ + "bytemuck", +] + [[package]] name = "ring" version = "0.16.20" @@ -5003,6 +5508,28 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "rust_hawktracer" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3480a29b927f66c6e06527be7f49ef4d291a01d694ec1fe85b0de71d6b02ac1" +dependencies = [ + "rust_hawktracer_normal_macro", + "rust_hawktracer_proc_macro", +] + +[[package]] +name = "rust_hawktracer_normal_macro" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a570059949e1dcdc6f35228fa389f54c2c84dfe0c94c05022baacd56eacd2e9" + +[[package]] +name = "rust_hawktracer_proc_macro" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb626abdbed5e93f031baae60d72032f56bc964e11ac2ff65f2ba3ed98d6d3e1" + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -5117,6 +5644,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scan_fmt" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b53b0a5db882a8e2fdaae0a43f7b39e7e9082389e978398bdf223a55b581248" + [[package]] name = "schannel" version = "0.1.21" @@ -5405,6 +5938,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -5558,6 +6100,12 @@ dependencies = [ "dirs", ] +[[package]] +name = "shlex" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" + [[package]] name = "signal-hook" version = "0.3.15" @@ -5603,6 +6151,15 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f" +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "simdutf8" version = "0.1.4" @@ -5865,6 +6422,12 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strsim" version = "0.10.0" @@ -7075,6 +7638,19 @@ dependencies = [ "libc", ] +[[package]] +name = "system-deps" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30c2de8a4d8f4b823d634affc9cd2a74ec98c53a756f317e529a48046cbf71f3" +dependencies = [ + "cfg-expr", + "heck 0.4.1", + "pkg-config", + "toml 0.7.8", + "version-compare", +] + [[package]] name = "tap" version = "1.0.1" @@ -7139,6 +7715,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "terminal_size" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237" +dependencies = [ + "rustix 0.37.23", + "windows-sys 0.48.0", +] + [[package]] name = "termios" version = "0.3.3" @@ -7185,6 +7771,15 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "textwrap" version = "0.15.2" @@ -7439,11 +8034,26 @@ dependencies = [ "serde", ] +[[package]] +name = "toml" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + [[package]] name = "toml_datetime" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -7452,6 +8062,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ "indexmap 2.0.0", + "serde", + "serde_spanned", "toml_datetime", "winnow", ] @@ -8564,6 +9176,19 @@ dependencies = [ "serde", ] +[[package]] +name = "v_frame" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85db69f33d00031c1b07f7292e56317d5aa9475bdbd3d27ef18f3633438a697e" +dependencies = [ + "cfg-if 1.0.0", + "noop_proc_macro", + "num-derive 0.4.0", + "num-traits", + "rust_hawktracer", +] + [[package]] name = "valuable" version = "0.1.0" @@ -8582,6 +9207,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "vergen" version = "7.5.1" @@ -8597,6 +9228,12 @@ dependencies = [ "time 0.3.26", ] +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" + [[package]] name = "version_check" version = "0.9.4" @@ -9546,6 +10183,12 @@ dependencies = [ "tap", ] +[[package]] +name = "y4m" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a72a9921af8237fe25097a1ae31c92a05c1d39b2454653ad48f2f407cf7a0dae" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/packages/next-swc/crates/core/src/optimize_server_react.rs b/packages/next-swc/crates/core/src/optimize_server_react.rs index 0931bccd4249d..1d1f662eef65b 100644 --- a/packages/next-swc/crates/core/src/optimize_server_react.rs +++ b/packages/next-swc/crates/core/src/optimize_server_react.rs @@ -145,38 +145,50 @@ impl Fold for OptimizeServerReact { for decl in d { if let Pat::Array(array_pat) = &decl.name { if array_pat.elems.len() == 2 { - if let Some(array_pat_1) = &array_pat.elems[0] { - if let Some(array_pat_2) = &array_pat.elems[1] { - if let Some(box Expr::Call(call)) = &decl.init { - if let Callee::Expr(box Expr::Ident(f)) = &call.callee { - if let Some(use_state_ident) = &self.use_state_ident { - if &f.to_id() == use_state_ident && call.args.len() == 1 { - // const state = x, setState = () => {}; - new_d.push(VarDeclarator { - definite: false, - name: array_pat_1.clone(), - init: Some(call.args[0].expr.clone()), - span: DUMMY_SP, - }); - new_d.push(VarDeclarator { - definite: false, - name: array_pat_2.clone(), - init: Some(Box::new(Expr::Arrow(ArrowExpr { - body: Box::new(BlockStmtOrExpr::Expr( - Box::new(Expr::Lit(Lit::Null(Null { - span: DUMMY_SP, - }))), - )), - params: vec![], - is_async: false, - is_generator: false, + if let (Some(array_pat_1), Some(array_pat_2)) = + (&array_pat.elems[0], &array_pat.elems[1]) + { + if let Some(box Expr::Call(call)) = &decl.init { + if let Callee::Expr(box Expr::Ident(f)) = &call.callee { + if let Some(use_state_ident) = &self.use_state_ident { + if &f.to_id() == use_state_ident && call.args.len() == 1 { + // We do the optimization only if the arg is a literal or a + // type that we can + // be sure is not a function (e.g. {} or [] lit). + // This is because useState allows a function as the + // initialiser. + match &call.args[0].expr { + box Expr::Lit(_) + | box Expr::Object(_) + | box Expr::Array(_) => { + // const state = x, setState = () => {}; + new_d.push(VarDeclarator { + definite: false, + name: array_pat_1.clone(), + init: Some(call.args[0].expr.clone()), span: DUMMY_SP, - type_params: None, - return_type: None, - }))), - span: DUMMY_SP, - }); - continue; + }); + new_d.push(VarDeclarator { + definite: false, + name: array_pat_2.clone(), + init: Some(Box::new(Expr::Arrow(ArrowExpr { + body: Box::new(BlockStmtOrExpr::Expr( + Box::new(Expr::Lit(Lit::Null(Null { + span: DUMMY_SP, + }))), + )), + params: vec![], + is_async: false, + is_generator: false, + span: DUMMY_SP, + type_params: None, + return_type: None, + }))), + span: DUMMY_SP, + }); + continue; + } + _ => {} } } } diff --git a/packages/next-swc/crates/core/tests/fixture/optimize_server_react/3/output.js b/packages/next-swc/crates/core/tests/fixture/optimize_server_react/3/output.js index 6fda0ba75a8eb..e3a99cbe265c9 100644 --- a/packages/next-swc/crates/core/tests/fixture/optimize_server_react/3/output.js +++ b/packages/next-swc/crates/core/tests/fixture/optimize_server_react/3/output.js @@ -1,8 +1,8 @@ import { useState } from 'react'; export default function App({ x }) { const state = 0, setState = ()=>null; - const state2 = ()=>0, setState2 = ()=>null; - const state3 = x, setState3 = ()=>null; + const [state2, setState2] = useState(()=>0); + const [state3, setState3] = useState(x); const s = useState(0); const [state4] = useState(0); const { a } = {