-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
CommonJS modules incorrectly overwritten #3363
Comments
Created internal issue http://b/132180272 |
I find the presence of 'default' in the output suspicious. @ChadKillingsworth could you take a look? Thanks. |
Thanks for the repo case. With type checking enabled, this warning is the clue to the problem:
The CommonJS pass isn't recognizing the assignment to the export, so it marks the export as CONST and in ADVANCED mode the compiler then inlines the references. |
I went back to look at this again. We don't support re-using the import variable in that way. Doing that would require full flow analysis. The pass specifically tries to avoid creating aliases so references to the import variable are re-targeted to the host module directly. To support this type of flow, you'd need: // a-or-b.js
var a = require('./a.js');
var aOrB = a;
if (global.useB) {
aOrB = 'b';
}
module.exports = aOrB; |
Version
(tested on v20190507-nightly as well)
Input
Command
Output
Issue
In the compiled version of
a-or-b.js
(that is, a CommonJS module that imports something and then overwrites the variable containing that imported module with something else), Closure incorrectly inlines the value ofrequire('./a.js')
anywhere the variableaOrB
is referenced (even without optimizations being enabled). This has the side effect of overwriting the export ofa.js
everywhere.The line
module$a.default = "b";
is expected to beaOrB$$module$a_or_b = "b";
(andmodule$a_or_b.default = module$a.default;
is expected to bemodule$a_or_b.default = aOrB$$module$a_or_b;
The text was updated successfully, but these errors were encountered: