Skip to content

Commit bef6f7f

Browse files
committed
WASM
1 parent 29a487f commit bef6f7f

File tree

8 files changed

+172
-41
lines changed

8 files changed

+172
-41
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
node_modules
2-
target
31
*.node
2+
node_modules/
3+
target/
4+
pkg/

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"bracketSpacing": false,
3+
"endOfLine": "lf",
4+
"singleQuote": true,
5+
"trailingComma": "all"
6+
}

Cargo.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
napi = { version = "1", features = ["serde-json"] }
12-
napi-derive = "1"
1311
serde = { version = "1.0.123", features = ["derive"] }
1412
serde_bytes = "0.11.5"
1513
cssparser = "0.28.1"
@@ -23,5 +21,18 @@ bitflags = "*"
2321
[target.'cfg(target_os = "macos")'.dependencies]
2422
jemallocator = { version = "0.3.2", features = ["disable_initial_exec_tls"] }
2523

26-
[build-dependencies]
27-
napi-build = { version = "1" }
24+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
25+
napi = { version = "1", features = ["serde-json"] }
26+
napi-derive = "1"
27+
28+
[target.'cfg(target_arch = "wasm32")'.dependencies]
29+
js-sys = "0.3"
30+
serde-wasm-bindgen = "0.3.0"
31+
wasm-bindgen = "0.2"
32+
33+
[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies]
34+
napi-build = "1"
35+
36+
[profile.release]
37+
lto = true
38+
opt-level = 's'

build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#[cfg(not(target_arch = "wasm32"))]
12
extern crate napi_build;
23

34
fn main() {
5+
#[cfg(not(target_arch = "wasm32"))]
46
napi_build::setup();
57
}

native.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,8 @@ if (process.platform === 'linux') {
1313
}
1414

1515
let name = `parcel-css.${parts.join('-')}.node`;
16-
if (process.env.PARCEL_BUILD_ENV === 'production') {
17-
module.exports = require(`./${name}`);
18-
} else if (process.env.PARCEL_SWC_WASM) {
19-
const {transform} = require('./wasm/dist-node/parcel_js_swc_wasm.js');
20-
21-
module.exports.transform = function(config) {
22-
let result = transform(config);
23-
return {
24-
...result,
25-
// Hydrate Uint8Array into Buffer
26-
code: Buffer.from(result.code.buffer),
27-
};
28-
};
29-
} else if (require('fs').existsSync(require('path').join(__dirname, name))) {
30-
module.exports = require(`./${name}`);
16+
if (process.env.CSS_TRANSFORMER_WASM) {
17+
module.exports = require(`./pkg`);
3118
} else {
32-
module.exports = require(`self-published/${name}`);
19+
module.exports = require(`./${name}`);
3320
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
},
3232
"scripts": {
3333
"build": "napi build --platform",
34-
"build-release": "napi build --platform --release"
34+
"build-release": "napi build --platform --release",
35+
"wasm:build": "wasm-pack build --target nodejs",
36+
"wasm:build-release": "wasm-pack build --target nodejs --release"
3537
}
3638
}

src/lib.rs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate napi;
2-
#[macro_use]
3-
extern crate napi_derive;
41
extern crate serde;
52
extern crate serde_bytes;
63
extern crate cssparser;
@@ -24,7 +21,6 @@ mod printer;
2421
mod traits;
2522
mod macros;
2623

27-
use napi::{CallContext, JsObject, JsBuffer};
2824
use serde::{Deserialize, Serialize};
2925
use cssparser::{Parser, ParserInput, RuleListParser};
3026
use crate::traits::ToCss;
@@ -36,25 +32,62 @@ use std::collections::HashMap;
3632

3733
use parser::TopLevelRuleParser;
3834

39-
#[derive(Serialize, Debug, Deserialize)]
40-
struct Config {
41-
filename: String,
42-
#[serde(with = "serde_bytes")]
43-
code: Vec<u8>,
44-
targets: Option<Browsers>
35+
// ---------------------------------------------
36+
37+
#[cfg(target_arch = "wasm32")]
38+
use serde_wasm_bindgen::{from_value};
39+
#[cfg(target_arch = "wasm32")]
40+
use wasm_bindgen::prelude::*;
41+
42+
#[cfg(target_arch = "wasm32")]
43+
#[wasm_bindgen]
44+
pub fn transform(config_val: JsValue) -> Result<JsValue, JsValue> {
45+
let config: Config = from_value(config_val).map_err(JsValue::from)?;
46+
47+
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
48+
49+
Ok(compile(code, true, config.targets).into())
4550
}
4651

52+
// ---------------------------------------------
53+
54+
#[cfg(not(target_arch = "wasm32"))]
55+
extern crate napi;
56+
#[cfg(not(target_arch = "wasm32"))]
57+
#[macro_use]
58+
extern crate napi_derive;
59+
#[cfg(not(target_arch = "wasm32"))]
60+
use napi::{CallContext, JsObject, JsBuffer};
61+
62+
#[cfg(not(target_arch = "wasm32"))]
4763
#[js_function(1)]
4864
fn transform(ctx: CallContext) -> napi::Result<JsBuffer> {
4965
let opts = ctx.get::<JsObject>(0)?;
5066
let config: Config = ctx.env.from_js_value(opts)?;
51-
5267
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
5368
let res = compile(code, true, config.targets);
5469

5570
Ok(ctx.env.create_buffer_with_data(res.into_bytes())?.into_raw())
5671
}
5772

73+
#[cfg(not(target_arch = "wasm32"))]
74+
#[module_exports]
75+
fn init(mut exports: JsObject) -> napi::Result<()> {
76+
exports.create_named_method("transform", transform)?;
77+
78+
Ok(())
79+
}
80+
81+
// ---------------------------------------------
82+
83+
#[derive(Serialize, Debug, Deserialize)]
84+
struct Config {
85+
pub filename: String,
86+
#[serde(with = "serde_bytes")]
87+
pub code: Vec<u8>,
88+
pub targets: Option<Browsers>
89+
}
90+
5891
fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> String {
5992
let mut input = ParserInput::new(&code);
6093
let mut parser = Parser::new(&mut input);
@@ -178,13 +211,6 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> String {
178211
dest
179212
}
180213

181-
#[module_exports]
182-
fn init(mut exports: JsObject) -> napi::Result<()> {
183-
exports.create_named_method("transform", transform)?;
184-
185-
Ok(())
186-
}
187-
188214
#[cfg(test)]
189215
mod tests {
190216
use super::*;

0 commit comments

Comments
 (0)