mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(minifier): expose dce as an API instead of an option (#7957)
This commit is contained in:
parent
18441afe1c
commit
1314c9763b
5 changed files with 45 additions and 23 deletions
|
|
@ -198,8 +198,12 @@ pub trait CompilerInterface {
|
||||||
if let Some(options) = define_options {
|
if let Some(options) = define_options {
|
||||||
let ret =
|
let ret =
|
||||||
ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, &mut program);
|
ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, &mut program);
|
||||||
Compressor::new(&allocator, CompressOptions::dead_code_elimination())
|
Compressor::new(&allocator, CompressOptions::default())
|
||||||
.build_with_symbols_and_scopes(ret.symbols, ret.scopes, &mut program);
|
.dead_code_elimination_with_symbols_and_scopes(
|
||||||
|
ret.symbols,
|
||||||
|
ret.scopes,
|
||||||
|
&mut program,
|
||||||
|
);
|
||||||
// symbols = ret.symbols;
|
// symbols = ret.symbols;
|
||||||
// scopes = ret.scopes;
|
// scopes = ret.scopes;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,20 +35,30 @@ impl<'a> Compressor<'a> {
|
||||||
) {
|
) {
|
||||||
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
|
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, self.allocator);
|
||||||
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
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);
|
PeepholeOptimizations::new().build(program, &mut ctx);
|
||||||
CollapsePass::new().build(program, &mut ctx);
|
CollapsePass::new().build(program, &mut ctx);
|
||||||
LatePeepholeOptimizations::new().run_in_loop(program, &mut ctx);
|
LatePeepholeOptimizations::new().run_in_loop(program, &mut ctx);
|
||||||
PeepholeOptimizations::new().build(program, &mut ctx);
|
PeepholeOptimizations::new().build(program, &mut ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut ReusableTraverseCtx<'a>) {
|
pub fn dead_code_elimination(self, program: &mut Program<'a>) {
|
||||||
PeepholeFoldConstants::new().build(program, ctx);
|
let (symbols, scopes) =
|
||||||
PeepholeRemoveDeadCode::new().build(program, ctx);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct CompressOptions {
|
pub struct CompressOptions {
|
||||||
pub dead_code_elimination: bool,
|
|
||||||
|
|
||||||
/// Remove `debugger;` statements.
|
/// Remove `debugger;` statements.
|
||||||
///
|
///
|
||||||
/// Default `true`
|
/// Default `true`
|
||||||
|
|
@ -16,20 +14,16 @@ pub struct CompressOptions {
|
||||||
#[allow(clippy::derivable_impls)]
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for CompressOptions {
|
impl Default for CompressOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self { dead_code_elimination: false, drop_console: false, ..Self::all_true() }
|
Self { drop_console: false, ..Self::all_true() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompressOptions {
|
impl CompressOptions {
|
||||||
pub fn all_true() -> Self {
|
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 {
|
pub fn all_false() -> Self {
|
||||||
Self { dead_code_elimination: false, drop_debugger: false, drop_console: false }
|
Self { drop_debugger: false, drop_console: false }
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dead_code_elimination() -> Self {
|
|
||||||
Self { dead_code_elimination: true, ..Self::all_false() }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,19 @@
|
||||||
use cow_utils::CowUtils;
|
use cow_utils::CowUtils;
|
||||||
|
|
||||||
|
use oxc_allocator::Allocator;
|
||||||
|
use oxc_codegen::Codegen;
|
||||||
use oxc_minifier::CompressOptions;
|
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) {
|
fn test(source_text: &str, expected: &str) {
|
||||||
let t = "('production' == 'production')";
|
let t = "('production' == 'production')";
|
||||||
|
|
@ -8,8 +21,10 @@ fn test(source_text: &str, expected: &str) {
|
||||||
let source_text = source_text.cow_replace("true", t);
|
let source_text = source_text.cow_replace("true", t);
|
||||||
let source_text = source_text.cow_replace("false", f);
|
let source_text = source_text.cow_replace("false", f);
|
||||||
|
|
||||||
let options = CompressOptions::dead_code_elimination();
|
let source_type = SourceType::default();
|
||||||
crate::test(&source_text, expected, options);
|
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) {
|
fn test_same(source_text: &str) {
|
||||||
|
|
|
||||||
|
|
@ -274,7 +274,6 @@ impl Oxc {
|
||||||
CompressOptions {
|
CompressOptions {
|
||||||
drop_console: compress_options.drop_console,
|
drop_console: compress_options.drop_console,
|
||||||
drop_debugger: compress_options.drop_debugger,
|
drop_debugger: compress_options.drop_debugger,
|
||||||
..CompressOptions::default()
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CompressOptions::all_false()
|
CompressOptions::all_false()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue