Unverified Commit 3ee42236 authored by LongYinan's avatar LongYinan Committed by GitHub

Align behavior of SWC transform & transformSync (#36500)

fixes https://github.com/vercel/next.js/issues/35661



## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 9168b386
...@@ -30,7 +30,7 @@ use crate::{ ...@@ -30,7 +30,7 @@ use crate::{
complete_output, get_compiler, complete_output, get_compiler,
util::{deserialize_json, CtxtExt, MapErr}, util::{deserialize_json, CtxtExt, MapErr},
}; };
use anyhow::{anyhow, bail, Context as _, Error}; use anyhow::{anyhow, bail, Context as _};
use fxhash::FxHashSet; use fxhash::FxHashSet;
use napi::{CallContext, Env, JsBoolean, JsBuffer, JsObject, JsString, JsUnknown, Status, Task}; use napi::{CallContext, Env, JsBoolean, JsBuffer, JsObject, JsString, JsUnknown, Status, Task};
use next_swc::{custom_before_pass, TransformOptions}; use next_swc::{custom_before_pass, TransformOptions};
...@@ -43,8 +43,7 @@ use std::{ ...@@ -43,8 +43,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use swc::{try_with_handler, Compiler, TransformOutput}; use swc::{try_with_handler, Compiler, TransformOutput};
use swc_common::{errors::ColorConfig, FileName, SourceFile}; use swc_common::{errors::ColorConfig, FileName};
use swc_ecmascript::ast::Program;
use swc_ecmascript::transforms::pass::noop; use swc_ecmascript::transforms::pass::noop;
/// Input to transform /// Input to transform
...@@ -156,11 +155,11 @@ impl Task for TransformTask { ...@@ -156,11 +155,11 @@ impl Task for TransformTask {
} }
/// returns `compiler, (src / path), options, plugin, callback` /// returns `compiler, (src / path), options, plugin, callback`
pub fn schedule_transform<F>(cx: CallContext, op: F) -> napi::Result<JsObject> pub fn schedule_transform<F>(cx: &CallContext, op: F) -> napi::Result<TransformTask>
where where
F: FnOnce(&Arc<Compiler>, Input, bool, String) -> TransformTask, F: FnOnce(&Arc<Compiler>, Input, bool, String) -> TransformTask,
{ {
let c = get_compiler(&cx); let c = get_compiler(cx);
let unknown_src = cx.get::<JsUnknown>(0)?; let unknown_src = cx.get::<JsUnknown>(0)?;
let src = match unknown_src.get_type()? { let src = match unknown_src.get_type()? {
...@@ -183,68 +182,28 @@ where ...@@ -183,68 +182,28 @@ where
let is_module = cx.get::<JsBoolean>(1)?; let is_module = cx.get::<JsBoolean>(1)?;
let options = cx.get_buffer_as_string(2)?; let options = cx.get_buffer_as_string(2)?;
let task = op(&c, src, is_module.get_value()?, options); Ok(op(&c, src, is_module.get_value()?, options))
cx.env.spawn(task).map(|t| t.promise_object())
}
pub fn exec_transform<F>(cx: CallContext, op: F) -> napi::Result<JsObject>
where
F: FnOnce(&Compiler, String, &TransformOptions) -> Result<Arc<SourceFile>, Error>,
{
let c = get_compiler(&cx);
let s = cx.get::<JsString>(0)?.into_utf8()?;
let is_module = cx.get::<JsBoolean>(1)?;
let mut options: TransformOptions = cx.get_deserialized(2)?;
options.swc.swcrc = false;
let output = try_with_handler(
c.cm.clone(),
swc::HandlerOpts {
color: ColorConfig::Never,
skip_filename: true,
},
|handler| {
c.run(|| {
if is_module.get_value()? {
let program: Program = serde_json::from_str(s.as_str()?)
.context("failed to deserialize Program")?;
c.process_js(handler, program, &options.swc)
} else {
let fm =
op(&c, s.as_str()?.to_string(), &options).context("failed to load file")?;
c.process_js_file(fm, handler, &options.swc)
}
})
},
)
.convert_err()?;
complete_output(cx.env, output, Default::default())
} }
#[js_function(4)] #[js_function(4)]
pub fn transform(cx: CallContext) -> napi::Result<JsObject> { pub fn transform(cx: CallContext) -> napi::Result<JsObject> {
schedule_transform(cx, |c, input, _, options| TransformTask { let task = schedule_transform(&cx, |c, input, _, options| TransformTask {
c: c.clone(), c: c.clone(),
input, input,
options, options,
}) })?;
cx.env.spawn(task).map(|handle| handle.promise_object())
} }
#[js_function(4)] #[js_function(4)]
pub fn transform_sync(cx: CallContext) -> napi::Result<JsObject> { pub fn transform_sync(cx: CallContext) -> napi::Result<JsObject> {
exec_transform(cx, |c, src, options| { let mut task = schedule_transform(&cx, |c, input, _, options| TransformTask {
Ok(c.cm.new_source_file( c: c.clone(),
if options.swc.filename.is_empty() { input,
FileName::Anon options,
} else { })?;
FileName::Real(options.swc.filename.clone().into()) let output = task.compute()?;
}, task.resolve(*cx.env, output)
src,
))
})
} }
#[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