feat(napi/transform): perform dce after define plugin (#6312)

This commit is contained in:
Boshen 2024-10-06 09:49:52 +00:00
parent 642725cd7d
commit abd3a9fe11
2 changed files with 9 additions and 8 deletions

View file

@ -189,16 +189,18 @@ pub trait CompilerInterface {
scopes = transformer_return.scopes;
}
if let Some(config) = self.define_options() {
if let Some(config) = self.inject_options() {
let ret =
ReplaceGlobalDefines::new(&allocator, config).build(symbols, scopes, &mut program);
InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, &mut program);
symbols = ret.symbols;
scopes = ret.scopes;
}
if let Some(config) = self.inject_options() {
let _ret =
InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, &mut program);
if let Some(config) = self.define_options() {
let ret =
ReplaceGlobalDefines::new(&allocator, config).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;
// scopes = ret.scopes;
}

View file

@ -62,11 +62,10 @@ describe('define plugin', () => {
it('matches output', () => {
const ret = oxc.transform('test.tsx', code, {
define: {
'process.env.NODE_ENV': 'false',
'process.env.NODE_ENV': '"development"',
},
});
// TODO: should be constant folded
assert.equal(ret.code, 'if (false === "production") {\n\tfoo;\n}\n');
assert.equal(ret.code, '');
});
});