GoogleChromeLabs / comlink

Comlink makes WebWorkers enjoyable.
Apache License 2.0
11.25k stars 386 forks source link

Specify `exports` in `package.json #508

Open lgarron opened 3 years ago

lgarron commented 3 years ago

Right now, it's not possible to import something like comlink/node-adapter and automatically get CJS or ESM depending on the environment.

node and some bundlers support:

{
  "exports": {
    ".": {
      "require": "dist/umd/comlink.js",
      "import": "dist/esm/comlink.mjs",
      "types": "dist/umd/comlink.d.ts",
    },
    "./node-adapter": {
      "require": "dist/umd/node-adapter.js",
      "import": "dist/esm/node-adapter.mjs",
      "types": "dist/umd/node-adapter.d.ts"
    }
  }
}

An alternative would be to add /node-adapter/package.json or /dist/node-adapter/package.json containing:

{
  "main": "dist/umd/node-adapter.js",
  "module": "dist/esm/node-adapter.mjs",
  "types": "dist/umd/node-adapter.d.ts"
}

Does the former seem reasonable? If not, does the latter?

One caveat is that exports can//should prevent importing any other files directly, so we'd need more fields if we want to export the minified versions so they can be used directly in node or bundlers that support exports. (If the minified files are mainly meant for using manually, this is not needed.)

lgarron commented 3 years ago

I'm trying to tackle https://github.com/lgarron/web-worker-compat-problems/blob/main/README.md#problem-9-node-workers-differ-from-browsers so I can avoid a source of pain at https://github.com/cubing/jsss/tree/scrambles/src/scrambles/worker/getNodeAdapter :-P

justinfagnani commented 3 weeks ago

I would love to see this for better compatibility with standard-module-only toolchains.