Skip to content

Commit

Permalink
fix(minifier): handle Object.definedPropert(exports for @babel/type…
Browse files Browse the repository at this point in the history
…s/lib/index.js (#4933)

Discoverd in `monitor-oxc`

```javascript
Object.keys(_index6).forEach(function(key) {
	if (key === "default" || key === "__esModule") return;
	if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
	if (key in exports && exports[key] === _index6[key]) return;
	Object.defineProperty(exports, key, {
		enumerable: true,
		get: function() {
			return _index6[key];
		}
	});
});
```

🙃
  • Loading branch information
Boshen committed Aug 16, 2024
1 parent 248a757 commit 46cb1c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_syntax::{
number::NumberBase,
operator::{BinaryOperator, UnaryOperator},
};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::{CompressOptions, CompressorPass};

Expand Down Expand Up @@ -56,10 +56,7 @@ impl<'a> Traverse<'a> for SubstituteAlternateSyntax<'a> {
call_expr: &mut CallExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
// Check if this call expression is a top level `ExpressionStatement`.
// NB: 1 = global, 2 = Program, 3 = ExpressionStatement
if ctx.ancestors_depth() == 3
&& matches!(ctx.parent(), Ancestor::ExpressionStatementExpression(_))
if ctx.parent().is_expression_statement()
&& Self::is_object_define_property_exports(call_expr)
{
self.in_define_export = true;
Expand Down
10 changes: 0 additions & 10 deletions crates/oxc_minifier/tests/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ fn test_same(source_text: &str) {

#[test]
fn cjs() {
// Export is undefined when `enumerable` is "!0".
// https://github.com/nodejs/cjs-module-lexer/issues/64
test_same(
"Object.defineProperty(exports, 'ConnectableObservable', {
enumerable: true,
get: function() {
return ConnectableObservable_1.ConnectableObservable;
}
});",
);
// Bail `cjs-module-lexer`.
test_same("0 && (module.exports = { version });");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ fn test(source_text: &str, expected: &str) {
crate::test(source_text, expected, options);
}

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

// Oxc

#[test]
fn cjs() {
// Bail `cjs-module-lexer`.
// Export is undefined when `enumerable` is "!0".
// https://github.com/nodejs/cjs-module-lexer/issues/64
test_same(
"Object.defineProperty(exports, 'ConnectableObservable', {
enumerable: true,
get: function() {
return ConnectableObservable_1.ConnectableObservable;
}
});",
);
// @babel/types/lib/index.js
test_same(
r#"Object.keys(_index6).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _index6[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
return _index6[key];
}
});
});"#,
);
}

// Google Closure Compiler

#[test]
fn fold_return_result() {
test("function f(){return !1;}", "function f(){return !1}");
Expand Down

0 comments on commit 46cb1c1

Please sign in to comment.