Open Macil opened 5 years ago
Hey @Macil! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.
If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."
My team experienced this issue as well in a large code base. Giving the evaluate: false
option to turn off the babel-plugin-minify-constant-folding solves the issue, so it would seem that it is related to that one.
I can reproduce it with your example. For my notes, here's what it looks like with evaluate: false
as @wessberg mentioned:
"use strict";
function Main() {
if (!(0.5 < Math.random()))
for (var template, templates = [], _i = 0, _templates = templates; _i < _templates.length; _i++)
(template = _templates[_i]), template.foo();
}
The preset-env
preset is running first and transforming it into this:
"use strict";
function Main() {
if (Math.random() > 0.5) {
return;
}
var templates = [];
for (var _i = 0, _templates = templates; _i < _templates.length; _i++) {
var template = _templates[_i];
template.foo();
}
}
Bug Report
Current Behavior babel-preset-minify breaks for-of loops in some cases. It seems like there's two necessary conditions:
Input Code
Current Output
Here's the buggy output prettified:
Note the presence of
(void 0).length
and(void 0)[_i]
, which will always cause exceptions. Each case of(void 0)
should betemplates
.Babel Configuration (.babelrc, package.json, cli command)
Environment
Additional context I ran into this issue while using Storybook, which includes babel-preset-minify by default in production builds.