Skip to content

Commit

Permalink
pass Buffer directly to Rust (#30975)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Nov 5, 2021
1 parent bc19c2a commit 177e15b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if (!bindings) {
}

async function transform(src, options) {
const isModule = typeof src !== 'string'
const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {}

if (options?.jsc?.parser) {
Expand All @@ -65,7 +65,7 @@ async function transform(src, options) {
}

function transformSync(src, options) {
const isModule = typeof src !== 'string'
const isModule = typeof src !== 'string' && !Buffer.isBuffer(src)
options = options || {}

if (options?.jsc?.parser) {
Expand Down
21 changes: 19 additions & 2 deletions packages/next/build/swc/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use crate::{
TransformOptions,
};
use anyhow::{anyhow, Context as _, Error};
use napi::{CallContext, Env, JsBoolean, JsObject, JsString, Status, Task};
use napi::{CallContext, Env, JsBoolean, JsBuffer, JsObject, JsString, JsUnknown, Status, Task};
use std::{
convert::TryFrom,
panic::{catch_unwind, AssertUnwindSafe},
sync::Arc,
};
Expand Down Expand Up @@ -117,7 +118,23 @@ where
{
let c = get_compiler(&cx);

let src = cx.get::<JsString>(0)?.into_utf8()?.as_str()?.to_owned();
let unknown_src = cx.get::<JsUnknown>(0)?;
let src = match unknown_src.get_type()? {
napi::ValueType::String => napi::Result::Ok(
JsString::try_from(unknown_src)?
.into_utf8()?
.as_str()?
.to_owned(),
),
napi::ValueType::Object => napi::Result::Ok(
String::from_utf8_lossy(JsBuffer::try_from(unknown_src)?.into_value()?.as_ref())
.to_string(),
),
_ => Err(napi::Error::new(
Status::GenericFailure,
"first argument must be a String or Buffer".to_string(),
)),
}?;
let is_module = cx.get::<JsBoolean>(1)?;
let options = cx.get_buffer_as_string(2)?;

Expand Down
3 changes: 3 additions & 0 deletions packages/next/build/webpack/loaders/next-swc-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ export default function swcLoader(inputSource, inputSourceMap) {
}
)
}

// accept Buffers instead of strings
export const raw = true

0 comments on commit 177e15b

Please sign in to comment.