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

Implement minify-builtins plugin #410

Merged
merged 10 commits into from
Feb 16, 2017
Merged

Conversation

vigneshshanmugam
Copy link
Member

@vigneshshanmugam vigneshshanmugam commented Feb 8, 2017

fixes #363

@boopathi
Copy link
Member

boopathi commented Feb 9, 2017

Thoughts: I think we could also help constant-propagation by evaluating the expressions if the arguments are constants ?

let a = 1.8, b = 2.5; // (2) becomes deadcode
let x = Math.floor(Math.max(a, b)); // (1) evaluate
foo(x);

could be transformed to

foo(2);

@vigneshshanmugam
Copy link
Member Author

vigneshshanmugam commented Feb 9, 2017

@boopathi I like the idea in general, I believe it shouldn't cause any issues..

Shall i create a new issue for that and add it as part of new PR?

@boopathi
Copy link
Member

boopathi commented Feb 9, 2017

Part of this PR would be great.

@boopathi boopathi added the Tag: New Feature Pull Request adding a new feature label Feb 9, 2017
@vigneshshanmugam
Copy link
Member Author

Handled the optimisations.

@@ -0,0 +1,79 @@
module.exports = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way babel does this is to maintain only [String, Number, Math]

https://github.com/babel/babel/blob/f8ffe03/packages/babel-traverse/src/path/evaluation.js#L5

So, I think it's relatively safe to assume that Math.x will be a side-effect free evaluation except Math.random

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh good one.. What about Date.x?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can chuck the Date for now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.. then will update


const expName = getExpressionName(callee);
if (isBuiltin(expName)) {
const result = evaluate(path);
Copy link
Member

@boopathi boopathi Feb 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to deopt when we have side-effecty evaluate-able arguments

for example,

Math.floor((foo(), 1));

@@ -0,0 +1,51 @@
# babel-plugin-transform-built-ins
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just call it babel-plugin-minify-builtins

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm.. dono really..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just change it to babel-plugin-minify-builtins

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

const evaluate = require("babel-helper-evaluate-path");



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty lines

}

const expName = getExpressionName(callee);
if (isBuiltin(expName)) {
Copy link
Member

@boopathi boopathi Feb 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check if it's a computed property

Will fail for Math[max]() - will assume it as Math.max().

let max = "floor";
Math[max](1.5);


if (t.isIdentifier(object)) result += object.name;
if (t.isMemberExpression(object)) result += memberToString(object);
if (t.isIdentifier(property)) result += property.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check for computed properties. same as above.

@vigneshshanmugam vigneshshanmugam changed the title Implement transform-built-ins plugin Implement minify-builtins plugin Feb 16, 2017
@vigneshshanmugam
Copy link
Member Author

@boopathi Addressed all the issues.

@boopathi boopathi merged commit 7f66b97 into babel:master Feb 16, 2017
@vigneshshanmugam vigneshshanmugam deleted the built-ins branch February 16, 2017 21:59
@FezVrasta
Copy link

Is this already available in 0.0.11?

@vigneshshanmugam
Copy link
Member Author

Nope.. Not released yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tag: New Feature Pull Request adding a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create aliases for built in functions
3 participants