mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +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 */
|
||||
|
||||
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
|
||||
|
|
@ -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<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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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>,
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue