brotli-wasm icon indicating copy to clipboard operation
brotli-wasm copied to clipboard

Incorrect "exports" in package.json

Open kettanaito opened this issue 1 year ago • 2 comments
trafficstars

  • Related to #30

First of all, thank you for a great package!

What

This library currently points to index.web.js for the import condition. That is incorrect. What ends up happening is that Node.js ESM (which resolves the import condition) tries to load the browser build of this package, resulting in a failure.

This package, basically, assumes that import is used only in the browser. Luckily, we can use ESM in Node.js now. I want to be able to use this package there too!

Proposal

Instead, provide environment-based export conditions to correctly distribute this package:

  "exports": {
    ".": {
      "types": "./index.d.ts",
-     "import": "./index.web.js",
+     "node": {
+       "import": "./index.node.mjs",
+       "require": "./index.node.js"
+     },
      "browser": "./index.browser.js",
      "require": "./index.node.js",
      "default": "./index.web.js"
    }
  },

Note that you can nest import/require/default/etc. export conditions inside an environment condition. We've been using that to a great effect in MSW for years now (ref).

Bundling

The node.import condition must points to an ESM build of this package for Node.js. If the said build target doesn't exist, let's add it.

Additional context

I am willing to open a pull request for this.

kettanaito avatar Oct 16 '24 10:10 kettanaito