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
Avoid compiling the _typeof helper with itself #11049
Avoid compiling the _typeof helper with itself #11049
Conversation
resolvePath(path, (err, path) => (err ? reject(err) : resolve(path))), | ||
); | ||
const readFile = path => | ||
new Promise((resolve, reject) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be refactored using util.promisify
Example:
const readFile = path => util.promisify(fs.readFile)(path, "utf8")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not supported by Node 6.
When we'll drop it, we'll directly use fs.promises
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure bundlers aren't just going to strip the directive?
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. |
Hi! when is this getting released? |
Tomorrow probably 😁 |
* Avoid compiling the typeof helper with itself * Update fixtures
We already had some logic to prevent the
typeof-symbol
plugin 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_typeof
helper name._typeof2
and_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
isUnderHelper
logic to make it work with@babel/runtime
, but:@babel/runtime
is 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/typeof
The 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: false
option 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