Skip to content
Closed
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
6 changes: 6 additions & 0 deletions crates/oxc_minifier/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct CompressOptions {

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

/// Remove redundant or non-standard directives
pub directives: bool,
}

impl Default for CompressOptions {
Expand All @@ -75,6 +78,7 @@ impl CompressOptions {
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
directives: true,
}
}

Expand All @@ -90,6 +94,7 @@ impl CompressOptions {
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
directives: true,
}
}

Expand All @@ -105,6 +110,7 @@ impl CompressOptions {
treeshake: TreeShakeOptions::default(),
drop_labels: FxHashSet::default(),
max_iterations: None,
directives: true,
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions crates/oxc_minifier/src/peephole/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl<'a> Normalize {
}

impl<'a> Traverse<'a, MinifierState<'a>> for Normalize {
fn exit_program(&mut self, node: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
if node.source_type.is_module() {
fn exit_program(&mut self, node: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if ctx.state.options.directives && node.source_type.is_module() {
node.directives.drain_filter(|d| d.directive.as_str() == "use strict");
}
}
Expand Down Expand Up @@ -695,4 +695,30 @@ mod test {
test("'use strict'; function _() { 'use strict' }", "function _() {}");
test("'use strict';", "");
}

#[test]
fn not_remove_unused_use_strict_directive_when_directives_disabled() {
use oxc_span::SourceType;
let mut options = default_options();
options.directives = false;
let source_type = SourceType::cjs();
test_options_source_type(
"'use strict'; function _() { 'use strict' }",
"'use strict'; function _() { }",
source_type,
&options,
);
test_options_source_type(
"function _() { 'use strict'; function __() { 'use strict' } }",
"function _() { 'use strict'; function __() { } }",
source_type,
&options,
);
test_options(
"'use strict'; function _() { 'use strict' }",
"'use strict'; function _() {}",
&options,
);
test_options("'use strict';", "'use strict';", &options);
}
}