Skip to content

Commit

Permalink
fix(minifier): do not join require calls for cjs-module-lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 13, 2024
1 parent a226962 commit 7ca96c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ impl<'a> Expression<'a> {
_ => false,
}
}

pub fn is_require_call(&self) -> bool {
if let Self::CallExpression(call_expr) = self {
call_expr.is_require_call()
} else {
false
}
}
}

impl<'a> IdentifierName<'a> {
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_minifier/src/ast_passes/collapse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ impl<'a> Collapse<'a> {
Statement::VariableDeclaration(prev_decl),
) = (cur, prev)
{
// Do not join `require` calls for cjs-module-lexer.
if cur_decl
.declarations
.first()
.and_then(|d| d.init.as_ref())
.is_some_and(Expression::is_require_call)
{
break;
}
if cur_decl.kind == prev_decl.kind {
if i - 1 != range.end {
range.start = i - 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/CollapseVariableDeclarations.java>
use crate::CompressOptions;

fn test(source_text: &str, expected: &str) {
let options = CompressOptions::all_true();
crate::test(source_text, expected, options);
}

fn test_same(source_text: &str) {
test(source_text, source_text);
}

#[test]
fn cjs() {
test_same(
"
Object.defineProperty(exports, '__esModule', { value: true });
var compilerDom = require('@vue/compiler-dom');
var runtimeDom = require('@vue/runtime-dom');
var shared = require('@vue/shared');
",
);
}
1 change: 1 addition & 0 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod collapse_variable_declarations;
mod fold_conditions;
mod fold_constants;
mod remove_dead_code;
Expand Down

0 comments on commit 7ca96c7

Please sign in to comment.