Skip to content

Commit

Permalink
Merge pull request #62 from vstefanovic97/main
Browse files Browse the repository at this point in the history
Add js/rust bindings for inline source map generation
  • Loading branch information
ef4 authored Jan 30, 2024
2 parents 203564e + 99aed16 commit 6e5c5a1
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions pkg/node.cjs
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 21 additions & 1 deletion pkg/standalone.js
Original file line number Diff line number Diff line change
@@ -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 });
}
}
34 changes: 26 additions & 8 deletions src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use crate::{Options, Preprocessor as CorePreprocessor};
use std::{fmt, str};
use std::{
fmt,
str,
path::PathBuf,
};
use swc_common::{
errors::Handler,
sync::{Lock, Lrc},
SourceMap, Spanned,
};
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" {
Expand All @@ -26,6 +32,12 @@ pub struct Preprocessor {
// core: Box<CorePreprocessor>,
}

#[derive(Serialize, Deserialize)]
pub struct ProcessOptions {
filename: Option<String>,
inline_source_map: bool,
}

#[derive(Clone, Default)]
struct Writer(Lrc<Lock<String>>);

Expand Down Expand Up @@ -86,13 +98,16 @@ impl Preprocessor {
Self {}
}

pub fn process(&self, src: String, filename: Option<String>) -> Result<String, JsValue> {
pub fn process(&self, src: String, options: JsValue) -> Result<String, JsValue> {
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,
},
);

Expand All @@ -102,17 +117,20 @@ impl Preprocessor {
}
}

pub fn parse(&self, src: String, filename: Option<String>) -> Result<JsValue, JsValue> {
pub fn parse(&self, src: String, options: JsValue) -> Result<JsValue, JsValue> {
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()))
Expand Down
2 changes: 1 addition & 1 deletion test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe(`parse`, function () {
p.process(
`const thing = "face";
<template>Hi`,
"path/to/my/component.gjs",
{ filename: 'path/to/my/component.gjs' }
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});
Expand Down
18 changes: 16 additions & 2 deletions test/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(`process`, function () {
}
});
}
};`,
};`
);
});

Expand All @@ -60,7 +60,7 @@ describe(`process`, function () {
p.process(
`const thing = "face";
<template>Hi`,
"path/to/my/component.gjs",
{ filename: "path/to/my/component.gjs" }
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});
Expand Down Expand Up @@ -89,4 +89,18 @@ describe(`process`, function () {
.to.have.property("source_code_color")
.matches(/Expected ident.*[\u001b].*class \{/s);
});

it("Provides inline source maps if inline_source_map option is set to true", function () {
let output = p.process(`<template>Hi</template>`, { inline_source_map: true });

expect(output).to.equalCode(
`import { template } from "@ember/template-compiler";
export default template(\`Hi\`, {
eval () {
return eval(arguments[0]);
}
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxhbm9uPiJdLCJzb3VyY2VzQ29udGVudCI6WyI8dGVtcGxhdGU-SGk8L3RlbXBsYXRlPiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsZUFBQSxTQUFVLENBQUEsRUFBRSxDQUFBLEVBQUE7SUFBQTtRQUFBLE9BQUEsS0FBQSxTQUFBLENBQUEsRUFBVztJQUFEO0FBQUEsR0FBQyJ9`
);
});
});

0 comments on commit 6e5c5a1

Please sign in to comment.