Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions napi/transform/test/transform.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
});
});

Expand Down