Open filips123 opened 2 years ago
see https://github.com/parcel-bundler/parcel/pull/8158,
~~ this bug was fixed if specified react-error-overlay to version 6.0.11 ~~
for your test code it seems they also introduced some bug this the process.env check
This happens both in 2.1.1 and 2.6.0, although it doesn't add process as a dependency in 2.1.1.
I'm pretty sure it would've been an issue back in 2.1.1 as well but was not noticed because at that time Node built-ins were already Parcel dependencies, though I may be completely wrong on that.
This issue can probably be resolved by supporting typeof process
within our constant expression evaluator.
I'm looking for a way to make my library run both in bundlers that do node emulation and in bundlers that don't, I take from this issue that that's currently not possible?
This issue can probably be resolved by supporting typeof process within our constant expression evaluator.
This would be great.
I'm getting this output with parcel 2.0.0-nightly.1347+824ddbe70
so I assume it's still an issue?
I've updated my code sample repository to Parcel 2.11.
TLDR: Part of this issue is still present.
The issue with installing polyfill is still present in Parcel 2.11.0:
@parcel/resolver-default: Auto installing polyfill for Node builtin module "process/"...
node_modules\semver\internal\debug.js:2:10
1 | const debug = (
> 2 | typeof process === 'object' &&
> | ^^^^^^^ used here
3 | process.env &&
4 | process.env.NODE_DEBUG &&
ᅵ Learn more: https://parceljs.org/features/node-emulation/#polyfilling-%26-excluding-builtin-node-modules
Installing process...
This can be seen when using semver
package in the main
branch.
A more minimal example that contains just the relevant code can be seen in the process-with-check
branch. It also seems that the generated code in Parcel 2.11.0 is slightly longer than in 2.6.0. I don't know if that is considered to be an issue.
The issue with not eliminating debug
function has been resolved. This can be seen in the process-without-check
branch.
However, when using "alias": { "process": false }
to disable the process
polyfill, the dead code elimination still isn't working:
(({}).env,()=>{})("Hello World");
I've pushed an example to the process-with-check-disable-alias
branch.
Should that be opened as a separate issue?
ð Bug Report
Checking
typeof process
orprocess.env
causes Parcel to unnecessarily polyfillprocess
module, even if the code only usesprocess.env.SOME_ENV
. Additionally, in 2.6.0, tree shaking does not work completely even without those checks.I noticed this because I use
semver
package which contains this code:It only uses
process.env.NODE_DEBUG
to determine whether debug should be enabled, soprocess
should not be polyfilled and the whole function should be eliminated from production builds when debug is not enabled.ð Configuration (.babelrc, package.json, cli command)
See repo in code sample section below.
ð€ Expected Behavior
process
polyfill when the code checkstypeof process
orprocess.env
./\bsemver\b/i.test(process.env.NODE_DEBUG)
is false and eliminate the whole function and its calls.ð¯ Current Behavior
Parcel thinks
process
module is used and addsprocess
package polyfill:This happens both in 2.1.1 and 2.6.0, although it doesn't add
process
as a dependency in 2.1.1.Parcel 2.6.0 does not eliminate
debug
function completely when building for production, even if problematic checks are removed.Source:
Parcel 2.1.1 (works fine, empty file, except sourcemap comment):
Parcel 2.6.0 (does not remove
debug
function):ðŠ Context
In this case it adds a few KB to file size. However, it may add even more unneeded code in some cases, especially when treeshaking wouldn't work properly.
ð» Code Sample
ð Your Environment