Skip to content

Commit

Permalink
feat(napi/transformer): add jsx option to force parsing with jsx (#…
Browse files Browse the repository at this point in the history
…4133)

To mimic the esbuild `loader=jsx` or babel `babel-plugin-syntax-jsx` behavior.
  • Loading branch information
Boshen committed Jul 10, 2024
1 parent 54cd04a commit 725571a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
19 changes: 11 additions & 8 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

/* auto-generated by NAPI-RS */

export interface IsolatedDeclarationsResult {
sourceText: string
errors: Array<string>
}
/** TypeScript Isolated Declarations for Standalone DTS Emit */
function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
export interface TypeScriptBindingOptions {
jsxPragma?: string
jsxPragmaFrag?: string
Expand All @@ -27,7 +33,10 @@ export interface ArrowFunctionsBindingOptions {
export interface Es2015BindingOptions {
arrowFunction?: ArrowFunctionsBindingOptions
}
export interface TransformBindingOptions {
export interface TransformOptions {
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
/** Force jsx parsing, */
jsx?: boolean
typescript?: TypeScriptBindingOptions
react?: ReactBindingOptions
es2015?: Es2015BindingOptions
Expand All @@ -53,10 +62,4 @@ export interface TransformResult {
map?: Sourcemap
errors: Array<string>
}
function transform(filename: string, sourceText: string, options?: TransformBindingOptions | undefined | null): TransformResult
export interface IsolatedDeclarationsResult {
sourceText: string
errors: Array<string>
}
/** TypeScript Isolated Declarations for Standalone DTS Emit */
function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult
4 changes: 2 additions & 2 deletions napi/transform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { transform, isolatedDeclaration } = nativeBinding
const { isolatedDeclaration, transform } = nativeBinding

module.exports.transform = transform
module.exports.isolatedDeclaration = isolatedDeclaration
module.exports.transform = transform
20 changes: 15 additions & 5 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ impl From<ES2015BindingOptions> for ES2015Options {
pub struct TransformOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,
/// Force jsx parsing,
pub jsx: Option<bool>,
pub typescript: Option<TypeScriptBindingOptions>,
pub react: Option<ReactBindingOptions>,
pub es2015: Option<ES2015BindingOptions>,
Expand Down Expand Up @@ -145,11 +147,19 @@ pub fn transform(
let sourcemap = options.as_ref().is_some_and(|x| x.sourcemap.unwrap_or_default());
let mut errors = vec![];

let source_type = SourceType::from_path(&filename).unwrap_or_default();
let source_type = match options.as_ref().and_then(|options| options.source_type.as_deref()) {
Some("script") => source_type.with_script(true),
Some("module") => source_type.with_module(true),
_ => source_type,
let source_type = {
let mut source_type = SourceType::from_path(&filename).unwrap_or_default();
// Force `script` or `module`
match options.as_ref().and_then(|options| options.source_type.as_deref()) {
Some("script") => source_type = source_type.with_script(true),
Some("module") => source_type = source_type.with_module(true),
_ => {}
}
// Force `jsx`
if let Some(jsx) = options.as_ref().and_then(|options| options.jsx.as_ref()) {
source_type = source_type.with_jsx(*jsx);
}
source_type
};

let allocator = Allocator::default();
Expand Down

0 comments on commit 725571a

Please sign in to comment.