Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make error format configurable #9441

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 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 Down Expand Up @@ -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 @@ -59,15 +60,18 @@ 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| {
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
napi::Error::from_reason(format!("Minification failed: {}", err))
})
.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 @@ -89,18 +93,12 @@ 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 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, opts.error_format, |handler| {
c.minify(fm, handler, &opts)
})
.convert_err()
}
Loading