Skip to content
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
19 changes: 16 additions & 3 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! - `--nospace`: Remove extra whitespace
//! - `--twice`: Test idempotency by running twice
//! - `--sourcemap`: Generate source maps
//! - `--max-iterations <u8>`: Set the maximum number of compress pass iterations

use std::path::{Path, PathBuf};

Expand All @@ -41,6 +42,9 @@ fn main() -> std::io::Result<()> {
let nospace = args.contains("--nospace");
let twice = args.contains("--twice");
let sourcemap = args.contains("--sourcemap");
let max_iterations = args
.opt_value_from_str::<&str, u8>("--max-iterations")
.expect("Invalid number for --max-iterations");
let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());

let path = Path::new(&name);
Expand All @@ -49,7 +53,15 @@ fn main() -> std::io::Result<()> {
let source_map_path = sourcemap.then(|| path.to_path_buf());

let mut allocator = Allocator::default();
let ret = minify(&allocator, &source_text, source_type, source_map_path, mangle, nospace);
let ret = minify(
&allocator,
&source_text,
source_type,
source_map_path,
mangle,
nospace,
max_iterations,
);
let printed = ret.code;
println!("{printed}");

Expand All @@ -61,7 +73,7 @@ fn main() -> std::io::Result<()> {

if twice {
allocator.reset();
let printed2 = minify(&allocator, &printed, source_type, None, mangle, nospace).code;
let printed2 = minify(&allocator, &printed, source_type, None, mangle, nospace, None).code;
println!("{printed2}");
println!("same = {}", printed == printed2);
}
Expand All @@ -76,12 +88,13 @@ fn minify(
source_map_path: Option<PathBuf>,
mangle: bool,
nospace: bool,
max_iterations: Option<u8>,
) -> CodegenReturn {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;
let options = MinifierOptions {
mangle: mangle.then(MangleOptions::default),
compress: Some(CompressOptions::smallest()),
compress: Some(CompressOptions { max_iterations, ..CompressOptions::smallest() }),
};
let ret = Minifier::new(options).minify(allocator, &mut program);
Codegen::new()
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ impl<'a> Compressor<'a> {
scoping: Scoping,
options: CompressOptions,
) -> u8 {
let max_iterations = options.max_iterations;
let state = MinifierState::new(program.source_type, options);
let mut ctx = ReusableTraverseCtx::new(state, scoping, self.allocator);
let normalize_options =
NormalizeOptions { convert_while_to_fors: true, convert_const_to_let: true };
Normalize::new(normalize_options).build(program, &mut ctx);
PeepholeOptimizations::new().run_in_loop(program, &mut ctx)
PeepholeOptimizations::new(max_iterations).run_in_loop(program, &mut ctx)
}

pub fn dead_code_elimination(self, program: &mut Program<'a>, options: CompressOptions) -> u8 {
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_minifier/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct CompressOptions {
/// Treeshake Options .
/// <https://rollupjs.org/configuration-options/#treeshake>
pub treeshake: TreeShakeOptions,

/// Limit the maximum number of iterations for debugging purpose.
pub max_iterations: Option<u8>,
}

impl Default for CompressOptions {
Expand All @@ -64,6 +67,7 @@ impl CompressOptions {
sequences: true,
unused: CompressOptionsUnused::Remove,
treeshake: TreeShakeOptions::default(),
max_iterations: None,
}
}

Expand All @@ -77,6 +81,7 @@ impl CompressOptions {
sequences: true,
unused: CompressOptionsUnused::Keep,
treeshake: TreeShakeOptions::default(),
max_iterations: None,
}
}

Expand All @@ -90,6 +95,7 @@ impl CompressOptions {
sequences: false,
unused: CompressOptionsUnused::Remove,
treeshake: TreeShakeOptions::default(),
max_iterations: None,
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions crates/oxc_minifier/src/peephole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
pub use self::normalize::{Normalize, NormalizeOptions};

pub struct PeepholeOptimizations {
max_iterations: Option<u8>,
/// Walk the ast in a fixed point loop until no changes are made.
/// `prev_function_changed`, `functions_changed` and `current_function` track changes
/// in top level and each function. No minification code are run if the function is not changed
Expand All @@ -41,8 +42,8 @@ pub struct PeepholeOptimizations {
}

impl<'a> PeepholeOptimizations {
pub fn new() -> Self {
Self { iteration: 0, changed: false }
pub fn new(max_iterations: Option<u8>) -> Self {
Self { max_iterations, iteration: 0, changed: false }
}

fn run_once(
Expand All @@ -64,7 +65,11 @@ impl<'a> PeepholeOptimizations {
if !self.changed {
break;
}
if self.iteration > 10 {
if let Some(max_iterations) = self.max_iterations {
if self.iteration >= max_iterations {
break;
}
} else if self.iteration > 10 {
debug_assert!(false, "Ran loop more than 10 times.");
break;
}
Expand Down
1 change: 1 addition & 0 deletions napi/minify/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl TryFrom<&CompressOptions> for oxc_minifier::CompressOptions {
unused: oxc_minifier::CompressOptionsUnused::Keep,
keep_names: o.keep_names.as_ref().map(Into::into).unwrap_or_default(),
treeshake: TreeShakeOptions::default(),
max_iterations: None,
})
}
}
Expand Down
Loading