Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Math["a"] = "blah";
Math.a();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Math["a"] = "blah";
Math.a();
26 changes: 25 additions & 1 deletion packages/babel-plugin-minify-builtins/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ module.exports = function({ types: t }) {
const context = this;

const collectVisitor = {
AssignmentExpression(path) {
const left = path.get("left");

// Should bail and not run the plugin
// when builtin is polyfilled
if (t.isMemberExpression(left) && isBuiltInComputed(left)) {
let parent = path;
do {
parent.stop();
} while ((parent = parent.parentPath));
}
},

MemberExpression(path) {
if (path.parentPath.isCallExpression()) {
return;
Expand All @@ -46,7 +59,7 @@ module.exports = function({ types: t }) {
return;
}

// computed property should be not optimized
// computed property should not be optimized
// Math[max]() -> Math.max()
if (!isComputed(callee) && isBuiltin(callee)) {
const result = evaluate(path, { tdz: context.tdz });
Expand Down Expand Up @@ -115,6 +128,17 @@ module.exports = function({ types: t }) {
return result;
}

function isBuiltInComputed(memberExpr) {
const { node } = memberExpr;
const { object, computed } = node;

return (
computed &&
t.isIdentifier(object) &&
VALID_CALLEES.indexOf(object.name) >= 0
);
}

function isBuiltin(memberExpr) {
const { object, property } = memberExpr.node;

Expand Down