diff --git a/Cargo.lock b/Cargo.lock index ff2534d..a8abab4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -283,6 +283,7 @@ dependencies = [ "js-sys", "lazy_static", "serde", + "serde-wasm-bindgen", "serde_json", "swc", "swc_atoms", diff --git a/Cargo.toml b/Cargo.toml index 4b6249b..55f57e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ lazy_static = "1.4.0" base64 = "0.21.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +serde-wasm-bindgen = "0.4" wasm-bindgen = "0.2.63" js-sys = "0.3.64" diff --git a/package-lock.json b/package-lock.json index 0bf3765..ee67ef5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "content-tag", - "version": "1.2.1", + "version": "1.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "content-tag", - "version": "1.2.1", + "version": "1.2.2", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/package.json b/package.json index e94792a..a199647 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,11 @@ "import": { "types": "./index.d.ts", "browser": "./pkg/standalone.js", - "default": "./pkg/node/content_tag.cjs" + "default": "./pkg/node.cjs" }, "require": { "types": "./index.d.ts", - "default": "./pkg/node/content_tag.cjs" + "default": "./pkg/node.cjs" } } }, diff --git a/pkg/node.cjs b/pkg/node.cjs new file mode 100644 index 0000000..86b1043 --- /dev/null +++ b/pkg/node.cjs @@ -0,0 +1,24 @@ +const { Preprocessor: WasmPreprocessor } = require("./node/content_tag.cjs"); + +const defaultOptions = { + inline_source_map: false, + filename: null +}; + +class Preprocessor { + #preprocessor; + + constructor() { + this.#preprocessor = new WasmPreprocessor(); + } + + process(str, options = {}) { + return this.#preprocessor.process(str, { ...defaultOptions, ...options }); + } + + parse(str, options = {}) { + return this.#preprocessor.parse(str, { ...defaultOptions, ...options }); + } +} + +module.exports.Preprocessor = Preprocessor; diff --git a/pkg/standalone.js b/pkg/standalone.js index 0df00d7..7be08fc 100644 --- a/pkg/standalone.js +++ b/pkg/standalone.js @@ -1,5 +1,25 @@ import init from "./standalone/content_tag.js"; +import { Preprocessor as WasmPreprocessor } from "./standalone/content_tag.js"; await init(); -export { Preprocessor } from "./standalone/content_tag.js"; +const defaultOptions = { + inline_source_map: false, + filename: null +}; + +export class Preprocessor { + #preprocessor; + + constructor() { + this.#preprocessor = new WasmPreprocessor(); + } + + process(str, options = {}) { + return this.#preprocessor.process(str, { ...defaultOptions, ...options }); + } + + parse(str, options = {}) { + return this.#preprocessor.parse(str, { ...defaultOptions, ...options }); + } +} diff --git a/src/bindings.rs b/src/bindings.rs index a546d91..8f82fd2 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1,5 +1,9 @@ use crate::{Options, Preprocessor as CorePreprocessor}; -use std::{fmt, str}; +use std::{ + fmt, + str, + path::PathBuf, +}; use swc_common::{ errors::Handler, sync::{Lock, Lrc}, @@ -7,6 +11,8 @@ use swc_common::{ }; use swc_error_reporters::{GraphicalReportHandler, GraphicalTheme, PrettyEmitter}; use wasm_bindgen::prelude::*; +use serde::{Serialize, Deserialize}; +use serde_wasm_bindgen::from_value; #[wasm_bindgen] extern "C" { @@ -26,6 +32,12 @@ pub struct Preprocessor { // core: Box, } +#[derive(Serialize, Deserialize)] +pub struct ProcessOptions { + filename: Option, + inline_source_map: bool, +} + #[derive(Clone, Default)] struct Writer(Lrc>); @@ -86,13 +98,16 @@ impl Preprocessor { Self {} } - pub fn process(&self, src: String, filename: Option) -> Result { + pub fn process(&self, src: String, options: JsValue) -> Result { + let options: ProcessOptions = from_value(options) + .map_err(|e| js_error(format!("Options parsing error: {:?}", e).into()))?; + let preprocessor = CorePreprocessor::new(); let result = preprocessor.process( &src, Options { - filename: filename.map(|f| f.into()), - inline_source_map: false, + filename: options.filename.map(|f| PathBuf::from(f)), + inline_source_map: options.inline_source_map, }, ); @@ -102,17 +117,20 @@ impl Preprocessor { } } - pub fn parse(&self, src: String, filename: Option) -> Result { + pub fn parse(&self, src: String, options: JsValue) -> Result { + let parse_options: ProcessOptions = from_value(options.clone()) + .map_err(|e| js_error(format!("Options parsing error: {:?}", e).into()))?; + let preprocessor = CorePreprocessor::new(); let result = preprocessor .parse( &src, Options { - filename: filename.as_ref().map(|f| f.into()), - inline_source_map: false, + filename: parse_options.filename.map(|f| PathBuf::from(f)), + inline_source_map: parse_options.inline_source_map, }, ) - .map_err(|_err| self.process(src, filename).unwrap_err())?; + .map_err(|_err| self.process(src, options).unwrap_err())?; let serialized = serde_json::to_string(&result) .map_err(|err| js_error(format!("Unexpected serialization error; please open an issue with the following debug info: {err:#?}").into()))?; Ok(json_parse(serialized.into())) diff --git a/test/parse.test.js b/test/parse.test.js index 0e0c716..ac4b25a 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -173,7 +173,7 @@ describe(`parse`, function () { p.process( `const thing = "face";