Commit 58d47361 authored by Tim Neutkens's avatar Tim Neutkens

Revert "Fix"

This reverts commit 112589e2.
parent cafb5ff8
...@@ -35,7 +35,7 @@ use serde::Deserialize; ...@@ -35,7 +35,7 @@ use serde::Deserialize;
use std::sync::Arc; use std::sync::Arc;
use swc_core::{ use swc_core::{
base::{config::JsMinifyOptions, try_with_handler, TransformOutput}, 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 { struct MinifyTask {
...@@ -85,17 +85,15 @@ impl Task for MinifyTask { ...@@ -85,17 +85,15 @@ impl Task for MinifyTask {
skip_filename: true, skip_filename: true,
}, },
|handler| { |handler| {
GLOBALS.set(&Default::default(), || { let fm = self.code.to_file(self.c.cm.clone());
let fm = self.code.to_file(self.c.cm.clone());
self.c.minify(
self.c.minify( fm,
fm, handler,
handler, &JsMinifyOptions {
&JsMinifyOptions { ..self.opts.clone()
..self.opts.clone() },
}, )
)
})
}, },
) )
.convert_err() .convert_err()
...@@ -133,7 +131,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> { ...@@ -133,7 +131,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> {
color: ColorConfig::Never, color: ColorConfig::Never,
skip_filename: true, skip_filename: true,
}, },
|handler| GLOBALS.set(&Default::default(), || c.minify(fm, handler, &opts)), |handler| c.minify(fm, handler, &opts),
) )
.convert_err()?; .convert_err()?;
......
...@@ -4,9 +4,7 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task}; ...@@ -4,9 +4,7 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task};
use std::sync::Arc; use std::sync::Arc;
use swc_core::{ use swc_core::{
base::{config::ParseOptions, try_with_handler}, base::{config::ParseOptions, try_with_handler},
common::{ common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap},
comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS,
},
}; };
pub struct ParseTask { pub struct ParseTask {
...@@ -24,44 +22,41 @@ impl Task for ParseTask { ...@@ -24,44 +22,41 @@ impl Task for ParseTask {
type JsValue = JsString; type JsValue = JsString;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> napi::Result<Self::Output> {
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 options: ParseOptions = deserialize_json(&self.options).convert_err()?;
let comments = c.comments().clone(); let comments = c.comments().clone();
let comments: Option<&dyn Comments> = if options.comments { let comments: Option<&dyn Comments> = if options.comments {
Some(&comments) Some(&comments)
} else { } else {
None None
}; };
let fm = let fm =
c.cm.new_source_file(self.filename.clone(), self.src.clone()); c.cm.new_source_file(self.filename.clone(), self.src.clone());
let program = try_with_handler( let program = try_with_handler(
c.cm.clone(), c.cm.clone(),
swc_core::base::HandlerOpts { swc_core::base::HandlerOpts {
color: ColorConfig::Never, color: ColorConfig::Never,
skip_filename: false, skip_filename: false,
}, },
|handler| { |handler| {
c.parse_js( c.parse_js(
fm, fm,
handler, handler,
options.target, options.target,
options.syntax, options.syntax,
options.is_module, options.is_module,
comments, comments,
) )
}, },
) )
.convert_err()?; .convert_err()?;
let ast_json = serde_json::to_string(&program) let ast_json = serde_json::to_string(&program)
.context("failed to serialize Program") .context("failed to serialize Program")
.convert_err()?; .convert_err()?;
Ok(ast_json) Ok(ast_json)
})
} }
fn resolve(self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> { fn resolve(self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
......
...@@ -44,7 +44,7 @@ use std::{ ...@@ -44,7 +44,7 @@ use std::{
}; };
use swc_core::{ use swc_core::{
base::{try_with_handler, Compiler, TransformOutput}, base::{try_with_handler, Compiler, TransformOutput},
common::{errors::ColorConfig, FileName, GLOBALS}, common::{errors::ColorConfig, FileName},
ecma::transforms::base::pass::noop, ecma::transforms::base::pass::noop,
}; };
...@@ -68,85 +68,83 @@ impl Task for TransformTask { ...@@ -68,85 +68,83 @@ impl Task for TransformTask {
type JsValue = JsObject; type JsValue = JsObject;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || { let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default();
let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default(); let res = catch_unwind(AssertUnwindSafe(|| {
let res = catch_unwind(AssertUnwindSafe(|| { try_with_handler(
try_with_handler( self.c.cm.clone(),
self.c.cm.clone(), swc_core::base::HandlerOpts {
swc_core::base::HandlerOpts { color: ColorConfig::Never,
color: ColorConfig::Never, skip_filename: true,
skip_filename: true, },
}, |handler| {
|handler| { self.c.run(|| {
self.c.run(|| { let options: TransformOptions = deserialize_json(&self.options)?;
let options: TransformOptions = deserialize_json(&self.options)?; let fm = match &self.input {
let fm = match &self.input { Input::Source { src } => {
Input::Source { src } => { let filename = if options.swc.filename.is_empty() {
let filename = if options.swc.filename.is_empty() { FileName::Anon
FileName::Anon } else {
} else { FileName::Real(options.swc.filename.clone().into())
FileName::Real(options.swc.filename.clone().into()) };
};
self.c.cm.new_source_file(filename, src.to_string())
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; self.c.cm.new_source_file(
if filename.is_empty() { FileName::Real(filename.into()),
bail!("no filename is provided via options"); read_to_string(filename).with_context(|| {
} format!("Failed to read source code from {}", filename)
})?,
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();
};
let options = options.patch(&fm); self.c.process_js_with_custom_pass(
fm,
let cm = self.c.cm.clone(); None,
let file = fm.clone(); handler,
&options.swc,
self.c.process_js_with_custom_pass( |_, comments| {
fm, custom_before_pass(
None, cm,
handler, file,
&options.swc, &options,
|_, comments| { comments.clone(),
custom_before_pass( eliminated_packages.clone(),
cm, )
file, },
&options, |_, _| noop(),
comments.clone(), )
eliminated_packages.clone(), })
) },
}, )
|_, _| noop(), }))
) .map_err(|err| {
}) if let Some(s) = err.downcast_ref::<String>() {
}, anyhow!("failed to process {}", s)
) } else {
})) anyhow!("failed to process")
.map_err(|err| {
if let Some(s) = err.downcast_ref::<String>() {
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),
)),
} }
}) });
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( fn resolve(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment