Skip to content

Commit

Permalink
feat: make error format configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mert Can Altin committed Aug 21, 2024
1 parent b0b5e36 commit 76db547
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions bindings/binding_core_node/src/minify.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -19,7 +20,7 @@ use crate::{get_compiler, util::try_with};
struct MinifyTask {
c: Arc<swc_core::base::Compiler>,
code: String,
options: String,
options: JsMinifyOptions,
}

#[derive(Deserialize)]
Expand All @@ -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();
Expand All @@ -57,7 +58,7 @@ impl Task for MinifyTask {

fn compute(&mut self) -> napi::Result<Self::Output> {
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| {
let fm = input.to_file(self.c.cm.clone());
Expand All @@ -67,7 +68,7 @@ impl Task for MinifyTask {
.convert_err()
}

fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> napi::Result<Self::JsValue> {
fn resolve(&mut self, _env: Env, output: Self::Output) -> napi::Result<Self::JsValue> {
Ok(output)
}
}
Expand All @@ -76,7 +77,8 @@ impl Task for MinifyTask {
fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<MinifyTask> {
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();

Expand All @@ -89,18 +91,15 @@ fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<
pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
crate::util::init_default_trace_subscriber();
let code: MinifyTarget = get_deserialized(code)?;
let opts = get_deserialized(opts)?;
let opts_str = String::from_utf8_lossy(opts.as_ref()).to_string();
let opts: JsMinifyOptions = deserialize_json(&opts_str)?;

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, ErrorFormat::Normal, |handler| {
c.minify(fm, handler, &opts)
})
.convert_err()
}

0 comments on commit 76db547

Please sign in to comment.