package-build-stats icon indicating copy to clipboard operation
package-build-stats copied to clipboard

getPackageExportSizes fails on packages with conditional export maps

Open arshaw opened this issue 1 year ago • 1 comments

While investigating why Exports Analysis fails for fullcalendar on bundlephobia, I wrote this script:

import { getPackageExportSizes } from 'package-build-stats'

const sizes = await getPackageExportSizes('@fullcalendar/[email protected]')
console.log(sizes)

I get the following error:

Error: Package path . is not exported from package /tmp/tmp-build/packages/build-@fullcalendarcore-fIi/node_modules/@fullcalendar/core (see exports field in /tmp/tmp-build/packages/build-@fullcalendarcore-fIi/node_modules/@fullcalendar/core/package.json)
    at /Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/ExportsFieldPlugin.js:104:7
    at Hook.eval [as callAsync] (eval at create (/Users/adam/Scratch/bundlephobia-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:22:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/adam/Scratch/bundlephobia-test/node_modules/tapable/lib/Hook.js:18:14)
    at Resolver.doResolve (/Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/Resolver.js:432:16)
    at /Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:74:17
    at /Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:118:13
    at /Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/forEachBail.js:16:12
    at onJson (/Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:108:6)
    at Array.<anonymous> (/Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js:68:7)
    at runCallbacks (/Users/adam/Scratch/bundlephobia-test/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:27:15)

It seems like a conditional exports map inpackage.json like this is not supported. Here's what fullcalendar has:

  "exports": {
    "./package.json": "./package.json",
    "./index.cjs": "./index.cjs",
    "./index.js": "./index.js",
    ".": {
      "types": "./index.d.ts",
      "require": "./index.cjs",
      "import": "./index.js"
    },

arshaw avatar Dec 07 '22 21:12 arshaw

This seems to be the issue: https://github.com/webpack/enhanced-resolve/issues/313 Adding a "default" export in the package.json should fix the issue.

Ivordir avatar Apr 04 '23 15:04 Ivordir