-
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
Top level variables not dead code eliminated #1183
Comments
The reason is that |
Is there a way to declare purity, to allow dead code elimination? E.g. const a = /*#__PURE__*/ b; const {
/*#__PURE__*/ useDebugValue
} = /*#__PURE__*/ require('react'); |
At the very least, the aliasing done by the minifier could be eliminated: - const { useDebugValue: a } = require('react');
+ const { useDebugValue } = require('react'); |
Yes. Pure comments apply to call expressions, not to identifier expressions. So you could do this: const a = /*#__PURE__*/ (() => b)(); |
@evanw I managed to avoid the original dilemma by not destructing I've tried making sure the const PropTypes = /*#__PURE__*/ require('prop-types'); Yet, with something like: if (typeof process === 'object' && process.env.NODE_ENV !== 'production') {
Foo.propTypes = {
bar: PropTypes.bool,
};
} In the production bundle all the uses of I'd raised a |
My answer in #1183 (comment) was general-purpose. Replace const PropTypes = /*#__PURE__*/ (() => require('prop-types'))(); I'm going to close issue this since a) there is already a way to mark top-level code as pure and b) the issue facebook/prop-types#350 about marking |
Using
bundle: true
andminify: true
, with an entry point containing only this results in the variable being eliminated:But this results in no elimination (assume
b
is a global):It should be eliminated just the same.
A similar problem manifests when destructuring. This whole statement gets eliminated:
While this does not:
I think this bug is the root reason why
useDebugValue
destructured fromrequire('react')
is not eliminated as dead code in my bundle, even when it's use within the module is eliminated:The text was updated successfully, but these errors were encountered: