bluwy / publint

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

Possibly invalid warning with dual esm/cjs types #55

Closed atomiks closed 1 year ago

atomiks commented 1 year ago

I may be wrong and this is necessary, but I've seen the second setup in a popular open source library - is it invalid or not?

"All good!"

  "types": "./src/types.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./src/types.d.mts",
        "default": "./dist/file.mjs"
      },
      "module": {
        "types": "./src/types.d.ts",
        "default": "./dist/file.esm.js"
      },
      "default": {
        "types": "./src/types.d.ts",
        "default": "./dist/file.umd.js"
      }
    }
  },

Warning

  1. pkg.exports["."].types types is an invalid format when resolving with the "import" condition. Consider splitting out two types conditions for import and require, and use the .mts extension, e.g. pkg.exports["."].import.types: "./src/types.d.mts"
  "types": "./src/types.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": "./src/types.d.ts",
      "import": {
        "types": "./src/types.d.mts",
        "default": "./dist/file.mjs"
      },
      "module": "./dist/file.esm.js",
      "default": "./dist/file.umd.js"
    }
  },
bluwy commented 1 year ago

Yeah it is invalid. When TypeScript tries to resolve the types for import 'my-lib', it will first see "types": "./src/types.d.ts" and use that as it matches all the criteria. It won't use the "types" in "import" (Exports conditions are order-sensitive)

A fix is to move "types": "./src/types.d.ts" after the "import" condition, so the "types" within the "import" condition gets the chance to match first.

Usually for types specific linting, https://arethetypeswrong.github.io is more robust too which you can check against.

atomiks commented 1 year ago

Ah ok, thanks for the info!