Unverified Commit cea5366c authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(next-swc): Provide tokio context required for WASM plugins (#62441)

### What?

Run transforms with the tokio context so Wasmer works properly.

### Why?

To allow invoking Wasm plugins. `wasmer` had a breaking change.

### How?



Closes PACK-2587
Closes #62081

---------
Co-authored-by: 's avatarhrmny <8845940+ForsakenHarmony@users.noreply.github.com>
parent 26de5ca2
...@@ -58,7 +58,7 @@ pub enum Input { ...@@ -58,7 +58,7 @@ pub enum Input {
pub struct TransformTask { pub struct TransformTask {
pub c: Arc<Compiler>, pub c: Arc<Compiler>,
pub input: Input, pub input: Input,
pub options: Buffer, pub options: Vec<u8>,
} }
#[inline] #[inline]
...@@ -66,12 +66,19 @@ fn skip_filename() -> bool { ...@@ -66,12 +66,19 @@ fn skip_filename() -> bool {
cfg!(debug_assertions) cfg!(debug_assertions)
} }
fn run_in_context<F, Ret>(op: F) -> Ret
where
F: FnOnce() -> Ret,
{
GLOBALS.set(&Default::default(), op)
}
impl Task for TransformTask { impl Task for TransformTask {
type Output = (TransformOutput, FxHashSet<String>); type Output = (TransformOutput, FxHashSet<String>);
type JsValue = Object; type JsValue = Object;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> Result<Self::Output> {
GLOBALS.set(&Default::default(), || { run_in_context(|| {
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(
...@@ -161,18 +168,18 @@ impl Task for TransformTask { ...@@ -161,18 +168,18 @@ impl Task for TransformTask {
&mut self, &mut self,
env: Env, env: Env,
(output, eliminated_packages): Self::Output, (output, eliminated_packages): Self::Output,
) -> napi::Result<Self::JsValue> { ) -> Result<Self::JsValue> {
complete_output(&env, output, eliminated_packages) complete_output(&env, output, eliminated_packages)
} }
} }
#[napi] #[napi]
pub fn transform( pub fn transform(
env: Env,
src: Either3<String, Buffer, Undefined>, src: Either3<String, Buffer, Undefined>,
_is_module: bool, _is_module: bool,
options: Buffer, options: Buffer,
signal: Option<AbortSignal>, ) -> Result<Object> {
) -> napi::Result<AsyncTask<TransformTask>> {
let c = get_compiler(); let c = get_compiler();
let input = match src { let input = match src {
...@@ -183,8 +190,14 @@ pub fn transform( ...@@ -183,8 +190,14 @@ pub fn transform(
Either3::C(_) => Input::FromFilename, Either3::C(_) => Input::FromFilename,
}; };
let task = TransformTask { c, input, options }; let options = options.to_vec();
Ok(AsyncTask::with_optional_signal(task, signal))
let mut task = TransformTask { c, input, options };
env.execute_tokio_future(
async move { task.compute() },
|env, (output, eliminated_packages)| complete_output(env, output, eliminated_packages),
)
} }
#[napi] #[napi]
...@@ -193,7 +206,7 @@ pub fn transform_sync( ...@@ -193,7 +206,7 @@ pub fn transform_sync(
src: Either3<String, Buffer, Undefined>, src: Either3<String, Buffer, Undefined>,
_is_module: bool, _is_module: bool,
options: Buffer, options: Buffer,
) -> napi::Result<Object> { ) -> Result<Object> {
let c = get_compiler(); let c = get_compiler();
let input = match src { let input = match src {
...@@ -204,8 +217,12 @@ pub fn transform_sync( ...@@ -204,8 +217,12 @@ pub fn transform_sync(
Either3::C(_) => Input::FromFilename, Either3::C(_) => Input::FromFilename,
}; };
let options = options.to_vec();
let mut task = TransformTask { c, input, options }; let mut task = TransformTask { c, input, options };
let output = task.compute()?;
let output = block_on(async { task.compute() })?;
task.resolve(env, output) task.resolve(env, output)
} }
#[test] #[test]
......
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