mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
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.
This commit is contained in:
parent
54cd04aead
commit
725571aad1
3 changed files with 28 additions and 15 deletions
19
napi/transform/index.d.ts
vendored
19
napi/transform/index.d.ts
vendored
|
|
@ -3,6 +3,12 @@
|
||||||
|
|
||||||
/* auto-generated by NAPI-RS */
|
/* 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 {
|
export interface TypeScriptBindingOptions {
|
||||||
jsxPragma?: string
|
jsxPragma?: string
|
||||||
jsxPragmaFrag?: string
|
jsxPragmaFrag?: string
|
||||||
|
|
@ -27,7 +33,10 @@ export interface ArrowFunctionsBindingOptions {
|
||||||
export interface Es2015BindingOptions {
|
export interface Es2015BindingOptions {
|
||||||
arrowFunction?: ArrowFunctionsBindingOptions
|
arrowFunction?: ArrowFunctionsBindingOptions
|
||||||
}
|
}
|
||||||
export interface TransformBindingOptions {
|
export interface TransformOptions {
|
||||||
|
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
|
||||||
|
/** Force jsx parsing, */
|
||||||
|
jsx?: boolean
|
||||||
typescript?: TypeScriptBindingOptions
|
typescript?: TypeScriptBindingOptions
|
||||||
react?: ReactBindingOptions
|
react?: ReactBindingOptions
|
||||||
es2015?: Es2015BindingOptions
|
es2015?: Es2015BindingOptions
|
||||||
|
|
@ -53,10 +62,4 @@ export interface TransformResult {
|
||||||
map?: Sourcemap
|
map?: Sourcemap
|
||||||
errors: Array<string>
|
errors: Array<string>
|
||||||
}
|
}
|
||||||
function transform(filename: string, sourceText: string, options?: TransformBindingOptions | undefined | null): TransformResult
|
function transform(filename: string, sourceText: string, options?: TransformOptions | 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
|
|
||||||
|
|
|
||||||
|
|
@ -310,7 +310,7 @@ if (!nativeBinding) {
|
||||||
throw new Error(`Failed to load native binding`)
|
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.isolatedDeclaration = isolatedDeclaration
|
||||||
|
module.exports.transform = transform
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,8 @@ impl From<ES2015BindingOptions> for ES2015Options {
|
||||||
pub struct TransformOptions {
|
pub struct TransformOptions {
|
||||||
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
|
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
|
||||||
pub source_type: Option<String>,
|
pub source_type: Option<String>,
|
||||||
|
/// Force jsx parsing,
|
||||||
|
pub jsx: Option<bool>,
|
||||||
pub typescript: Option<TypeScriptBindingOptions>,
|
pub typescript: Option<TypeScriptBindingOptions>,
|
||||||
pub react: Option<ReactBindingOptions>,
|
pub react: Option<ReactBindingOptions>,
|
||||||
pub es2015: Option<ES2015BindingOptions>,
|
pub es2015: Option<ES2015BindingOptions>,
|
||||||
|
|
@ -145,11 +147,19 @@ pub fn transform(
|
||||||
let sourcemap = options.as_ref().is_some_and(|x| x.sourcemap.unwrap_or_default());
|
let sourcemap = options.as_ref().is_some_and(|x| x.sourcemap.unwrap_or_default());
|
||||||
let mut errors = vec![];
|
let mut errors = vec![];
|
||||||
|
|
||||||
let source_type = SourceType::from_path(&filename).unwrap_or_default();
|
let source_type = {
|
||||||
let source_type = match options.as_ref().and_then(|options| options.source_type.as_deref()) {
|
let mut source_type = SourceType::from_path(&filename).unwrap_or_default();
|
||||||
Some("script") => source_type.with_script(true),
|
// Force `script` or `module`
|
||||||
Some("module") => source_type.with_module(true),
|
match options.as_ref().and_then(|options| options.source_type.as_deref()) {
|
||||||
_ => source_type,
|
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();
|
let allocator = Allocator::default();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue