Skip to content

Commit 725571a

Browse files
committed
feat(napi/transformer): add jsx option to force parsing with jsx (#4133)
To mimic the esbuild `loader=jsx` or babel `babel-plugin-syntax-jsx` behavior.
1 parent 54cd04a commit 725571a

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

napi/transform/index.d.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
/* auto-generated by NAPI-RS */
55

6+
export interface IsolatedDeclarationsResult {
7+
sourceText: string
8+
errors: Array<string>
9+
}
10+
/** TypeScript Isolated Declarations for Standalone DTS Emit */
11+
function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
612
export interface TypeScriptBindingOptions {
713
jsxPragma?: string
814
jsxPragmaFrag?: string
@@ -27,7 +33,10 @@ export interface ArrowFunctionsBindingOptions {
2733
export interface Es2015BindingOptions {
2834
arrowFunction?: ArrowFunctionsBindingOptions
2935
}
30-
export interface TransformBindingOptions {
36+
export interface TransformOptions {
37+
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
38+
/** Force jsx parsing, */
39+
jsx?: boolean
3140
typescript?: TypeScriptBindingOptions
3241
react?: ReactBindingOptions
3342
es2015?: Es2015BindingOptions
@@ -53,10 +62,4 @@ export interface TransformResult {
5362
map?: Sourcemap
5463
errors: Array<string>
5564
}
56-
function transform(filename: string, sourceText: string, options?: TransformBindingOptions | undefined | null): TransformResult
57-
export interface IsolatedDeclarationsResult {
58-
sourceText: string
59-
errors: Array<string>
60-
}
61-
/** TypeScript Isolated Declarations for Standalone DTS Emit */
62-
function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
65+
function transform(filename: string, sourceText: string, options?: TransformOptions | undefined | null): TransformResult

napi/transform/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ if (!nativeBinding) {
310310
throw new Error(`Failed to load native binding`)
311311
}
312312

313-
const { transform, isolatedDeclaration } = nativeBinding
313+
const { isolatedDeclaration, transform } = nativeBinding
314314

315-
module.exports.transform = transform
316315
module.exports.isolatedDeclaration = isolatedDeclaration
316+
module.exports.transform = transform

napi/transform/src/transformer.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ impl From<ES2015BindingOptions> for ES2015Options {
9696
pub struct TransformOptions {
9797
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
9898
pub source_type: Option<String>,
99+
/// Force jsx parsing,
100+
pub jsx: Option<bool>,
99101
pub typescript: Option<TypeScriptBindingOptions>,
100102
pub react: Option<ReactBindingOptions>,
101103
pub es2015: Option<ES2015BindingOptions>,
@@ -145,11 +147,19 @@ pub fn transform(
145147
let sourcemap = options.as_ref().is_some_and(|x| x.sourcemap.unwrap_or_default());
146148
let mut errors = vec![];
147149

148-
let source_type = SourceType::from_path(&filename).unwrap_or_default();
149-
let source_type = match options.as_ref().and_then(|options| options.source_type.as_deref()) {
150-
Some("script") => source_type.with_script(true),
151-
Some("module") => source_type.with_module(true),
152-
_ => source_type,
150+
let source_type = {
151+
let mut source_type = SourceType::from_path(&filename).unwrap_or_default();
152+
// Force `script` or `module`
153+
match options.as_ref().and_then(|options| options.source_type.as_deref()) {
154+
Some("script") => source_type = source_type.with_script(true),
155+
Some("module") => source_type = source_type.with_module(true),
156+
_ => {}
157+
}
158+
// Force `jsx`
159+
if let Some(jsx) = options.as_ref().and_then(|options| options.jsx.as_ref()) {
160+
source_type = source_type.with_jsx(*jsx);
161+
}
162+
source_type
153163
};
154164

155165
let allocator = Allocator::default();

0 commit comments

Comments
 (0)