From bef6f7ff68b4acbcf670baf2f4a8faafddb73df5 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Thu, 28 Oct 2021 18:57:36 +0200 Subject: [PATCH 1/2] WASM --- .gitignore | 5 +-- .prettierrc | 6 ++++ Cargo.lock | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 19 ++++++++--- build.rs | 2 ++ native.js | 19 ++--------- package.json | 4 ++- src/lib.rs | 62 +++++++++++++++++++++++---------- 8 files changed, 172 insertions(+), 41 deletions(-) create mode 100644 .prettierrc diff --git a/.gitignore b/.gitignore index 095f0b4a..9b332c12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -node_modules -target *.node +node_modules/ +target/ +pkg/ diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..3b23a667 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "bracketSpacing": false, + "endOfLine": "lf", + "singleQuote": true, + "trailingComma": "all" +} diff --git a/Cargo.lock b/Cargo.lock index ceb35050..ec272fc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bumpalo" +version = "3.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c" + [[package]] name = "byteorder" version = "1.4.3" @@ -93,6 +99,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fs_extra" version = "1.2.0" @@ -164,6 +176,21 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cc9ffccd38c451a86bf13657df244e9c3f37493cce8e5e21e940963777acc84" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.103" @@ -236,13 +263,16 @@ dependencies = [ "indoc", "itertools", "jemallocator", + "js-sys", "napi", "napi-build", "napi-derive", "selectors", "serde", + "serde-wasm-bindgen", "serde_bytes", "smallvec", + "wasm-bindgen", ] [[package]] @@ -455,6 +485,18 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "618365e8e586c22123d692b72a7d791d5ee697817b65a218cdf12a98870af0f7" +dependencies = [ + "fnv", + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_bytes" version = "0.11.5" @@ -549,6 +591,60 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasm-bindgen" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "632f73e236b219150ea279196e54e610f5dbafa5d61786303d4da54f84e47fce" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a317bf8f9fba2476b4b2c85ef4c4af8ff39c3c7f0cdfeed4f82c34a880aa837b" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56146e7c495528bf6587663bea13a8eb588d39b36b679d83972e1a2dbbdacf9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7803e0eea25835f8abdc585cd3021b3deb11543c6fe226dcd30b228857c5c5ab" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index f8eba167..d4f57f35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,6 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -napi = { version = "1", features = ["serde-json"] } -napi-derive = "1" serde = { version = "1.0.123", features = ["derive"] } serde_bytes = "0.11.5" cssparser = "0.28.1" @@ -23,5 +21,18 @@ bitflags = "*" [target.'cfg(target_os = "macos")'.dependencies] jemallocator = { version = "0.3.2", features = ["disable_initial_exec_tls"] } -[build-dependencies] -napi-build = { version = "1" } +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +napi = { version = "1", features = ["serde-json"] } +napi-derive = "1" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3" +serde-wasm-bindgen = "0.3.0" +wasm-bindgen = "0.2" + +[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies] +napi-build = "1" + +[profile.release] +lto = true +opt-level = 's' diff --git a/build.rs b/build.rs index 1f866b6a..4e9e5a27 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,7 @@ +#[cfg(not(target_arch = "wasm32"))] extern crate napi_build; fn main() { + #[cfg(not(target_arch = "wasm32"))] napi_build::setup(); } diff --git a/native.js b/native.js index a8f527fb..77705d4c 100644 --- a/native.js +++ b/native.js @@ -13,21 +13,8 @@ if (process.platform === 'linux') { } let name = `parcel-css.${parts.join('-')}.node`; -if (process.env.PARCEL_BUILD_ENV === 'production') { - module.exports = require(`./${name}`); -} else if (process.env.PARCEL_SWC_WASM) { - const {transform} = require('./wasm/dist-node/parcel_js_swc_wasm.js'); - - module.exports.transform = function(config) { - let result = transform(config); - return { - ...result, - // Hydrate Uint8Array into Buffer - code: Buffer.from(result.code.buffer), - }; - }; -} else if (require('fs').existsSync(require('path').join(__dirname, name))) { - module.exports = require(`./${name}`); +if (process.env.CSS_TRANSFORMER_WASM) { + module.exports = require(`./pkg`); } else { - module.exports = require(`self-published/${name}`); + module.exports = require(`./${name}`); } diff --git a/package.json b/package.json index fb5d2560..e712768b 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ }, "scripts": { "build": "napi build --platform", - "build-release": "napi build --platform --release" + "build-release": "napi build --platform --release", + "wasm:build": "wasm-pack build --target nodejs", + "wasm:build-release": "wasm-pack build --target nodejs --release" } } diff --git a/src/lib.rs b/src/lib.rs index 85402365..7467f744 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,3 @@ -extern crate napi; -#[macro_use] -extern crate napi_derive; extern crate serde; extern crate serde_bytes; extern crate cssparser; @@ -24,7 +21,6 @@ mod printer; mod traits; mod macros; -use napi::{CallContext, JsObject, JsBuffer}; use serde::{Deserialize, Serialize}; use cssparser::{Parser, ParserInput, RuleListParser}; use crate::traits::ToCss; @@ -36,25 +32,62 @@ use std::collections::HashMap; use parser::TopLevelRuleParser; -#[derive(Serialize, Debug, Deserialize)] -struct Config { - filename: String, - #[serde(with = "serde_bytes")] - code: Vec, - targets: Option +// --------------------------------------------- + +#[cfg(target_arch = "wasm32")] +use serde_wasm_bindgen::{from_value}; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::*; + +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +pub fn transform(config_val: JsValue) -> Result { + let config: Config = from_value(config_val).map_err(JsValue::from)?; + + let code = unsafe { std::str::from_utf8_unchecked(&config.code) }; + + Ok(compile(code, true, config.targets).into()) } +// --------------------------------------------- + +#[cfg(not(target_arch = "wasm32"))] +extern crate napi; +#[cfg(not(target_arch = "wasm32"))] +#[macro_use] +extern crate napi_derive; +#[cfg(not(target_arch = "wasm32"))] +use napi::{CallContext, JsObject, JsBuffer}; + +#[cfg(not(target_arch = "wasm32"))] #[js_function(1)] fn transform(ctx: CallContext) -> napi::Result { let opts = ctx.get::(0)?; let config: Config = ctx.env.from_js_value(opts)?; - let code = unsafe { std::str::from_utf8_unchecked(&config.code) }; let res = compile(code, true, config.targets); Ok(ctx.env.create_buffer_with_data(res.into_bytes())?.into_raw()) } +#[cfg(not(target_arch = "wasm32"))] +#[module_exports] +fn init(mut exports: JsObject) -> napi::Result<()> { + exports.create_named_method("transform", transform)?; + + Ok(()) +} + +// --------------------------------------------- + +#[derive(Serialize, Debug, Deserialize)] +struct Config { + pub filename: String, + #[serde(with = "serde_bytes")] + pub code: Vec, + pub targets: Option +} + fn compile(code: &str, minify: bool, targets: Option) -> String { let mut input = ParserInput::new(&code); let mut parser = Parser::new(&mut input); @@ -178,13 +211,6 @@ fn compile(code: &str, minify: bool, targets: Option) -> String { dest } -#[module_exports] -fn init(mut exports: JsObject) -> napi::Result<()> { - exports.create_named_method("transform", transform)?; - - Ok(()) -} - #[cfg(test)] mod tests { use super::*; From 6326bd6852eff7ee4c05d7bb9123196bb84da300 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Tue, 9 Nov 2021 09:23:13 -0500 Subject: [PATCH 2/2] Remove opt-level=s --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d4f57f35..e68a2269 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,4 +35,3 @@ napi-build = "1" [profile.release] lto = true -opt-level = 's'