Avoid compiling the _typeof helper with itself#11049
Merged
nicolo-ribaudo merged 2 commits intobabel:masterfrom Jan 27, 2020
Merged
Avoid compiling the _typeof helper with itself#11049nicolo-ribaudo merged 2 commits intobabel:masterfrom
nicolo-ribaudo merged 2 commits intobabel:masterfrom
Conversation
soryy708
reviewed
Jan 23, 2020
| resolvePath(path, (err, path) => (err ? reject(err) : resolve(path))), | ||
| ); | ||
| const readFile = path => | ||
| new Promise((resolve, reject) => |
There was a problem hiding this comment.
Can be refactored using util.promisify
Example:
const readFile = path => util.promisify(fs.readFile)(path, "utf8")
Member
Author
There was a problem hiding this comment.
No, it's not supported by Node 6.
When we'll drop it, we'll directly use fs.promises.
jridgewell
approved these changes
Jan 23, 2020
Member
jridgewell
left a comment
There was a problem hiding this comment.
Are we sure bundlers aren't just going to strip the directive?
Member
Author
|
If the file is already bundled, then Babel can't reference it and wouldn't introduce a circular dependency but only duplicate the helper. Anyway, I don't think that bundlers strip directives, especially if they are inside a nested function. |
|
Don't assume, verify. |
Member
Author
existentialism
approved these changes
Jan 27, 2020
|
Hi! when is this getting released? |
Member
Author
|
Tomorrow probably 😁 |
rajasekarm
pushed a commit
to rajasekarm/babel
that referenced
this pull request
Feb 17, 2020
* Avoid compiling the typeof helper with itself * Update fixtures
This was referenced Feb 24, 2020
This was referenced Mar 7, 2020
This was referenced Mar 14, 2020
This was referenced Mar 25, 2020
This was referenced Apr 2, 2020
This was referenced Apr 9, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We already had some logic to prevent the
typeof-symbolplugin from transpiling its own helper. It worked by generating the helper name and checking if the current path is inside a function or a variable declaration with that name.However, it had a few problems:
@babel/runtime, because the generated name is a unique identifier so it's guaranteed not to be equal to the_typeofhelper name._typeof2and_typeof3), causing circular dependencies. This is probably caused by running twice the plugin, but we can avoid failing in that case.I initially hard-coded the helper name in the
isUnderHelperlogic to make it work with@babel/runtime, but:@babel/runtimeis bundled in an app/library byrollup, the function name might changeI then decided to introduce a
"@babel/helpers - typeof"directive, which has a few advantage:@babel/runtime/helpers/typeofThe directive still doesn't work with Terser and UglifyJS because they strip them by default. We could either suggest to whoever finds this bug to set the
directives: falseoption in their minifier settings, or we could ask UglifyJS and Terser's maintainers if they could keep this specific directive in the minified output.This PR makes it safe to transpile
@babel/runtime, which should provide a solution for #9903