bluwy / publint

Lint packaging errors
https://publint.dev
MIT License
960 stars 21 forks source link

Check that `module` preceeds `import` and `require` in exports definitions #49

Closed nvie closed 1 year ago

nvie commented 1 year ago

This provides a fix for common bugs like this:

{
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js",
      "module": "./dist/index.mjs",  // ⚠️
    }
  }
}

Due to the way conditions are checked top-to-bottom, the module definition has no chance to take precedence over import and require—which is typically the whole point of including module. Bundler implementations may chose to not respect definition order (so some bundlers may work despite the incorrect order of the module field), but it's not the way the conditions are supposed to be checked. By enforcing module to appear before import and/or require, this problem is circumvented and should work in all bundlers.

The fix for the above is to move the "module" definition up:

{
  "exports": {
    ".": {
      "module": "./dist/index.mjs",  // ✅
      "import": "./dist/index.mjs",
      "require": "./dist/index.js",
    }
  }
}

Based on this description of how conditions work.

nvie commented 1 year ago

Thanks for the review, @bluwy! I just made the changes you suggested. Does it look okay to you this way?