-
Notifications
You must be signed in to change notification settings - Fork 629
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
Constant folding breaks some already minified code #291
Comments
I confirm this is a problem. I ran into it as well with minified Google Closure code. Thanks @chpill for the patch, it worked as a temp solution. |
I can confirm this issue seems to still exist in the latest version of Metro, I did not hit this with minified code but with hand written library code so I managed to reduce it to a simple test case, and it seems to be caused by a function declaring a variable shadowing its own name: function foo() {
let foo;
}
// Side Effect
console.log(foo); With |
Summary: **Summary** This is a possibly incomplete fix for #291, it corrects at least one issue with name shadowing but there might be other problems present in the very large code payload presented in the original issue. The exact bug fixed here happens when a function declares an unused variable shadowing its own name: ```js export function foo() { let foo; } ``` The current code checks whether that the binding corresponding to the function is referenced in the scope of the FunctionDeclaration node, however in Babel that scope is situated inside the body of the function and not in the outer program. Since the binding is retrieved by name, Babel returns informations related to the inner, unreferenced variable and thus the entire function is removed. This fix changes the biding retrieval to read from the parent scope of the FunctionDefinition, which returns the correct binding. **Test plan** An additional test case has been added to verify the function remains in the final output. Pull Request resolved: #643 Reviewed By: MichaReiser Differential Revision: D27324443 Pulled By: motiz88 fbshipit-source-id: 30a9b7244846ad0ff4ba080c96e81dc6f582e5e5
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
See https://github.com/chpill/repro-bug-metro
The
index.js
file contains JS compiled from Clojurescript and JS optimized by google closure compiler. The minimalist app boots without issue when__DEV__ == false
or whenminify == true
.But when
__DEV == false && minify == true
, the app crashes with a JS error because of an undefined variable (ReferenceError: $G__1978__22$$ is not defined
).It turns out the issue comes from the constant folding optimization when applied on our already optimized/minified index.js
If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can
yarn install
andyarn test
.To reproduce the bug:
git clone https://github.com/chpill/repro-bug-metro
react-native start --reset-cache
react-native run-android
then check that the app boots (you should see a green screen)__DEV == false && minify == true
and reloadReferenceError: $G__1978__22$$ is not defined
To see that the problems comes from constant folding on our index.js:
metro/packages/metro/src/JSTransformer/worker.js
Lines 241 to 244 in 612d4d7
react-native start --reset-cache
What is the expected behavior?
The constant folding optimization should not produce invalid JS code, or there should be a way to opt out of it for specific files.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
The text was updated successfully, but these errors were encountered: