Skip to content

Commit 291891e

Browse files
committed
feat(napi/transform): add define option (#6212)
part of #6156
1 parent 61805fd commit 291891e

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

napi/transform/index.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ export interface TransformOptions {
187187
* options.
188188
*/
189189
cwd?: string
190-
/** Configure how TypeScript is transformed. */
191-
typescript?: TypeScriptOptions
192-
/** Configure how TSX and JSX are transformed. */
193-
jsx?: JsxOptions
194-
/** Enable ES2015 transformations. */
195-
es2015?: ES2015BindingOptions
196190
/**
197191
* Enable source map generation.
198192
*
@@ -203,6 +197,14 @@ export interface TransformOptions {
203197
* @see {@link SourceMap}
204198
*/
205199
sourcemap?: boolean
200+
/** Configure how TypeScript is transformed. */
201+
typescript?: TypeScriptOptions
202+
/** Configure how TSX and JSX are transformed. */
203+
jsx?: JsxOptions
204+
/** Enable ES2015 transformations. */
205+
es2015?: ES2015BindingOptions
206+
/** Define Plugin */
207+
define?: Record<string, string>
206208
}
207209

208210
export interface TransformResult {

napi/transform/src/options.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(rustdoc::bare_urls)]
2+
#![allow(clippy::disallowed_types)] // allow HashMap
23

4+
use std::collections::HashMap;
35
use std::path::PathBuf;
46

57
use napi::Either;
@@ -21,6 +23,15 @@ pub struct TransformOptions {
2123
/// options.
2224
pub cwd: Option<String>,
2325

26+
/// Enable source map generation.
27+
///
28+
/// When `true`, the `sourceMap` field of transform result objects will be populated.
29+
///
30+
/// @default false
31+
///
32+
/// @see {@link SourceMap}
33+
pub sourcemap: Option<bool>,
34+
2435
/// Configure how TypeScript is transformed.
2536
pub typescript: Option<TypeScriptOptions>,
2637

@@ -30,14 +41,8 @@ pub struct TransformOptions {
3041
/// Enable ES2015 transformations.
3142
pub es2015: Option<ES2015BindingOptions>,
3243

33-
/// Enable source map generation.
34-
///
35-
/// When `true`, the `sourceMap` field of transform result objects will be populated.
36-
///
37-
/// @default false
38-
///
39-
/// @see {@link SourceMap}
40-
pub sourcemap: Option<bool>,
44+
/// Define Plugin
45+
pub define: Option<HashMap<String, String>>,
4146
}
4247

4348
impl From<TransformOptions> for oxc_transformer::TransformOptions {

napi/transform/src/transformer.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use oxc_allocator::Allocator;
33
use oxc_codegen::CodegenReturn;
44
use oxc_semantic::SemanticBuilder;
55
use oxc_span::SourceType;
6-
use oxc_transformer::Transformer;
6+
use oxc_transformer::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig, Transformer};
77

88
use crate::{context::TransformContext, isolated_declaration, SourceMap, TransformOptions};
99

@@ -105,9 +105,13 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
105105
.build(&ctx.program());
106106
ctx.add_diagnostics(semantic_ret.errors);
107107

108+
let mut options = options;
109+
let define = options.as_mut().and_then(|options| options.define.take());
110+
108111
let options = options.map(oxc_transformer::TransformOptions::from).unwrap_or_default();
109112

110-
let (symbols, scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();
113+
let (mut symbols, mut scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();
114+
111115
let ret = Transformer::new(
112116
ctx.allocator,
113117
ctx.file_path(),
@@ -117,8 +121,27 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
117121
options,
118122
)
119123
.build_with_symbols_and_scopes(symbols, scopes, &mut ctx.program_mut());
120-
121124
ctx.add_diagnostics(ret.errors);
125+
symbols = ret.symbols;
126+
scopes = ret.scopes;
127+
128+
if let Some(define) = define {
129+
let define = define.into_iter().collect::<Vec<_>>();
130+
match ReplaceGlobalDefinesConfig::new(&define) {
131+
Ok(config) => {
132+
let _ret = ReplaceGlobalDefines::new(ctx.allocator, config).build(
133+
symbols,
134+
scopes,
135+
&mut ctx.program_mut(),
136+
);
137+
// symbols = ret.symbols;
138+
// scopes = ret.scopes;
139+
}
140+
Err(errors) => {
141+
ctx.add_diagnostics(errors);
142+
}
143+
}
144+
}
122145

123146
ctx.codegen().build(&ctx.program())
124147
}

0 commit comments

Comments
 (0)