Commit 112589e2 authored by Donny/강동윤's avatar Donny/강동윤

Fix

parent fbf1bd11
...@@ -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}, common::{errors::ColorConfig, sync::Lrc, FileName, SourceFile, SourceMap, GLOBALS},
}; };
struct MinifyTask { struct MinifyTask {
...@@ -85,15 +85,17 @@ impl Task for MinifyTask { ...@@ -85,15 +85,17 @@ impl Task for MinifyTask {
skip_filename: true, skip_filename: true,
}, },
|handler| { |handler| {
let fm = self.code.to_file(self.c.cm.clone()); GLOBALS.set(&Default::default(), || {
let fm = self.code.to_file(self.c.cm.clone());
self.c.minify(
fm, self.c.minify(
handler, fm,
&JsMinifyOptions { handler,
..self.opts.clone() &JsMinifyOptions {
}, ..self.opts.clone()
) },
)
})
}, },
) )
.convert_err() .convert_err()
...@@ -131,7 +133,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> { ...@@ -131,7 +133,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> {
color: ColorConfig::Never, color: ColorConfig::Never,
skip_filename: true, skip_filename: true,
}, },
|handler| c.minify(fm, handler, &opts), |handler| GLOBALS.set(&Default::default(), || c.minify(fm, handler, &opts)),
) )
.convert_err()?; .convert_err()?;
......
...@@ -4,7 +4,9 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task}; ...@@ -4,7 +4,9 @@ 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::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap}, common::{
comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS,
},
}; };
pub struct ParseTask { pub struct ParseTask {
...@@ -22,41 +24,44 @@ impl Task for ParseTask { ...@@ -22,41 +24,44 @@ 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> {
let c = swc_core::base::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty()))); GLOBALS.set(&Default::default(), || {
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()?;
let ast_json = serde_json::to_string(&program)
.context("failed to serialize Program")
.convert_err()?; .convert_err()?;
Ok(ast_json) let ast_json = serde_json::to_string(&program)
.context("failed to serialize Program")
.convert_err()?;
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}, common::{errors::ColorConfig, FileName, GLOBALS},
ecma::transforms::base::pass::noop, ecma::transforms::base::pass::noop,
}; };
...@@ -68,83 +68,85 @@ impl Task for TransformTask { ...@@ -68,83 +68,85 @@ 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> {
let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default(); GLOBALS.set(&Default::default(), || {
let res = catch_unwind(AssertUnwindSafe(|| { let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default();
try_with_handler( let res = catch_unwind(AssertUnwindSafe(|| {
self.c.cm.clone(), try_with_handler(
swc_core::base::HandlerOpts { self.c.cm.clone(),
color: ColorConfig::Never, swc_core::base::HandlerOpts {
skip_filename: true, color: ColorConfig::Never,
}, skip_filename: true,
|handler| { },
self.c.run(|| { |handler| {
let options: TransformOptions = deserialize_json(&self.options)?; self.c.run(|| {
let fm = match &self.input { let options: TransformOptions = deserialize_json(&self.options)?;
Input::Source { src } => { let fm = match &self.input {
let filename = if options.swc.filename.is_empty() { Input::Source { src } => {
FileName::Anon let filename = if options.swc.filename.is_empty() {
} else { FileName::Anon
FileName::Real(options.swc.filename.clone().into()) } else {
}; 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 => {
self.c.cm.new_source_file( let filename = &options.swc.filename;
FileName::Real(filename.into()), if filename.is_empty() {
read_to_string(filename).with_context(|| { bail!("no filename is provided via options");
format!("Failed to read source code from {}", filename) }
})?,
) self.c.cm.new_source_file(
} FileName::Real(filename.into()),
}; read_to_string(filename).with_context(|| {
let options = options.patch(&fm); format!("Failed to read source code from {}", filename)
})?,
let cm = self.c.cm.clone(); )
let file = fm.clone(); }
};
self.c.process_js_with_custom_pass( let options = options.patch(&fm);
fm,
None, let cm = self.c.cm.clone();
handler, let file = fm.clone();
&options.swc,
|_, comments| { self.c.process_js_with_custom_pass(
custom_before_pass( fm,
cm, None,
file, handler,
&options, &options.swc,
comments.clone(), |_, comments| {
eliminated_packages.clone(), custom_before_pass(
) cm,
}, file,
|_, _| noop(), &options,
) 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