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

[Feature Request] enable even simpler transformation #4840

Closed
bigopon opened this issue Nov 14, 2016 · 3 comments · Fixed by GulajavaMinistudio/babel#96

Comments

@bigopon
Copy link

@bigopon bigopon commented Nov 14, 2016

Please ignore if this already discussed.
Suppose I have a class like this:

class MyClass {
   //....
   // Here I have 100 class methods
}

This will be transformed into:

function MyClass() {}
// 100 times of this repeated: MyClass.prototype

Can we have something like this instead:

var _proto = MyClass.prototype
// 1000 class methods ?
_proto.method1 = () => null;
_proto.method2 = () => null;

// a lot less output code after minification
// var a = MyClass.prototype;
// a.method1 = () => null;
// a.method2 = () => null;

Imagine if we have 20 of classes like this, the amount of prototype repeated will be big, not counted the long class names

Can we have it enhanced in this way ?

@hzoo

This comment has been minimized.

Copy link
Member

@hzoo hzoo commented Nov 15, 2016

We could do that - it only uses prototype in loose mode btw. It would probably be low priority for us since it's not a bug fix or core feature

@bigopon

This comment has been minimized.

Copy link
Author

@bigopon bigopon commented Nov 15, 2016

I could help with some guideline of where to start, if you are interested.

@hzoo

This comment has been minimized.

Copy link
Member

@hzoo hzoo commented Nov 17, 2016

Yeah sure! - you can also join our slack #development #plugins as well slack.babeljs.io

I would check our CONTRIBUTING.md, https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md, and test with astexplorer (http://astexplorer.net/#/sskXYR4icD/20)

This is going to touch loose mode so there's a loose.js from index.js

if (!node.static) classRef = t.memberExpression(classRef, t.identifier("prototype"));
let methodName = t.memberExpression(classRef, node.key, node.computed || t.isLiteral(node.key));

Looks like that is the place where it's doing a .prototype and creating the A.prototype.a = function a() {};

Instead your want to do a one time addition of var _proto = MyClass.prototype at the top and then change the new methods to be _proto.method = function a() {}

@hzoo hzoo added the help wanted label Mar 23, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Mar 28, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Mar 28, 2017
@babel-bot babel-bot added the Has PR label Mar 28, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Jul 21, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Jul 21, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Jul 21, 2017
oliverdon added a commit to oliverdon/babel that referenced this issue Jul 21, 2017
jridgewell added a commit that referenced this issue Aug 27, 2017
* Fix #4840: Alias class prototype for methods in loose mode

* Cleanup
GulajavaMinistudio added a commit to GulajavaMinistudio/babel that referenced this issue Aug 27, 2017
Fix babel#4840: Alias class prototype for methods in loose mode (babel#5560)
@lock lock bot added the outdated label May 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.