Skip to content

Commit

Permalink
Revert "fix(next-swc): Provide tokio context required for WASM plugins (
Browse files Browse the repository at this point in the history
#62441)"

This reverts commit cea5366.
  • Loading branch information
kdy1 committed Mar 8, 2024
1 parent e3a93a5 commit b5f7539
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions packages/next-swc/crates/napi/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,20 @@ pub enum Input {
pub struct TransformTask {
pub c: Arc<Compiler>,
pub input: Input,
pub options: Vec<u8>,
pub options: Buffer,
}

#[inline]
fn skip_filename() -> bool {
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 {
type Output = (TransformOutput, FxHashSet<String>);
type JsValue = Object;

fn compute(&mut self) -> Result<Self::Output> {
run_in_context(|| {
fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || {
let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default();
let res = catch_unwind(AssertUnwindSafe(|| {
try_with_handler(
Expand Down Expand Up @@ -168,18 +161,18 @@ impl Task for TransformTask {
&mut self,
env: Env,
(output, eliminated_packages): Self::Output,
) -> Result<Self::JsValue> {
) -> napi::Result<Self::JsValue> {
complete_output(&env, output, eliminated_packages)
}
}

#[napi]
pub fn transform(
env: Env,
src: Either3<String, Buffer, Undefined>,
_is_module: bool,
options: Buffer,
) -> Result<Object> {
signal: Option<AbortSignal>,
) -> napi::Result<AsyncTask<TransformTask>> {
let c = get_compiler();

let input = match src {
Expand All @@ -190,14 +183,8 @@ pub fn transform(
Either3::C(_) => Input::FromFilename,
};

let options = options.to_vec();

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),
)
let task = TransformTask { c, input, options };
Ok(AsyncTask::with_optional_signal(task, signal))
}

#[napi]
Expand All @@ -206,7 +193,7 @@ pub fn transform_sync(
src: Either3<String, Buffer, Undefined>,
_is_module: bool,
options: Buffer,
) -> Result<Object> {
) -> napi::Result<Object> {
let c = get_compiler();

let input = match src {
Expand All @@ -217,12 +204,8 @@ pub fn transform_sync(
Either3::C(_) => Input::FromFilename,
};

let options = options.to_vec();

let mut task = TransformTask { c, input, options };

let output = block_on(async { task.compute() })?;

let output = task.compute()?;
task.resolve(env, output)
}
#[test]
Expand Down

0 comments on commit b5f7539

Please sign in to comment.