Unverified Commit e7b17133 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Support swc parse api in WASM (#36482)

* Add `parseSync` in swc WASM binding
* Support no native binding mode for react 18 next apps which are using `NODE_OPTIONS='--no-addons'` flag
* Workaround: adding minus begining position back for wasm loader since the span isn't starting from zero
parent a16fa392
......@@ -1064,6 +1064,9 @@ jobs:
- run: TEST_WASM=true xvfb-run node run-tests.js test/integration/production/test/index.test.js
if: ${{needs.build.outputs.docsChange != 'docs only change'}}
- run: TEST_WASM=true xvfb-run node run-tests.js test/production/react-18-streaming-ssr/index.test.ts
if: ${{needs.build.outputs.docsChange != 'docs only change'}}
# Build binaries for publishing
build-native:
strategy:
......
......@@ -2,8 +2,8 @@ use anyhow::{Context, Error};
use next_swc::{custom_before_pass, TransformOptions};
use once_cell::sync::Lazy;
use std::sync::Arc;
use swc::{config::JsMinifyOptions, try_with_handler, Compiler};
use swc_common::{errors::ColorConfig, FileName, FilePathMapping, SourceMap};
use swc::{config::JsMinifyOptions, config::ParseOptions, try_with_handler, Compiler};
use swc_common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap};
use swc_ecmascript::transforms::pass::noop;
use wasm_bindgen::prelude::*;
......@@ -81,6 +81,49 @@ pub fn transform_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
.map_err(convert_err)
}
#[wasm_bindgen(js_name = "parseSync")]
pub fn parse_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let c = compiler();
try_with_handler(
c.cm.clone(),
swc::HandlerOpts {
..Default::default()
},
|handler| {
c.run(|| {
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))
})
},
)
.map_err(convert_err)
}
/// Get global sourcemap
fn compiler() -> Arc<Compiler> {
static C: Lazy<Arc<Compiler>> = Lazy::new(|| {
......
......@@ -74,7 +74,8 @@ async function loadWasm() {
return Promise.resolve(bindings.minifySync(src.toString(), options))
},
parse(src, options) {
return Promise.resolve(bindings.parse(src.toString(), options))
const astStr = bindings.parseSync(src.toString(), options)
return Promise.resolve(astStr)
},
getTargetTriple() {
return undefined
......
......@@ -12,7 +12,9 @@ function createFlightServerRequest(
request: string,
options?: { client: 1 | undefined }
) {
return `next-flight-server-loader?${JSON.stringify(options)}!${request}`
return `next-flight-server-loader${
options ? '?' + JSON.stringify(options) : ''
}!${request}`
}
function hasFlightLoader(request: string, type: 'client' | 'server') {
......@@ -45,6 +47,7 @@ async function parseModuleInfo({
isModule: 'unknown',
})
const { type, body } = ast
const beginPos = ast.span.start
let transformedSource = ''
let lastIndex = 0
let imports = []
......@@ -105,7 +108,7 @@ async function parseModuleInfo({
const importDeclarations = source.substring(
lastIndex,
node.source.span.start
node.source.span.start - beginPos
)
if (isClientComponent(importSource)) {
......@@ -135,7 +138,7 @@ async function parseModuleInfo({
addClientImport(importSource)
}
lastIndex = node.source.span.end
lastIndex = node.source.span.end - beginPos
break
case 'ExportDeclaration':
if (isClientCompilation) {
......
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