parcel-bundler/parcel

Unnecessary `process` polyfill when checking `process.env` and incomplete treeshaking

Open

#8,156 创建于 2022年5月28日

在 GitHub 查看
 (5 评论) (3 反应) (0 负责人)JavaScript (44,030 star) (2,274 fork)batch import
:bug: BugGood First IssueStale Ignore🐡 JS Codegen

描述

🐛 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.

process is imported automatically from the process module, unless it's part of a process.browser or process.env.FOO expression 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

  1. Parcel should not add unnecessary process polyfill when the code checks typeof process or process.env.
  2. Parcel should determine whether /\bsemver\b/i.test(process.env.NODE_DEBUG) is false and eliminate the whole function and its calls.

😯 Current Behavior

  1. Parcel thinks process module is used and adds process package 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-modules
    

    This happens both in 2.1.1 and 2.6.0, although it doesn't add process as a dependency in 2.1.1.

  2. Parcel 2.6.0 does not eliminate debug function 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.map
    

    Parcel 2.6.0 (does not remove debug function):

    (()=>{})("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

🌍 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

贡献者指南