minify-html/nodejs/native/src/lib.rs

19 lines
701 B
Rust
Raw Normal View History

2019-12-30 02:16:33 -05:00
use neon::prelude::*;
use minify_html::{Cfg, Error, in_place};
2019-12-30 02:16:33 -05:00
fn minify(mut cx: FunctionContext) -> JsResult<JsNumber> {
let mut buffer = cx.argument::<JsBuffer>(0)?;
let cfg_obj = cx.argument::<JsObject>(1)?;
let cfg = Cfg {
minify_js: cfg_obj.get(&mut cx, "minifyJs")?.downcast::<JsBoolean>().or_throw(&mut cx)?.value(),
};
2020-07-11 11:29:34 -04:00
match cx.borrow_mut(&mut buffer, |code| in_place(code.as_mut_slice::<u8>(), &cfg)) {
2019-12-30 02:16:33 -05:00
Ok(out_len) => Ok(cx.number(out_len as f64)),
Err(Error { error_type, position }) => cx.throw_error(format!("{} [Character {}]", error_type.message(), position)),
2019-12-30 02:16:33 -05:00
}
}
register_module!(mut cx, {
cx.export_function("minify", minify)
});