Skip to content

Commit

Permalink
refactor(minifier): expose dce as an API instead of an option (#7957)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 17, 2024
1 parent 18441af commit 1314c97
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
8 changes: 6 additions & 2 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ pub trait CompilerInterface {
if let Some(options) = define_options {
let ret =
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);
Compressor::new(&allocator, CompressOptions::default())
.dead_code_elimination_with_symbols_and_scopes(
ret.symbols,
ret.scopes,
&mut program,
);
// symbols = ret.symbols;
// scopes = ret.scopes;
}
Expand Down
28 changes: 19 additions & 9 deletions crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,30 @@ impl<'a> Compressor<'a> {
) {
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
RemoveSyntax::new(self.options).build(program, &mut ctx);

if self.options.dead_code_elimination {
Self::dead_code_elimination(program, &mut ctx);
return;
}

PeepholeOptimizations::new().build(program, &mut ctx);
CollapsePass::new().build(program, &mut ctx);
LatePeepholeOptimizations::new().run_in_loop(program, &mut ctx);
PeepholeOptimizations::new().build(program, &mut ctx);
}

fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
PeepholeFoldConstants::new().build(program, ctx);
PeepholeRemoveDeadCode::new().build(program, ctx);
pub fn dead_code_elimination(self, program: &mut Program<'a>) {
let (symbols, scopes) =
SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree();
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
RemoveSyntax::new(self.options).build(program, &mut ctx);
PeepholeFoldConstants::new().build(program, &mut ctx);
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
}

pub fn dead_code_elimination_with_symbols_and_scopes(
self,
symbols: SymbolTable,
scopes: ScopeTree,
program: &mut Program<'a>,
) {
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
RemoveSyntax::new(self.options).build(program, &mut ctx);
PeepholeFoldConstants::new().build(program, &mut ctx);
PeepholeRemoveDeadCode::new().build(program, &mut ctx);
}
}
12 changes: 3 additions & 9 deletions crates/oxc_minifier/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[derive(Debug, Clone, Copy)]
pub struct CompressOptions {
pub dead_code_elimination: bool,

/// Remove `debugger;` statements.
///
/// Default `true`
Expand All @@ -16,20 +14,16 @@ pub struct CompressOptions {
#[allow(clippy::derivable_impls)]
impl Default for CompressOptions {
fn default() -> Self {
Self { dead_code_elimination: false, drop_console: false, ..Self::all_true() }
Self { drop_console: false, ..Self::all_true() }
}
}

impl CompressOptions {
pub fn all_true() -> Self {
Self { dead_code_elimination: false, drop_debugger: true, drop_console: true }
Self { drop_debugger: true, drop_console: true }
}

pub fn all_false() -> Self {
Self { dead_code_elimination: false, drop_debugger: false, drop_console: false }
}

pub fn dead_code_elimination() -> Self {
Self { dead_code_elimination: true, ..Self::all_false() }
Self { drop_debugger: false, drop_console: false }
}
}
19 changes: 17 additions & 2 deletions crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
use cow_utils::CowUtils;

use oxc_allocator::Allocator;
use oxc_codegen::Codegen;
use oxc_minifier::CompressOptions;
use oxc_minifier::Compressor;
use oxc_parser::Parser;
use oxc_span::SourceType;

fn run(source_text: &str, source_type: SourceType) -> String {
let allocator = Allocator::default();
let mut ret = Parser::new(&allocator, source_text, source_type).parse();
let program = &mut ret.program;
Compressor::new(&allocator, CompressOptions::default()).dead_code_elimination(program);
Codegen::new().build(program).code
}

fn test(source_text: &str, expected: &str) {
let t = "('production' == 'production')";
let f = "('production' == 'development')";
let source_text = source_text.cow_replace("true", t);
let source_text = source_text.cow_replace("false", f);

let options = CompressOptions::dead_code_elimination();
crate::test(&source_text, expected, options);
let source_type = SourceType::default();
let result = run(&source_text, source_type);
let expected = run(expected, source_type);
assert_eq!(result, expected, "\nfor source\n{source_text}\nexpect\n{expected}\ngot\n{result}");
}

fn test_same(source_text: &str) {
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ impl Oxc {
CompressOptions {
drop_console: compress_options.drop_console,
drop_debugger: compress_options.drop_debugger,
..CompressOptions::default()
}
} else {
CompressOptions::all_false()
Expand Down

0 comments on commit 1314c97

Please sign in to comment.