mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
parent
61805fd3f2
commit
291891e71a
3 changed files with 47 additions and 17 deletions
14
napi/transform/index.d.ts
vendored
14
napi/transform/index.d.ts
vendored
|
|
@ -187,12 +187,6 @@ export interface TransformOptions {
|
|||
* options.
|
||||
*/
|
||||
cwd?: string
|
||||
/** Configure how TypeScript is transformed. */
|
||||
typescript?: TypeScriptOptions
|
||||
/** Configure how TSX and JSX are transformed. */
|
||||
jsx?: JsxOptions
|
||||
/** Enable ES2015 transformations. */
|
||||
es2015?: ES2015BindingOptions
|
||||
/**
|
||||
* Enable source map generation.
|
||||
*
|
||||
|
|
@ -203,6 +197,14 @@ export interface TransformOptions {
|
|||
* @see {@link SourceMap}
|
||||
*/
|
||||
sourcemap?: boolean
|
||||
/** Configure how TypeScript is transformed. */
|
||||
typescript?: TypeScriptOptions
|
||||
/** Configure how TSX and JSX are transformed. */
|
||||
jsx?: JsxOptions
|
||||
/** Enable ES2015 transformations. */
|
||||
es2015?: ES2015BindingOptions
|
||||
/** Define Plugin */
|
||||
define?: Record<string, string>
|
||||
}
|
||||
|
||||
export interface TransformResult {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#![allow(rustdoc::bare_urls)]
|
||||
#![allow(clippy::disallowed_types)] // allow HashMap
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use napi::Either;
|
||||
|
|
@ -21,6 +23,15 @@ pub struct TransformOptions {
|
|||
/// options.
|
||||
pub cwd: Option<String>,
|
||||
|
||||
/// Enable source map generation.
|
||||
///
|
||||
/// When `true`, the `sourceMap` field of transform result objects will be populated.
|
||||
///
|
||||
/// @default false
|
||||
///
|
||||
/// @see {@link SourceMap}
|
||||
pub sourcemap: Option<bool>,
|
||||
|
||||
/// Configure how TypeScript is transformed.
|
||||
pub typescript: Option<TypeScriptOptions>,
|
||||
|
||||
|
|
@ -30,14 +41,8 @@ pub struct TransformOptions {
|
|||
/// Enable ES2015 transformations.
|
||||
pub es2015: Option<ES2015BindingOptions>,
|
||||
|
||||
/// Enable source map generation.
|
||||
///
|
||||
/// When `true`, the `sourceMap` field of transform result objects will be populated.
|
||||
///
|
||||
/// @default false
|
||||
///
|
||||
/// @see {@link SourceMap}
|
||||
pub sourcemap: Option<bool>,
|
||||
/// Define Plugin
|
||||
pub define: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
impl From<TransformOptions> for oxc_transformer::TransformOptions {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use oxc_allocator::Allocator;
|
|||
use oxc_codegen::CodegenReturn;
|
||||
use oxc_semantic::SemanticBuilder;
|
||||
use oxc_span::SourceType;
|
||||
use oxc_transformer::Transformer;
|
||||
use oxc_transformer::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig, Transformer};
|
||||
|
||||
use crate::{context::TransformContext, isolated_declaration, SourceMap, TransformOptions};
|
||||
|
||||
|
|
@ -105,9 +105,13 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
|
|||
.build(&ctx.program());
|
||||
ctx.add_diagnostics(semantic_ret.errors);
|
||||
|
||||
let mut options = options;
|
||||
let define = options.as_mut().and_then(|options| options.define.take());
|
||||
|
||||
let options = options.map(oxc_transformer::TransformOptions::from).unwrap_or_default();
|
||||
|
||||
let (symbols, scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();
|
||||
let (mut symbols, mut scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();
|
||||
|
||||
let ret = Transformer::new(
|
||||
ctx.allocator,
|
||||
ctx.file_path(),
|
||||
|
|
@ -117,8 +121,27 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
|
|||
options,
|
||||
)
|
||||
.build_with_symbols_and_scopes(symbols, scopes, &mut ctx.program_mut());
|
||||
|
||||
ctx.add_diagnostics(ret.errors);
|
||||
symbols = ret.symbols;
|
||||
scopes = ret.scopes;
|
||||
|
||||
if let Some(define) = define {
|
||||
let define = define.into_iter().collect::<Vec<_>>();
|
||||
match ReplaceGlobalDefinesConfig::new(&define) {
|
||||
Ok(config) => {
|
||||
let _ret = ReplaceGlobalDefines::new(ctx.allocator, config).build(
|
||||
symbols,
|
||||
scopes,
|
||||
&mut ctx.program_mut(),
|
||||
);
|
||||
// symbols = ret.symbols;
|
||||
// scopes = ret.scopes;
|
||||
}
|
||||
Err(errors) => {
|
||||
ctx.add_diagnostics(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.codegen().build(&ctx.program())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue