Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect TSC polyfills to avoid marking them as CJS #9318

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,25 @@ impl Visit for Collect {
self.used_imports.insert(id!(ident));
}
}
Expr::Bin(bin_expr) => {
// Some TSC polyfills use a pattern like below.
// We want to avoid marking these modules as CJS
// e.g. var _polyfill = (this && this.polyfill) || function () {}
if matches!(*bin_expr.left, Expr::This(..)) {
mattcompiles marked this conversation as resolved.
Show resolved Hide resolved
match &*bin_expr.right {
Expr::Member(member_expr) => {
if matches!(*member_expr.obj, Expr::This(..))
&& matches!(member_expr.prop, MemberProp::Ident(..))
{
return;
}
}
_ => {}
}
}

node.visit_children_with(self);
}
_ => {
node.visit_children_with(self);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,32 @@ mod tests {
assert!(collect.should_wrap);
}

#[test]
fn collect_has_cjs_exports() {
let (collect, _code, _hoist) = parse(
r#"
module.exports = {};
"#,
);
assert!(collect.has_cjs_exports);

let (collect, _code, _hoist) = parse(
r#"
this.someExport = 'true';
"#,
);
assert!(collect.has_cjs_exports);

// Some TSC polyfills use a pattern like below.
// We want to avoid marking these modules as CJS
let (collect, _code, _hoist) = parse(
r#"
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function () {}
"#,
);
assert!(!collect.has_cjs_exports);
}

#[test]
fn collect_should_wrap() {
let (collect, _code, _hoist) = parse(
Expand Down