Unnecessary `process` polyfill when checking `process.env` and incomplete treeshaking
#8,156 opened on May 28, 2022
Description
🐛 Bug Report
Checking typeof process or process.env causes Parcel to unnecessarily polyfill process module, even if the code only uses process.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:
const debug = (
typeof process === 'object' &&
process.env &&
process.env.NODE_DEBUG &&
/\bsemver\b/i.test(process.env.NODE_DEBUG)
) ? (...args) => console.error('SEMVER', ...args)
: () => {}
It only uses process.env.NODE_DEBUG to determine whether debug should be enabled, so process should not be polyfilled and the whole function should be eliminated from production builds when debug is not enabled.
processis imported automatically from theprocessmodule, unless it's part of aprocess.browserorprocess.env.FOOexpression which is replaced by a boolean or the value of the environment variable.
🎛 Configuration (.babelrc, package.json, cli command)
See repo in code sample section below.
🤔 Expected Behavior
- Parcel should not add unnecessary
processpolyfill when the code checkstypeof processorprocess.env. - Parcel should determine whether
/\bsemver\b/i.test(process.env.NODE_DEBUG)is false and eliminate the whole function and its calls.
😯 Current Behavior
-
Parcel thinks
processmodule is used and addsprocesspackage polyfill:@parcel/resolver-default: Auto installing polyfill for Node builtin module "process"... D:\Users\filips\Downloads\parcel-polyfill-issue\src\index.js:4:10 3 | const debug = ( > 4 | typeof process === 'object' && > | ^^^^^^^ used here 5 | process.env && 6 | process.env.NODE_DEBUG && 📝 Learn more: https://parceljs.org/features/node-emulation/#polyfilling-%26-excluding-builtin-node-modules@parcel/resolver-default: Auto installing polyfill for Node builtin module "process"... D:\Users\filips\Downloads\parcel-polyfill-issue\src\index.js:4:3 3 | const debug = ( > 4 | process.env && > | ^^^^^^^ used here 5 | process.env.NODE_DEBUG && 6 | /\bsemver\b/i.test(process.env.NODE_DEBUG) 📝 Learn more: https://parceljs.org/features/node-emulation/#polyfilling-%26-excluding-builtin-node-modulesThis happens both in 2.1.1 and 2.6.0, although it doesn't add
processas a dependency in 2.1.1. -
Parcel 2.6.0 does not eliminate
debugfunction completely when building for production, even if problematic checks are removed.Source:
const debug = ( process.env.NODE_DEBUG && /\bsemver\b/i.test(process.env.NODE_DEBUG) ) ? (...args) => console.error('SEMVER', ...args) : () => {} debug('Hello World')Parcel 2.1.1 (works fine, empty file, except sourcemap comment):
//# sourceMappingURL=index.81c000e3.js.mapParcel 2.6.0 (does not remove
debugfunction):(()=>{})("Hello World"); //# sourceMappingURL=index.16b57466.js.map
🔦 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
- Branch without checks: https://github.com/filips123/parcel-polyfill-issue/tree/process-without-check
- Branch with checks: https://github.com/filips123/parcel-polyfill-issue/tree/process-with-check
🌍 Your Environment
| Software | Version(s) |
|---|---|
| Parcel | 2.1.1, 2.6.0 |
| Node | 16.15.0 |
| npm/Yarn | 1.22.18 |
| Operating System | Windows 10 |