Annotating transformed classes with #__PURE__ comment#6209
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/4825/ |
|
Does UglifyJS remove this? var A = /*#__PURE__*/ /* foo */ function() { /* ... */ }(X);Which is the output for var A = /* foo */ class A extends X {} |
|
Also, maybe this should be enabled using an option since not everyone uses UglifyJS after Babel? |
Unfortunately not. They explicitly check only the last comment. I might poke around in the UglifyJS repo as well to make this work, or at least investigate why it doesnt.
Should be easy to add
Ive also thought about this, however:
|
|
It's fine as a default since it's a comment anyway, I was just confused as to what the comment is supposed to be? I thought it was |
| var Foo = function (_Bar) { | ||
| var Foo = | ||
| /*#__PURE__*/ | ||
| function (_Bar) { |
There was a problem hiding this comment.
This is not important... but is there a reason why this is on 3 lines instead of 1?
There was a problem hiding this comment.
its just how babel-generator spits out code from the AST, looks weird to me too, but 🙄
There was a problem hiding this comment.
It's probably just because we removed some logic with newlines using tokens/comments so it's like that now
|
Seems fine. I assume @Andarist verified the result against uglify. Will non-standard static class properties be generated within the IIFE as per #5632 (comment)? |
| import VanillaTransformer from "./vanilla"; | ||
| import nameFunction from "babel-helper-function-name"; | ||
|
|
||
| const PURE_ANNOTATION = "#__PURE__"; |
There was a problem hiding this comment.
We could use this across multiple plugins? We might want to add it in a helper.
There was a problem hiding this comment.
@xtuc sure thing, whats the best place to add such?
There was a problem hiding this comment.
Too late but I had something like babel-help-emit-pure-annotation in mind but I'm not sure it will be used apart from here.
There was a problem hiding this comment.
I would just make it when we need/get to it. It's not a breaking change to swap it out either
I plan to work on it too, need to establish how it should be done though. The problem I see is that those 2 plugins are independent but they need to work together here somehow. Ideally also we could have 2 different outputs for:
|
It seems that |
I ran into the same issue when hacking together the proof of concept. I did not know how Babel plugins are supposed to communicate/coordinate with each other - via Babylon AST only? Maybe just eliminate |
@Andarist Do you know if this got solved anywhere? I'm still seeing class properties outside of the IIFE which is breaking treeshaking. |
|
You could try this https://github.com/emotion-js/emotion/blob/64a39f1eb6abf5afe6949b702436423938f34fd0/scripts/build/fix-dce-for-classes-with-statics.js , just keep in mind that this might break your code - it's not overly tested 😉 I unfortunately couldn't find time yet to revive my old PR to fix this issue, I was hoping that this PR would get merged in #8130 which would the whole WAY easier. |
This is a simple change which annotate created by
transform-es2015-classesIIFEs with#__PURE__comments. This allows UglifyJS to remove those IIFE calls if their result is unused - generally speaking it allows for better Dead Code Elimination.The core of the change is here. The rest are only adjusted tests.