diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index e01b97d67..e7cc166fa 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -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> { diff --git a/crates/oxc_minifier/src/ast_passes/collapse.rs b/crates/oxc_minifier/src/ast_passes/collapse.rs index b59a3814b..19375ed7f 100644 --- a/crates/oxc_minifier/src/ast_passes/collapse.rs +++ b/crates/oxc_minifier/src/ast_passes/collapse.rs @@ -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; diff --git a/crates/oxc_minifier/tests/ast_passes/collapse_variable_declarations.rs b/crates/oxc_minifier/tests/ast_passes/collapse_variable_declarations.rs new file mode 100644 index 000000000..18f6e85af --- /dev/null +++ b/crates/oxc_minifier/tests/ast_passes/collapse_variable_declarations.rs @@ -0,0 +1,25 @@ +//! + +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() { + // Do not join `require` calls for cjs-module-lexer. + 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'); + ", + ); +} diff --git a/crates/oxc_minifier/tests/ast_passes/mod.rs b/crates/oxc_minifier/tests/ast_passes/mod.rs index fb32b2f16..1ecc67f1b 100644 --- a/crates/oxc_minifier/tests/ast_passes/mod.rs +++ b/crates/oxc_minifier/tests/ast_passes/mod.rs @@ -1,3 +1,4 @@ +mod collapse_variable_declarations; mod fold_conditions; mod fold_constants; mod remove_dead_code;