parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.52k stars 2.27k forks source link

Parcel Canary: Cannot bundle Nodejs ESModule with dynamic imports #9925

Open aminya opened 3 months ago

aminya commented 3 months ago

🐛 bug report

If I add a esmodule target I get this error. After debugging, I realized this line in Parcel returns true. As far as I know, Nodejs supports Dynamic imports natively.

  let needsDynamicImportPolyfill = !bundle.env.isLibrary && !bundle.env.supports('dynamic-import', true);

@parcel/runtime-js: No import() polyfill available for context 'node'

  Error: No import() polyfill available for context 'node'
      at nullthrows (.../setup-cpp/node_modules/.pnpm/nullthrows@1.1.1/node_modules/nullthrows/nullthrows.js:7:15)
      at getLoaderRuntime
  (.../setup-cpp/node_modules/.pnpm/@parcel+runtime-js@2.0.0-canary.1720_@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12_/node_modules/@parcel/runtime-js/lib/JSRuntime.js:360:42)
      at Object.apply
  (.../setup-cpp/node_modules/.pnpm/@parcel+runtime-js@2.0.0-canary.1720_@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12_/node_modules/@parcel/runtime-js/lib/JSRuntime.js:171:29)
      at applyRuntimes
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/applyRuntimes.js:114:44)
      at async BundlerRunner.bundle
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/requests/BundleGraphRequest.js:334:25)
      at async Object.run
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/requests/BundleGraphRequest.js:132:17)
      at async RequestTracker.runRequest
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/RequestTracker.js:784:20)
      at async Object.run
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/requests/ParcelBuildRequest.js:52:7)
      at async RequestTracker.runRequest
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/RequestTracker.js:784:20)
      at async Parcel._build
  (.../setup-cpp/node_modules/.pnpm/@parcel+core@2.0.0-canary.1718_@swc+helpers@0.5.12/node_modules/@parcel/core/lib/Parcel.js:369:11)

🎛 Configuration (.babelrc, package.json, cli command)

The modern config fails.

https://github.com/aminya/setup-cpp/tree/modern

 "targets": {
    "main": {
      "context": "node",
      "engines": {
        "node": ">=12.x"
      },
      "includeNodeModules": true,
      "optimize": true,
      "outputFormat": "commonjs"
    },
    "actions": {
      "context": "node",
      "engines": {
        "node": ">=20.x"
      },
      "includeNodeModules": true,
      "optimize": true,
      "outputFormat": "commonjs"
    },
    "modern": {
      "context": "node",
      "engines": {
        "node": ">=22.x"
      },
      "includeNodeModules": true,
      "optimize": true,
      "outputFormat": "esmodule"
    }
  }

🤔 Expected Behavior

Not sure why isLibrary matters here in determining the dynamic import support. But it could be related.

😯 Current Behavior

💁 Possible Solution

🔦 Context

💻 Code Sample

https://github.com/aminya/setup-cpp/tree/modern https://github.com/aminya/setup-cpp/pull/265

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-canary.1718
Node 22
npm/Yarn pnpm 9.8
Operating System KDE Neon Ubuntu 22.04
aminya commented 2 months ago

The workaround for this is to specify isLibrary: true in the package.json for the target.