diff --git a/crates/oxc/src/compiler.rs b/crates/oxc/src/compiler.rs index 8c95baf04..fe9efb716 100644 --- a/crates/oxc/src/compiler.rs +++ b/crates/oxc/src/compiler.rs @@ -152,6 +152,7 @@ pub trait CompilerInterface { return; } + let stats = semantic_return.semantic.stats(); let (mut symbols, mut scopes) = semantic_return.semantic.into_symbol_table_and_scope_tree(); /* Transform */ @@ -169,20 +170,34 @@ pub trait CompilerInterface { return; } - symbols = transformer_return.symbols; - scopes = transformer_return.scopes; + (symbols, scopes) = (transformer_return.symbols, transformer_return.scopes); } - if let Some(config) = self.inject_options() { - let ret = - InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, &mut program); + let inject_options = self.inject_options(); + let define_options = self.define_options(); + + // Symbols and scopes are out of sync. + if inject_options.is_some() || define_options.is_some() { + (symbols, scopes) = SemanticBuilder::new() + .with_stats(stats) + .build(&program) + .semantic + .into_symbol_table_and_scope_tree(); + } + + if let Some(options) = inject_options { + let ret = InjectGlobalVariables::new(&allocator, options).build( + symbols, + scopes, + &mut program, + ); symbols = ret.symbols; scopes = ret.scopes; } - if let Some(config) = self.define_options() { + if let Some(options) = define_options { let ret = - ReplaceGlobalDefines::new(&allocator, config).build(symbols, scopes, &mut program); + ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, &mut program); Compressor::new(&allocator, CompressOptions::dead_code_elimination()) .build_with_symbols_and_scopes(ret.symbols, ret.scopes, &mut program); // symbols = ret.symbols; diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index e18b5bd3b..4c76416ab 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -203,8 +203,6 @@ export interface TransformOptions { typescript?: TypeScriptOptions /** Configure how TSX and JSX are transformed. */ jsx?: JsxOptions - /** Enable ES2015 transformations. */ - es2015?: Es2015Options /** Define Plugin */ define?: Record /** Inject Plugin */ diff --git a/napi/transform/test/transform.test.mjs b/napi/transform/test/transform.test.mjs index 7b5e71586..4230849e3 100644 --- a/napi/transform/test/transform.test.mjs +++ b/napi/transform/test/transform.test.mjs @@ -63,9 +63,8 @@ $RefreshReg$(_c, "App"); }); describe('define plugin', () => { - const code = 'if (process.env.NODE_ENV === "production") { foo; }'; - it('matches output', () => { + const code = 'if (process.env.NODE_ENV === "production") { foo; }'; const ret = oxc.transform('test.tsx', code, { define: { 'process.env.NODE_ENV': '"development"', @@ -73,6 +72,16 @@ describe('define plugin', () => { }); assert.equal(ret.code, ''); }); + + it('handles typescript declare global', () => { + const code = 'declare let __TEST_DEFINE__: string; console.log({ __TEST_DEFINE__ });'; + const ret = oxc.transform('test.ts', code, { + define: { + '__TEST_DEFINE__': '"replaced"', + }, + }); + assert.equal(ret.code, 'console.log({ __TEST_DEFINE__: "replaced" });\n'); + }); }); describe('inject plugin', () => {