Commit fbf1bd11 authored by Donny/강동윤's avatar Donny/강동윤

Fix wasm

parent 082e766e
...@@ -8,7 +8,9 @@ use wasm_bindgen_futures::future_to_promise; ...@@ -8,7 +8,9 @@ use wasm_bindgen_futures::future_to_promise;
use swc_core::{ use swc_core::{
base::{config::JsMinifyOptions, config::ParseOptions, try_with_handler, Compiler}, base::{config::JsMinifyOptions, config::ParseOptions, try_with_handler, Compiler},
common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap}, common::{
comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS,
},
ecma::transforms::base::pass::noop, ecma::transforms::base::pass::noop,
}; };
...@@ -29,14 +31,16 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> { ...@@ -29,14 +31,16 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
skip_filename: false, skip_filename: false,
}, },
|handler| { |handler| {
let opts: JsMinifyOptions = opts.into_serde().context("failed to parse options")?; GLOBALS.set(&Default::default(), || {
let opts: JsMinifyOptions = opts.into_serde().context("failed to parse options")?;
let fm = c.cm.new_source_file(FileName::Anon, s.into()); let fm = c.cm.new_source_file(FileName::Anon, s.into());
let program = c let program = c
.minify(fm, handler, &opts) .minify(fm, handler, &opts)
.context("failed to minify file")?; .context("failed to minify file")?;
JsValue::from_serde(&program).context("failed to serialize json") JsValue::from_serde(&program).context("failed to serialize json")
})
}, },
) )
.map_err(convert_err) .map_err(convert_err)
...@@ -62,43 +66,46 @@ pub fn transform_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> { ...@@ -62,43 +66,46 @@ pub fn transform_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
skip_filename: false, skip_filename: false,
}, },
|handler| { |handler| {
let opts: TransformOptions = opts.into_serde().context("failed to parse options")?; GLOBALS.set(&Default::default(), || {
let opts: TransformOptions =
let s = s.dyn_into::<js_sys::JsString>(); opts.into_serde().context("failed to parse options")?;
let out = match s {
Ok(s) => { let s = s.dyn_into::<js_sys::JsString>();
let fm = c.cm.new_source_file( let out = match s {
if opts.swc.filename.is_empty() { Ok(s) => {
FileName::Anon let fm = c.cm.new_source_file(
} else { if opts.swc.filename.is_empty() {
FileName::Real(opts.swc.filename.clone().into()) FileName::Anon
}, } else {
s.into(), FileName::Real(opts.swc.filename.clone().into())
); },
let cm = c.cm.clone(); s.into(),
let file = fm.clone(); );
c.process_js_with_custom_pass( let cm = c.cm.clone();
fm, let file = fm.clone();
None, c.process_js_with_custom_pass(
handler, fm,
&opts.swc, None,
|_, comments| { handler,
custom_before_pass( &opts.swc,
cm, |_, comments| {
file, custom_before_pass(
&opts, cm,
comments.clone(), file,
Default::default(), &opts,
) comments.clone(),
}, Default::default(),
|_, _| noop(), )
) },
.context("failed to process js file")? |_, _| noop(),
} )
Err(v) => c.process_js(handler, v.into_serde().expect(""), &opts.swc)?, .context("failed to process js file")?
}; }
Err(v) => c.process_js(handler, v.into_serde().expect(""), &opts.swc)?,
JsValue::from_serde(&out).context("failed to serialize json") };
JsValue::from_serde(&out).context("failed to serialize json")
})
}, },
) )
.map_err(convert_err) .map_err(convert_err)
...@@ -124,30 +131,33 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> { ...@@ -124,30 +131,33 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
}, },
|handler| { |handler| {
c.run(|| { c.run(|| {
let opts: ParseOptions = opts.into_serde().context("failed to parse options")?; GLOBALS.set(&Default::default(), || {
let opts: ParseOptions =
let fm = c.cm.new_source_file(FileName::Anon, s.into()); opts.into_serde().context("failed to parse options")?;
let cmts = c.comments().clone(); let fm = c.cm.new_source_file(FileName::Anon, s.into());
let comments = if opts.comments {
Some(&cmts as &dyn Comments) let cmts = c.comments().clone();
} else { let comments = if opts.comments {
None Some(&cmts as &dyn Comments)
}; } else {
None
let program = c };
.parse_js(
fm, let program = c
handler, .parse_js(
opts.target, fm,
opts.syntax, handler,
opts.is_module, opts.target,
comments, opts.syntax,
) opts.is_module,
.context("failed to parse code")?; comments,
)
let s = serde_json::to_string(&program).unwrap(); .context("failed to parse code")?;
Ok(JsValue::from_str(&s))
let s = serde_json::to_string(&program).unwrap();
Ok(JsValue::from_str(&s))
})
}) })
}, },
) )
......
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