From b9430590a48da05ad250436abd30aced8ddf9d57 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Wed, 21 Aug 2024 16:31:36 +0300 Subject: [PATCH 1/8] feat: make error format configurable --- bindings/binding_core_node/src/minify.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index 25e2a30a14b4..8895e831cd77 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -1,8 +1,9 @@ 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::{ @@ -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(); @@ -67,7 +68,7 @@ impl Task for MinifyTask { .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) } } @@ -89,18 +90,14 @@ 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 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(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| { + let fm = input.to_file(self.c.cm.clone()); + + self.c.minify(fm, handler, &options) + }) .convert_err() } From d1a57283faf0fe27b48f900f3b53841b02f1664a Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 22 Aug 2024 11:15:39 +0300 Subject: [PATCH 2/8] patch --- bindings/binding_core_node/src/minify.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index 8895e831cd77..a658cfb2e30c 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -60,11 +60,14 @@ impl Task for MinifyTask { 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()); - + try_with(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| { + let fm = input.to_file(self.c.cm.clone()); + self.c.minify(fm, handler, &options) }) + .map_err(|err| { + napi::Error::from_reason(format!("Minification failed: {}", err)) + }) .convert_err() } @@ -94,10 +97,8 @@ pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result let fm = code.to_file(c.cm.clone()); - try_with(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| { - let fm = input.to_file(self.c.cm.clone()); - - self.c.minify(fm, handler, &options) + try_with(c.cm.clone(), false, opts.error_format, |handler| { + c.minify(fm, handler, &opts) }) .convert_err() } From 09eeb68684cff288bd38d0be638477366aa005ec Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Thu, 22 Aug 2024 11:48:32 +0300 Subject: [PATCH 3/8] revert --- bindings/binding_core_node/src/minify.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index a658cfb2e30c..bf681e49c7fd 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -65,9 +65,6 @@ impl Task for MinifyTask { self.c.minify(fm, handler, &options) }) - .map_err(|err| { - napi::Error::from_reason(format!("Minification failed: {}", err)) - }) .convert_err() } From 454cae4d4e133ef774865bd491e2ab002cc60426 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 23 Aug 2024 14:18:11 +0300 Subject: [PATCH 4/8] repair --- bindings/binding_core_node/src/minify.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index bf681e49c7fd..251fe4a377e2 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -90,12 +90,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 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, opts.error_format, |handler| { - c.minify(fm, handler, &opts) + try_with(c.cm.clone(), false, options.error_format, |handler| { + c.minify(fm, handler, &options) }) .convert_err() } From c3f4596b5030d4fbcd215c6e65cf31116b1cc02f Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 23 Aug 2024 14:30:20 +0300 Subject: [PATCH 5/8] repair --- bindings/binding_core_node/src/minify.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index 251fe4a377e2..9c54eb04e461 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -20,7 +20,7 @@ use crate::{get_compiler, util::try_with}; struct MinifyTask { c: Arc, code: String, - options: String, + options: JsMinifyOptions, } #[derive(Deserialize)] @@ -58,9 +58,9 @@ impl Task for MinifyTask { fn compute(&mut self) -> napi::Result { let input: MinifyTarget = deserialize_json(&self.code)?; - let options: JsMinifyOptions = deserialize_json(&self.options)?; + let options = self.options.clone(); - try_with(self.c.cm.clone(), false, ErrorFormat::Normal, |handler| { + try_with(self.c.cm.clone(), false, options.error_format.unwrap_or(ErrorFormat::Normal), |handler| { let fm = input.to_file(self.c.cm.clone()); self.c.minify(fm, handler, &options) @@ -77,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(); @@ -95,7 +96,7 @@ pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result let fm = code.to_file(c.cm.clone()); - try_with(c.cm.clone(), false, options.error_format, |handler| { + try_with(c.cm.clone(), false, options.error_format.unwrap_or(ErrorFormat::Normal), |handler| { c.minify(fm, handler, &options) }) .convert_err() From 89bd60923137f43f23e6528fcb2cd03fa3a29ece Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 23 Aug 2024 14:35:38 +0300 Subject: [PATCH 6/8] repair --- bindings/binding_core_node/src/minify.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index 9c54eb04e461..e920df296159 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -59,8 +59,9 @@ impl Task for MinifyTask { fn compute(&mut self) -> napi::Result { let input: MinifyTarget = deserialize_json(&self.code)?; let options = self.options.clone(); + let error_format = options.format.error_format.unwrap_or(ErrorFormat::Normal); - try_with(self.c.cm.clone(), false, options.error_format.unwrap_or(ErrorFormat::Normal), |handler| { + try_with(self.c.cm.clone(), false, error_format, |handler| { let fm = input.to_file(self.c.cm.clone()); self.c.minify(fm, handler, &options) @@ -94,9 +95,11 @@ pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result let options: JsMinifyOptions = deserialize_json(&String::from_utf8_lossy(opts.as_ref()).to_string())?; let c = get_compiler(); + let error_format = options.format.error_format.unwrap_or(ErrorFormat::Normal); + let fm = code.to_file(c.cm.clone()); - try_with(c.cm.clone(), false, options.error_format.unwrap_or(ErrorFormat::Normal), |handler| { + try_with(c.cm.clone(), false, error_format, |handler| { c.minify(fm, handler, &options) }) .convert_err() From 7c3980b363a5c84e99b1d521c16f910b9790b5c8 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Fri, 23 Aug 2024 14:42:51 +0300 Subject: [PATCH 7/8] repair --- bindings/binding_core_node/src/minify.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index e920df296159..bebffe6065a6 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -8,7 +8,7 @@ use napi::{ use serde::Deserialize; use swc_core::{ base::{ - config::{ErrorFormat, JsMinifyOptions}, + config::{JsMinifyOptions}, TransformOutput, }, common::{collections::AHashMap, sync::Lrc, FileName, SourceFile, SourceMap}, @@ -59,9 +59,8 @@ impl Task for MinifyTask { fn compute(&mut self) -> napi::Result { let input: MinifyTarget = deserialize_json(&self.code)?; let options = self.options.clone(); - let error_format = options.format.error_format.unwrap_or(ErrorFormat::Normal); - try_with(self.c.cm.clone(), false, error_format, |handler| { + 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) @@ -95,11 +94,9 @@ pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result let options: JsMinifyOptions = deserialize_json(&String::from_utf8_lossy(opts.as_ref()).to_string())?; let c = get_compiler(); - let error_format = options.format.error_format.unwrap_or(ErrorFormat::Normal); - let fm = code.to_file(c.cm.clone()); - try_with(c.cm.clone(), false, error_format, |handler| { + try_with(c.cm.clone(), false, Default::default(), |handler| { c.minify(fm, handler, &options) }) .convert_err() From 5ff46345f73a1f3d0dc8a8d825bc83c6bc387ead Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 27 Aug 2024 12:43:08 +0300 Subject: [PATCH 8/8] fix: type --- bindings/binding_core_node/src/minify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/binding_core_node/src/minify.rs b/bindings/binding_core_node/src/minify.rs index bebffe6065a6..244239611233 100644 --- a/bindings/binding_core_node/src/minify.rs +++ b/bindings/binding_core_node/src/minify.rs @@ -20,7 +20,7 @@ use crate::{get_compiler, util::try_with}; struct MinifyTask { c: Arc, code: String, - options: JsMinifyOptions, + options: String, } #[derive(Deserialize)]