From c5299239d3173ac3e7ce0fc863ca8b82ebfe0afb Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:45:02 -0800 Subject: [PATCH] feat(next-swc): lightningcss binding (#62604) ### What This PR enables lightningcss features in next-swc. Currently this is for the native build targets only, for the wasm there are some unknown build error prevents to enable this. Closes PACK-2607 --- Cargo.lock | 132 +++++++++++-------- Cargo.toml | 5 +- packages/next-swc/crates/napi/Cargo.toml | 5 + packages/next-swc/crates/napi/src/css/mod.rs | 25 ++++ packages/next-swc/crates/napi/src/lib.rs | 2 + packages/next-swc/package.json | 2 +- packages/next/src/build/swc/index.ts | 14 ++ 7 files changed, 131 insertions(+), 54 deletions(-) create mode 100644 packages/next-swc/crates/napi/src/css/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 0b24128dfe..2ac1e93916 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,7 +44,7 @@ version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d713b3834d76b85304d4d525563c1276e2e30dc97cc67bfb4585a4a29fc2c89f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "getrandom", "once_cell", "serde", @@ -438,7 +438,7 @@ checksum = "4319208da049c43661739c5fade2ba182f09d1dc2299b32298d3a31692b17e12" dependencies = [ "addr2line", "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "miniz_oxide 0.7.1", "object", @@ -552,7 +552,7 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d84ea71c85d1fe98fe67a9b9988b1695bc24c0b0d3bfb18d4c510f44b4b09941" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -570,7 +570,7 @@ dependencies = [ "funty", "radium", "tap", - "wyz", + "wyz 0.5.1", ] [[package]] @@ -582,7 +582,7 @@ dependencies = [ "arrayref", "arrayvec", "cc", - "cfg-if 1.0.0", + "cfg-if", "constant_time_eq", "digest", ] @@ -760,12 +760,6 @@ dependencies = [ "target-lexicon", ] -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -945,7 +939,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "wasm-bindgen", ] @@ -1051,7 +1045,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9847f90f32a50b0dcbd68bc23ff242798b13080b97b0569f6ed96a45ce4cf2cd" dependencies = [ "autocfg", - "cfg-if 1.0.0", + "cfg-if", "libc", "scopeguard", "windows-sys 0.33.0", @@ -1170,7 +1164,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -1185,7 +1179,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-channel", "crossbeam-deque", "crossbeam-epoch", @@ -1199,7 +1193,7 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-utils", ] @@ -1209,7 +1203,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-epoch", "crossbeam-utils", ] @@ -1221,7 +1215,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" dependencies = [ "autocfg", - "cfg-if 1.0.0", + "cfg-if", "crossbeam-utils", "memoffset", "scopeguard", @@ -1233,7 +1227,7 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-utils", ] @@ -1243,7 +1237,7 @@ version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -1399,7 +1393,7 @@ version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "hashbrown 0.14.3", "lock_api", "once_cell", @@ -1605,7 +1599,7 @@ version = "0.8.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -1780,7 +1774,7 @@ version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4029edd3e734da6fe05b6cd7bd2960760a616bd2ddd0d59a0124746d6272af0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "windows-sys 0.48.0", @@ -1989,7 +1983,7 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "js-sys", "libc", "wasi", @@ -2778,7 +2772,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "winapi", ] @@ -2788,7 +2782,7 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "windows-sys 0.48.0", ] @@ -2826,17 +2820,18 @@ dependencies = [ [[package]] name = "lightningcss" -version = "1.0.0-alpha.51" +version = "1.0.0-alpha.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d6ad516c08b24c246b339159dc2ee2144c012e8ebdf4db4bddefb8734b2b69" +checksum = "07d306844e5af1753490c420c0d6ae3d814b00725092d106332762827ca8f0fe" dependencies = [ - "ahash 0.7.6", + "ahash 0.8.9", "bitflags 2.4.0", "const-str", "cssparser", "cssparser-color", "dashmap", "data-encoding", + "getrandom", "itertools 0.10.5", "lazy_static", "lightningcss-derive", @@ -2861,6 +2856,22 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "lightningcss-napi" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d1876c6046ed009d60145501187245afc823bcce50ed15964c7801f41592f4" +dependencies = [ + "cssparser", + "lightningcss", + "napi", + "parcel_sourcemap", + "serde", + "serde-detach", + "serde_bytes", + "smallvec", +] + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -2967,7 +2978,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "rayon", ] @@ -3201,7 +3212,7 @@ version = "2.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7622f0dbe0968af2dacdd64870eee6dee94f93c989c841f1ad8f300cf1abd514" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "convert_case 0.6.0", "napi-derive-backend", "proc-macro2", @@ -3361,6 +3372,7 @@ dependencies = [ "fxhash", "getrandom", "iana-time-zone", + "lightningcss-napi", "napi", "napi-build", "napi-derive", @@ -3600,7 +3612,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b277f87dacc05a6b709965d1cbafac4649d6ce9f3ce9ceb88508b5666dfec9" dependencies = [ "bitflags 1.3.2", - "cfg-if 1.0.0", + "cfg-if", "foreign-types", "libc", "once_cell", @@ -3728,7 +3740,7 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "smallvec", @@ -4258,7 +4270,7 @@ dependencies = [ "bitstream-io", "built", "cc", - "cfg-if 1.0.0", + "cfg-if", "clap 4.4.2", "clap_complete", "console", @@ -4824,6 +4836,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-detach" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c621150da442b6a854bb63c431347bcd4de19219a3e1f06fd744208ded057288" +dependencies = [ + "serde", + "wyz 0.2.0", +] + [[package]] name = "serde-wasm-bindgen" version = "0.4.5" @@ -4987,7 +5009,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -4998,7 +5020,7 @@ version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -5009,7 +5031,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -5232,7 +5254,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "psm", "winapi", @@ -5530,7 +5552,7 @@ dependencies = [ "atty", "better_scoped_tls", "bytecheck", - "cfg-if 1.0.0", + "cfg-if", "either", "from_variant", "new_debug_unreachable", @@ -6801,7 +6823,7 @@ version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "fastrand", "redox_syscall", "rustix 0.38.31", @@ -6945,7 +6967,7 @@ version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "once_cell", ] @@ -7232,7 +7254,7 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "log", "pin-project-lite", "tracing-attributes", @@ -8034,7 +8056,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "rand", "static_assertions", ] @@ -8208,7 +8230,7 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85db69f33d00031c1b07f7292e56317d5aa9475bdbd3d27ef18f3633438a697e" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "noop_proc_macro", "num-derive 0.4.0", "num-traits", @@ -8240,7 +8262,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f21b881cd6636ece9735721cf03c1fe1e774fe258683d084bb2812ab67435749" dependencies = [ "anyhow", - "cfg-if 1.0.0", + "cfg-if", "enum-iterator 1.4.1", "getset", "rustversion", @@ -8499,7 +8521,7 @@ version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "wasm-bindgen-macro", ] @@ -8524,7 +8546,7 @@ version = "0.4.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877b9c3f61ceea0e56331985743b13f3d25c406a7098d45180fb5f09bc19ed97" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "js-sys", "wasm-bindgen", "web-sys", @@ -8575,7 +8597,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5467c7a23f9be04d5691590bea509dbea27e5ba5810d0020bef908456a495f33" dependencies = [ "bytes", - "cfg-if 1.0.0", + "cfg-if", "derivative", "indexmap 1.9.3", "js-sys", @@ -8618,7 +8640,7 @@ checksum = "510ad01a668d774f3a103a7c219bbc0970be93e8f1b27e2fdb48d1f4ccd1deff" dependencies = [ "backtrace", "bytes", - "cfg-if 1.0.0", + "cfg-if", "enum-iterator 0.7.0", "enumset", "lazy_static", @@ -8711,7 +8733,7 @@ checksum = "58315c25492bc72a33f47a7d7fb0869a0106fc0164ec051e349a9e1eddba9a01" dependencies = [ "backtrace", "cc", - "cfg-if 1.0.0", + "cfg-if", "corosensei", "crossbeam-queue", "dashmap", @@ -8741,7 +8763,7 @@ dependencies = [ "async-trait", "bincode", "bytes", - "cfg-if 1.0.0", + "cfg-if", "chrono", "cooked-waker", "dashmap", @@ -8801,7 +8823,7 @@ dependencies = [ "anyhow", "bitflags 1.3.2", "byteorder", - "cfg-if 1.0.0", + "cfg-if", "num_enum", "serde", "time", @@ -9264,6 +9286,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "wyz" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + [[package]] name = "wyz" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 16972824ff..91e41b2786 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,11 +92,14 @@ indoc = "2.0.0" itertools = "0.10.5" lazy_static = "1.4.0" log = "0.4.17" -lightningcss = { version = "1.0.0-alpha.50", features = [ +lightningcss = { version = "=1.0.0-alpha.54", features = [ "serde", "visitor", "into_owned", ] } +lightningcss-napi = { version = "0.1.0", default-features = false, features = [ + "visitor" +]} mime = "0.3.16" nohash-hasher = "0.2.0" once_cell = "1.17.1" diff --git a/packages/next-swc/crates/napi/Cargo.toml b/packages/next-swc/crates/napi/Cargo.toml index fbb633aa73..e37bcad685 100644 --- a/packages/next-swc/crates/napi/Cargo.toml +++ b/packages/next-swc/crates/napi/Cargo.toml @@ -68,6 +68,10 @@ napi = { version = "2", default-features = false, features = [ "serde-json", "tokio_rt", "error_anyhow", + # Lightningcss uses this features + "napi4", + "napi5", + "compat-mode" ] } napi-derive = "2" next-custom-transforms = { workspace = true } @@ -83,6 +87,7 @@ urlencoding = {workspace = true} # Dependencies for the native, non-wasm32 build. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] +lightningcss-napi = { workspace = true } tokio = { workspace = true, features = ["full"] } turbo-tasks = { workspace = true } next-api = { workspace = true } diff --git a/packages/next-swc/crates/napi/src/css/mod.rs b/packages/next-swc/crates/napi/src/css/mod.rs new file mode 100644 index 0000000000..aa8a009dbc --- /dev/null +++ b/packages/next-swc/crates/napi/src/css/mod.rs @@ -0,0 +1,25 @@ +use napi::{CallContext, JsObject, JsUnknown}; +use napi_derive::{js_function, module_exports}; + +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[js_function(1)] +fn transform(ctx: CallContext) -> napi::Result { + lightningcss_napi::transform(ctx) +} + +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[js_function(1)] +fn transform_style_attribute(ctx: CallContext) -> napi::Result { + lightningcss_napi::transform_style_attribute(ctx) +} + +#[cfg_attr(not(target_arch = "wasm32"), module_exports)] +fn init(mut exports: JsObject) -> napi::Result<()> { + exports.create_named_method("lightningCssTransform", transform)?; + exports.create_named_method( + "lightningCssTransformStyleAttribute", + transform_style_attribute, + )?; + + Ok(()) +} diff --git a/packages/next-swc/crates/napi/src/lib.rs b/packages/next-swc/crates/napi/src/lib.rs index cd2a95c624..1eb4bd117a 100644 --- a/packages/next-swc/crates/napi/src/lib.rs +++ b/packages/next-swc/crates/napi/src/lib.rs @@ -49,6 +49,8 @@ use turbopack_binding::swc::core::{ #[cfg(not(target_arch = "wasm32"))] pub mod app_structure; +#[cfg(not(target_arch = "wasm32"))] +pub mod css; pub mod mdx; pub mod minify; #[cfg(not(target_arch = "wasm32"))] diff --git a/packages/next-swc/package.json b/packages/next-swc/package.json index c62399a2d3..b6e65f44e4 100644 --- a/packages/next-swc/package.json +++ b/packages/next-swc/package.json @@ -15,7 +15,7 @@ "rust-check-fmt": "cd ../..; cargo fmt -- --check", "rust-check-clippy": "cargo clippy --workspace --all-targets -- -D warnings -A deprecated", "rust-check-napi-rustls": "cargo check -p next-swc-napi", - "test-cargo-unit": "cargo nextest run --workspace --release --no-fail-fast" + "test-cargo-unit": "cargo nextest run --workspace --exclude next-swc-napi --release --no-fail-fast --features rustls-tls" }, "napi": { "name": "next-swc", diff --git a/packages/next/src/build/swc/index.ts b/packages/next/src/build/swc/index.ts index 34502acf97..a38b31eb81 100644 --- a/packages/next/src/build/swc/index.ts +++ b/packages/next/src/build/swc/index.ts @@ -173,6 +173,12 @@ export interface Binding { teardownTraceSubscriber?: any initHeapProfiler?: any teardownHeapProfiler?: any + css: { + lightning: { + transform(transformOptions: any): Promise + transformStyleAttr(attrOptions: any): Promise + } + } } export async function loadBindings( @@ -1444,6 +1450,14 @@ function loadNative(importPath?: string) { compileSync: (src: string, options: any) => bindings.mdxCompileSync(src, toBuffer(getMdxOptions(options))), }, + css: { + lightning: { + transform: (transformOptions: any) => + bindings.lightningCssTransform(transformOptions), + transformStyleAttr: (transformAttrOptions: any) => + bindings.lightningCssTransformStyleAttribute(transformAttrOptions), + }, + }, } return nativeBindings }