Skip to content

Commit

Permalink
feat(napi/transform): add define option (#6212)
Browse files Browse the repository at this point in the history
part of #6156
  • Loading branch information
Boshen committed Oct 1, 2024
1 parent 61805fd commit 291891e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
14 changes: 8 additions & 6 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions napi/transform/src/options.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>,

Expand All @@ -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 {
Expand Down
29 changes: 26 additions & 3 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(),
Expand All @@ -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())
}

0 comments on commit 291891e

Please sign in to comment.