Skip to content

Commit 743488a

Browse files
committed
fix(minifier): remove unused import source/defer statements
1 parent d209c21 commit 743488a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

crates/oxc_minifier/src/peephole/remove_unused_declaration.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ impl<'a> PeepholeOptimizations {
140140
let Statement::ImportDeclaration(import_decl) = stmt else { return };
141141

142142
if import_decl.phase.is_some() {
143+
if ctx.scoping().symbol_is_unused(
144+
import_decl.specifiers.as_ref().unwrap().first().unwrap().local().symbol_id(),
145+
) {
146+
*stmt = ctx.ast.statement_empty(import_decl.span);
147+
ctx.state.changed = true;
148+
}
149+
143150
return;
144151
}
145152

@@ -345,4 +352,26 @@ mod test {
345352
test_same_options("import * as a from 'a'; eval('a');", &options);
346353
test_same_options("import { a } from 'a'; function f() { eval('a'); }", &options);
347354
}
355+
356+
#[test]
357+
fn remove_unused_import_source_statement() {
358+
let options = CompressOptions::smallest();
359+
360+
test_options("import source a from 'a'", "", &options);
361+
test_options("import source a from 'a'; if (false) { console.log(a) }", "", &options);
362+
test_same_options("import source a from 'a'; foo(a);", &options);
363+
}
364+
365+
#[test]
366+
fn remove_unused_import_defer_statements() {
367+
let options = CompressOptions::smallest();
368+
369+
test_options("import defer * as a from 'a'", "", &options);
370+
test_options(
371+
"import defer * as a from 'a'; if (false) { console.log(a.foo) }",
372+
"",
373+
&options,
374+
);
375+
test_same_options("import defer * as a from 'a'; foo(a.bar);", &options);
376+
}
348377
}

0 commit comments

Comments
 (0)