Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
218 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::util::{deserialize_json, CtxtExt, MapErr}; | ||
use anyhow::Context as _; | ||
use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task}; | ||
use std::sync::Arc; | ||
use swc::{config::ParseOptions, try_with_handler}; | ||
use swc_common::{FileName, FilePathMapping, SourceMap}; | ||
|
||
pub struct ParseTask { | ||
pub filename: FileName, | ||
pub src: String, | ||
pub options: String, | ||
} | ||
|
||
pub fn complete_parse<'a>(env: &Env, ast_json: String) -> napi::Result<JsString> { | ||
env.create_string_from_std(ast_json) | ||
} | ||
|
||
impl Task for ParseTask { | ||
type Output = String; | ||
type JsValue = JsString; | ||
|
||
fn compute(&mut self) -> napi::Result<Self::Output> { | ||
let c = swc::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty()))); | ||
|
||
let options: ParseOptions = deserialize_json(&self.options).convert_err()?; | ||
let fm = | ||
c.cm.new_source_file(self.filename.clone(), self.src.clone()); | ||
let program = try_with_handler(c.cm.clone(), false, |handler| { | ||
c.parse_js( | ||
fm, | ||
&handler, | ||
options.target, | ||
options.syntax, | ||
options.is_module, | ||
options.comments, | ||
) | ||
}) | ||
.convert_err()?; | ||
|
||
let ast_json = serde_json::to_string(&program) | ||
.context("failed to serialize Program") | ||
.convert_err()?; | ||
|
||
Ok(ast_json) | ||
} | ||
|
||
fn resolve(self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> { | ||
complete_parse(&env, result) | ||
} | ||
} | ||
|
||
#[js_function(3)] | ||
pub fn parse(ctx: CallContext) -> napi::Result<JsObject> { | ||
let src = ctx.get::<JsString>(0)?.into_utf8()?.as_str()?.to_string(); | ||
let options = ctx.get_buffer_as_string(1)?; | ||
let filename = ctx.get::<Either<JsString, JsUndefined>>(2)?; | ||
let filename = if let Either::A(value) = filename { | ||
FileName::Real(value.into_utf8()?.as_str()?.to_owned().into()) | ||
} else { | ||
FileName::Anon | ||
}; | ||
|
||
ctx.env | ||
.spawn(ParseTask { | ||
filename, | ||
src, | ||
options, | ||
}) | ||
.map(|t| t.promise_object()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...gration/react-streaming-and-server-components/app/components/client-exports-all.client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './client-exports' |
20 changes: 20 additions & 0 deletions
20
test/integration/react-streaming-and-server-components/app/components/client-exports-all.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export * from './client-exports' | ||
|
||
// TODO: add exports all test case in pages | ||
/** | ||
import * as all from '../components/client-exports-all' | ||
import * as allClient from '../components/client-exports-all.client' | ||
export default function Page() { | ||
const { a, b, c, d, e } = all | ||
const { a: ac, b: bc, c: cc, d: dc, e: ec } = allClient | ||
return ( | ||
<div> | ||
<div id='server'>{a}{b}{c}{d}{e[0]}</div> | ||
<div id='client'>{ac}{bc}{cc}{dc}{ec[0]}</div> | ||
</div> | ||
) | ||
} | ||
*/ |
Oops, something went wrong.