From 291891e71aed697b3df6652448664d4241a9c24d Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:57:29 +0000 Subject: [PATCH] feat(napi/transform): add `define` option (#6212) part of #6156 --- napi/transform/index.d.ts | 14 ++++++++------ napi/transform/src/options.rs | 21 +++++++++++++-------- napi/transform/src/transformer.rs | 29 ++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index ff0f123a657cf..6917ffba798f7 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -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 } export interface TransformResult { diff --git a/napi/transform/src/options.rs b/napi/transform/src/options.rs index cf577fe044ded..b57357e27fd11 100644 --- a/napi/transform/src/options.rs +++ b/napi/transform/src/options.rs @@ -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, + /// Enable source map generation. + /// + /// When `true`, the `sourceMap` field of transform result objects will be populated. + /// + /// @default false + /// + /// @see {@link SourceMap} + pub sourcemap: Option, + /// Configure how TypeScript is transformed. pub typescript: Option, @@ -30,14 +41,8 @@ pub struct TransformOptions { /// Enable ES2015 transformations. pub es2015: Option, - /// Enable source map generation. - /// - /// When `true`, the `sourceMap` field of transform result objects will be populated. - /// - /// @default false - /// - /// @see {@link SourceMap} - pub sourcemap: Option, + /// Define Plugin + pub define: Option>, } impl From for oxc_transformer::TransformOptions { diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 662d7e4f1152f..1bb73bd4bf379 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -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) -> 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) -> 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::>(); + 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()) }