diff --git a/packages/next-swc/crates/napi/src/minify.rs b/packages/next-swc/crates/napi/src/minify.rs index 17d08840765d1..349fee4c76e3f 100644 --- a/packages/next-swc/crates/napi/src/minify.rs +++ b/packages/next-swc/crates/napi/src/minify.rs @@ -35,7 +35,7 @@ use serde::Deserialize; use std::sync::Arc; use swc_core::{ base::{config::JsMinifyOptions, try_with_handler, TransformOutput}, - common::{errors::ColorConfig, sync::Lrc, FileName, SourceFile, SourceMap, GLOBALS}, + common::{errors::ColorConfig, sync::Lrc, FileName, SourceFile, SourceMap}, }; struct MinifyTask { @@ -85,17 +85,15 @@ impl Task for MinifyTask { skip_filename: true, }, |handler| { - GLOBALS.set(&Default::default(), || { - let fm = self.code.to_file(self.c.cm.clone()); - - self.c.minify( - fm, - handler, - &JsMinifyOptions { - ..self.opts.clone() - }, - ) - }) + let fm = self.code.to_file(self.c.cm.clone()); + + self.c.minify( + fm, + handler, + &JsMinifyOptions { + ..self.opts.clone() + }, + ) }, ) .convert_err() @@ -133,7 +131,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result { color: ColorConfig::Never, skip_filename: true, }, - |handler| GLOBALS.set(&Default::default(), || c.minify(fm, handler, &opts)), + |handler| c.minify(fm, handler, &opts), ) .convert_err()?; diff --git a/packages/next-swc/crates/napi/src/parse.rs b/packages/next-swc/crates/napi/src/parse.rs index 082b5dc85f23b..4119b3e3dc9bf 100644 --- a/packages/next-swc/crates/napi/src/parse.rs +++ b/packages/next-swc/crates/napi/src/parse.rs @@ -4,9 +4,7 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task}; use std::sync::Arc; use swc_core::{ base::{config::ParseOptions, try_with_handler}, - common::{ - comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS, - }, + common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap}, }; pub struct ParseTask { @@ -24,44 +22,41 @@ impl Task for ParseTask { type JsValue = JsString; fn compute(&mut self) -> napi::Result { - GLOBALS.set(&Default::default(), || { - let c = - swc_core::base::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty()))); + let c = swc_core::base::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty()))); - let options: ParseOptions = deserialize_json(&self.options).convert_err()?; - let comments = c.comments().clone(); - let comments: Option<&dyn Comments> = if options.comments { - Some(&comments) - } else { - None - }; - let fm = - c.cm.new_source_file(self.filename.clone(), self.src.clone()); - let program = try_with_handler( - c.cm.clone(), - swc_core::base::HandlerOpts { - color: ColorConfig::Never, - skip_filename: false, - }, - |handler| { - c.parse_js( - fm, - handler, - options.target, - options.syntax, - options.is_module, - comments, - ) - }, - ) - .convert_err()?; + let options: ParseOptions = deserialize_json(&self.options).convert_err()?; + let comments = c.comments().clone(); + let comments: Option<&dyn Comments> = if options.comments { + Some(&comments) + } else { + None + }; + let fm = + c.cm.new_source_file(self.filename.clone(), self.src.clone()); + let program = try_with_handler( + c.cm.clone(), + swc_core::base::HandlerOpts { + color: ColorConfig::Never, + skip_filename: false, + }, + |handler| { + c.parse_js( + fm, + handler, + options.target, + options.syntax, + options.is_module, + comments, + ) + }, + ) + .convert_err()?; - let ast_json = serde_json::to_string(&program) - .context("failed to serialize Program") - .convert_err()?; + let ast_json = serde_json::to_string(&program) + .context("failed to serialize Program") + .convert_err()?; - Ok(ast_json) - }) + Ok(ast_json) } fn resolve(self, env: Env, result: Self::Output) -> napi::Result { diff --git a/packages/next-swc/crates/napi/src/transform.rs b/packages/next-swc/crates/napi/src/transform.rs index 1b8833431a9c8..25c4bfba747d3 100644 --- a/packages/next-swc/crates/napi/src/transform.rs +++ b/packages/next-swc/crates/napi/src/transform.rs @@ -44,7 +44,7 @@ use std::{ }; use swc_core::{ base::{try_with_handler, Compiler, TransformOutput}, - common::{errors::ColorConfig, FileName, GLOBALS}, + common::{errors::ColorConfig, FileName}, ecma::transforms::base::pass::noop, }; @@ -68,85 +68,83 @@ impl Task for TransformTask { type JsValue = JsObject; fn compute(&mut self) -> napi::Result { - GLOBALS.set(&Default::default(), || { - let eliminated_packages: Rc>> = Default::default(); - let res = catch_unwind(AssertUnwindSafe(|| { - try_with_handler( - self.c.cm.clone(), - swc_core::base::HandlerOpts { - color: ColorConfig::Never, - skip_filename: true, - }, - |handler| { - self.c.run(|| { - let options: TransformOptions = deserialize_json(&self.options)?; - let fm = match &self.input { - Input::Source { src } => { - let filename = if options.swc.filename.is_empty() { - FileName::Anon - } else { - FileName::Real(options.swc.filename.clone().into()) - }; - - self.c.cm.new_source_file(filename, src.to_string()) + let eliminated_packages: Rc>> = Default::default(); + let res = catch_unwind(AssertUnwindSafe(|| { + try_with_handler( + self.c.cm.clone(), + swc_core::base::HandlerOpts { + color: ColorConfig::Never, + skip_filename: true, + }, + |handler| { + self.c.run(|| { + let options: TransformOptions = deserialize_json(&self.options)?; + let fm = match &self.input { + Input::Source { src } => { + let filename = if options.swc.filename.is_empty() { + FileName::Anon + } else { + FileName::Real(options.swc.filename.clone().into()) + }; + + self.c.cm.new_source_file(filename, src.to_string()) + } + Input::FromFilename => { + let filename = &options.swc.filename; + if filename.is_empty() { + bail!("no filename is provided via options"); } - Input::FromFilename => { - let filename = &options.swc.filename; - if filename.is_empty() { - bail!("no filename is provided via options"); - } - - self.c.cm.new_source_file( - FileName::Real(filename.into()), - read_to_string(filename).with_context(|| { - format!("Failed to read source code from {}", filename) - })?, - ) - } - }; - let options = options.patch(&fm); - - let cm = self.c.cm.clone(); - let file = fm.clone(); - - self.c.process_js_with_custom_pass( - fm, - None, - handler, - &options.swc, - |_, comments| { - custom_before_pass( - cm, - file, - &options, - comments.clone(), - eliminated_packages.clone(), - ) - }, - |_, _| noop(), - ) - }) - }, - ) - })) - .map_err(|err| { - if let Some(s) = err.downcast_ref::() { - anyhow!("failed to process {}", s) - } else { - anyhow!("failed to process") - } - }); - - match res { - Ok(res) => res - .map(|o| (o, eliminated_packages.replace(Default::default()))) - .convert_err(), - Err(err) => Err(napi::Error::new( - Status::GenericFailure, - format!("{:?}", err), - )), + + self.c.cm.new_source_file( + FileName::Real(filename.into()), + read_to_string(filename).with_context(|| { + format!("Failed to read source code from {}", filename) + })?, + ) + } + }; + let options = options.patch(&fm); + + let cm = self.c.cm.clone(); + let file = fm.clone(); + + self.c.process_js_with_custom_pass( + fm, + None, + handler, + &options.swc, + |_, comments| { + custom_before_pass( + cm, + file, + &options, + comments.clone(), + eliminated_packages.clone(), + ) + }, + |_, _| noop(), + ) + }) + }, + ) + })) + .map_err(|err| { + if let Some(s) = err.downcast_ref::() { + anyhow!("failed to process {}", s) + } else { + anyhow!("failed to process") } - }) + }); + + match res { + Ok(res) => res + .map(|o| (o, eliminated_packages.replace(Default::default()))) + .convert_err(), + Err(err) => Err(napi::Error::new( + Status::GenericFailure, + format!("{:?}", err), + )), + } } fn resolve(