diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index 25e2a30a14b4..244239611233 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -1,13 +1,14 @@ use std::sync::Arc; use napi::{ - bindgen_prelude::{AbortSignal, AsyncTask, Buffer}, - Task, + bindgen_prelude::{AbortSignal, AsyncTask, Buffer, Env, FromNapiValue}, + sys::{napi_env, napi_value}, + JsUnknown, NapiValue, Task, }; use serde::Deserialize; use swc_core::{ base::{ - config::{ErrorFormat, JsMinifyOptions}, + config::{JsMinifyOptions}, TransformOutput, }, common::{collections::AHashMap, sync::Lrc, FileName, SourceFile, SourceMap}, @@ -39,7 +40,7 @@ impl MinifyTarget { assert_eq!( codes.len(), 1, - "swc.minify does not support concatting multiple files yet" + "swc.minify does not support concatenating multiple files yet" ); let (filename, code) = codes.iter().next().unwrap(); @@ -57,17 +58,17 @@ impl Task for MinifyTask { fn compute(&mut self) -> napi::Result { let input: MinifyTarget = deserialize_json(&self.code)?; - let options: JsMinifyOptions = deserialize_json(&self.options)?; - - try_with(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| { - let fm = input.to_file(self.c.cm.clone()); + let options = self.options.clone(); + try_with(self.c.cm.clone(), false, Default::default(), |handler| { + let fm = input.to_file(self.c.cm.clone()); + self.c.minify(fm, handler, &options) }) .convert_err() } - fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> napi::Result { + fn resolve(&mut self, _env: Env, output: Self::Output) -> napi::Result { Ok(output) } } @@ -76,7 +77,8 @@ impl Task for MinifyTask { fn minify(code: Buffer, opts: Buffer, signal: Option) -> AsyncTask { crate::util::init_default_trace_subscriber(); let code = String::from_utf8_lossy(code.as_ref()).to_string(); - let options = String::from_utf8_lossy(opts.as_ref()).to_string(); + let opts_str = String::from_utf8_lossy(opts.as_ref()).to_string(); + let options: JsMinifyOptions = deserialize_json(&opts_str).expect("Invalid options format"); let c = get_compiler(); @@ -89,18 +91,13 @@ fn minify(code: Buffer, opts: Buffer, signal: Option) -> AsyncTask< pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result { crate::util::init_default_trace_subscriber(); let code: MinifyTarget = get_deserialized(code)?; - let opts = get_deserialized(opts)?; - + let options: JsMinifyOptions = deserialize_json(&String::from_utf8_lossy(opts.as_ref()).to_string())?; let c = get_compiler(); let fm = code.to_file(c.cm.clone()); - try_with( - c.cm.clone(), - false, - // TODO(kdy1): Maybe make this configurable? - ErrorFormat::Normal, - |handler| c.minify(fm, handler, &opts), - ) + try_with(c.cm.clone(), false, Default::default(), |handler| { + c.minify(fm, handler, &options) + }) .convert_err() }