Skip to content

Commit

Permalink
Revert "Fix wasm"
Browse files Browse the repository at this point in the history
This reverts commit fbf1bd1.
  • Loading branch information
timneutkens committed Sep 28, 2022
1 parent 62f996e commit cafb5ff
Showing 1 changed file with 68 additions and 78 deletions.
146 changes: 68 additions & 78 deletions packages/next-swc/crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use wasm_bindgen_futures::future_to_promise;

use swc_core::{
base::{config::JsMinifyOptions, config::ParseOptions, try_with_handler, Compiler},
common::{
comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS,
},
common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap},
ecma::transforms::base::pass::noop,
};

Expand All @@ -31,16 +29,14 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
skip_filename: false,
},
|handler| {
GLOBALS.set(&Default::default(), || {
let opts: JsMinifyOptions = opts.into_serde().context("failed to parse options")?;
let opts: JsMinifyOptions = opts.into_serde().context("failed to parse options")?;

let fm = c.cm.new_source_file(FileName::Anon, s.into());
let program = c
.minify(fm, handler, &opts)
.context("failed to minify file")?;
let fm = c.cm.new_source_file(FileName::Anon, s.into());
let program = c
.minify(fm, handler, &opts)
.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)
Expand All @@ -66,46 +62,43 @@ pub fn transform_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
skip_filename: false,
},
|handler| {
GLOBALS.set(&Default::default(), || {
let opts: TransformOptions =
opts.into_serde().context("failed to parse options")?;

let s = s.dyn_into::<js_sys::JsString>();
let out = match s {
Ok(s) => {
let fm = c.cm.new_source_file(
if opts.swc.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(opts.swc.filename.clone().into())
},
s.into(),
);
let cm = c.cm.clone();
let file = fm.clone();
c.process_js_with_custom_pass(
fm,
None,
handler,
&opts.swc,
|_, comments| {
custom_before_pass(
cm,
file,
&opts,
comments.clone(),
Default::default(),
)
},
|_, _| noop(),
)
.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")
})
let opts: TransformOptions = opts.into_serde().context("failed to parse options")?;

let s = s.dyn_into::<js_sys::JsString>();
let out = match s {
Ok(s) => {
let fm = c.cm.new_source_file(
if opts.swc.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(opts.swc.filename.clone().into())
},
s.into(),
);
let cm = c.cm.clone();
let file = fm.clone();
c.process_js_with_custom_pass(
fm,
None,
handler,
&opts.swc,
|_, comments| {
custom_before_pass(
cm,
file,
&opts,
comments.clone(),
Default::default(),
)
},
|_, _| noop(),
)
.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")
},
)
.map_err(convert_err)
Expand All @@ -131,33 +124,30 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
},
|handler| {
c.run(|| {
GLOBALS.set(&Default::default(), || {
let opts: ParseOptions =
opts.into_serde().context("failed to parse options")?;

let fm = c.cm.new_source_file(FileName::Anon, s.into());

let cmts = c.comments().clone();
let comments = if opts.comments {
Some(&cmts as &dyn Comments)
} else {
None
};

let program = c
.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
comments,
)
.context("failed to parse code")?;

let s = serde_json::to_string(&program).unwrap();
Ok(JsValue::from_str(&s))
})
let opts: ParseOptions = opts.into_serde().context("failed to parse options")?;

let fm = c.cm.new_source_file(FileName::Anon, s.into());

let cmts = c.comments().clone();
let comments = if opts.comments {
Some(&cmts as &dyn Comments)
} else {
None
};

let program = c
.parse_js(
fm,
handler,
opts.target,
opts.syntax,
opts.is_module,
comments,
)
.context("failed to parse code")?;

let s = serde_json::to_string(&program).unwrap();
Ok(JsValue::from_str(&s))
})
},
)
Expand Down

0 comments on commit cafb5ff

Please sign in to comment.