Skip to content

"main" and "browser" root fields in package.json #9

@hugomrdias

Description

@hugomrdias

while debugging and build issue with @ipld/car i noticed that the browser field can't be resolved by esbuild

 "browser": {
    ".": "./cjs/car-browser.js",
    "./reader": "./cjs/lib/reader-browser.js",
    "./indexed-reader": "./cjs/lib/indexed-reader-browser.js",
    "./indexer": "./cjs/lib/indexer.js",
    "./iterator": "./cjs/lib/iterator.js",
    "./writer": "./cjs/lib/writer.js"
  },

i think this is trying to mimic how export maps work with abstract entry points to the package but the old main and browser fields dont care about that, they just care about "real" files and mapping "real" files to other "real" files.
In the snippet above we can see that "." or "./reader" etc don't actually exist inside @ipld/car and so esbuild can't bundle and errors saying cant resolve @ipld/car.

A fix would be to output this instead

 "main": "./cjs/car.js",
 "browser": {
    "./cjs/car.js": "./cjs/car-browser.js",
    "./cjs/lib/reader.js": "./cjs/lib/reader-browser.js",
    "./cjs/lib/indexed-reader": "./cjs/lib/indexed-reader-browser.js",
    "./cjs/lib/indexer": "./cjs/lib/indexer.js", // optional
    "./cjs/lib/iterator": "./cjs/lib/iterator.js", // optional
    "./cjs/lib/writer": "./cjs/lib/writer.js" // optional
  },

Theres other ways to fix this but this seems to be most canonical one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions