-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Partially removed default export produces invalid syntax #1772
Comments
Thanks for the issue. I hope we find some time soon to get to the bottom of this. To anyone who wants to take a look at this: This is probably related to something happening in the rendering function for default exports. I guess in the presence of unused default exports with side-effects, we would still need to introduce a variable if the expression is not a statement. |
Actually, we might also consider if we could interpret a default export with an object literal the same way as a namespace export. If we slightly adjust |
I had wondered why they generated different code in |
To just fix the issue at hand, adding parentheses should suffice: {x: 1} // BlockStatement that contains illegal syntax
({ x: 1 }) // ExpressionStatement that contains an ObjectExpression Then we could do the rest as a separate enhancement if we want to. |
@lukastaegert see this repl, not work |
Which part isn't working? |
BTW, rollup does not yet know that [].map does not have side-effects. That is quite a different issue and has nothing to do with the issue at hand. |
If rollup were to assume no side effects for |
Rollup has always had an implicit assumption that it's generating code for an ES6 platform with unmodified built-in classes. |
in my opinion, i think all these will be removed by tree-shaking so this issue may focus on default export object-literal that may have side-effect |
Again: Why do you think this is not working? When I paste this into the console of my browser, it is recognised as valid syntax (I have changed it a bit so that we have an actual side-effect): ({
map: console.log('effect') || []
}) it just doesn't look very valid but your JS engine will probably not care about looks 😜 (our parser agrees with me here as well). Or is this incompatible with older interpreters? I am also asking as I think the fix on our side should be to add the parentheses for you in the future if necessary. As for the side-effect detection, the relevant issue here in my eyes is #673 so discussions about this should probably continue there. Just note that |
ok, thank you very much! it's my fault. // I often write this
let someVar = {
obj: 'literal'
}
// not just this
{
obj: 'literal'
}
// or even this
({
obj: 'literal'
}) create then drop the obj-literal, it's truly valid! 😀 |
No problem, I understand it's confusing. No-one would write a literal without a variable. It is just in this specific situation where the literal definition might have a side-effect that rollup cannot simply remove it and we need a way to keep the expression without producing invalid syntax. Maybe in the future we can remove even more code so that just the side-effect itself will remain in the code. |
I have updated the issue name to make it easier for us to reference it. |
You may be right, @kzc. Even if that’s the case, it would have to do type flow analysis to determine that something is an array except in trivial cases like this one. To my knowledge, rollup doesn’t do that (yet). |
As long as variables are not reassigned, we already have type flow analysis now, and it's working quite well 😜. If a variable or object key is reassigned, it is marked as "unknown" in the type. This is the magic done by the |
I guess I need to pay more attention 😉 Fortunately, I finally got a Mac again after not having a personal computer capable of software development for most of this year. I'll have to take another look at the source code! |
This was only added in Rollup@0.51.0. It is actually somewhat hidden. Basically, a variable or object property is considered an "ArrayExpression" if the only thing assigned to it is something that is itself considered to be an "ArrayExpression" (including of course ArrayExpressions themselves). Thus, Rollup now strongly favours immutability. |
version=0.52.0
see the rollupjs.org/repl repro
or https://github.com/hisland/rollup-issue
The text was updated successfully, but these errors were encountered: