diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index 8f0fad58daaf0..0a63fdf46012d 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -3,6 +3,12 @@ /* auto-generated by NAPI-RS */ +export interface IsolatedDeclarationsResult { + sourceText: string + errors: Array +} +/** TypeScript Isolated Declarations for Standalone DTS Emit */ +function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult export interface TypeScriptBindingOptions { jsxPragma?: string jsxPragmaFrag?: string @@ -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 @@ -53,10 +62,4 @@ export interface TransformResult { map?: Sourcemap errors: Array } -function transform(filename: string, sourceText: string, options?: TransformBindingOptions | undefined | null): TransformResult -export interface IsolatedDeclarationsResult { - sourceText: string - errors: Array -} -/** 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 diff --git a/napi/transform/index.js b/napi/transform/index.js index 5220f9bbc7906..60bfaa9256101 100644 --- a/napi/transform/index.js +++ b/napi/transform/index.js @@ -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 diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 5d9841b58cfce..9facfd34d9b1c 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -96,6 +96,8 @@ impl From for ES2015Options { pub struct TransformOptions { #[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")] pub source_type: Option, + /// Force jsx parsing, + pub jsx: Option, pub typescript: Option, pub react: Option, pub es2015: Option, @@ -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();