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

perf(builtins): run builtins transform on multi pass #859

Merged
merged 6 commits into from May 17, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 13 additions & 17 deletions packages/babel-plugin-minify-builtins/src/index.js
Expand Up @@ -8,19 +8,12 @@ const INVALID_METHODS = ["random"];

module.exports = function({ types: t }) {
class BuiltInReplacer {
constructor(program, { tdz }) {
this.program = program;
this.tdz = tdz;
constructor() {
// map<expr_name, path[]>;
this.pathsToUpdate = new Map();
}

run() {
this.collect();
this.replace();
}

collect() {
getCollectVisitor() {
const context = this;

const collectVisitor = {
Expand Down Expand Up @@ -55,7 +48,7 @@ module.exports = function({ types: t }) {
},

CallExpression: {
exit(path) {
exit(path, { opts: { tdz = false } = {} }) {
const callee = path.get("callee");
if (!callee.isMemberExpression()) {
return;
Expand All @@ -66,7 +59,7 @@ module.exports = function({ types: t }) {
// computed property should not be optimized
// Math[max]() -> Math.max()
if (!isComputed(node) && isBuiltin(node)) {
const result = evaluate(path, { tdz: context.tdz });
const result = evaluate(path, { tdz });
// deopt when we have side effecty evaluate-able arguments
// Math.max(foo(), 1) --> untouched
// Math.floor(1) --> 1
Expand All @@ -81,7 +74,7 @@ module.exports = function({ types: t }) {
}
};

this.program.traverse(collectVisitor);
return collectVisitor;
}

replace() {
Expand Down Expand Up @@ -111,14 +104,17 @@ module.exports = function({ types: t }) {
}
}

const builtInReplacer = new BuiltInReplacer();

return {
name: "minify-builtins",
visitor: {
Program(path, { opts: { tdz = false } = {} }) {
const builtInReplacer = new BuiltInReplacer(path, { tdz });
builtInReplacer.run();
visitor: Object.assign({}, builtInReplacer.getCollectVisitor(), {
Program: {
exit() {
builtInReplacer.replace();
}
}
}
})
};

function memberToString(memberExprNode) {
Expand Down