feat(napi/transform): add define option (#6212)

part of #6156
This commit is contained in:
Boshen 2024-10-01 12:57:29 +00:00
parent 61805fd3f2
commit 291891e71a
3 changed files with 47 additions and 17 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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())
}