keithamus / sort-package-json

Sort an Object or package.json based on the well-known package.json keys
MIT License
788 stars 83 forks source link

bug: `packageJson.exports.types` should be always be ordered first #294

Open jlarmstrongiv opened 1 year ago

jlarmstrongiv commented 1 year ago

Failing Example:

{
  "name": "rcva",
  "version": "1.0.6",
  "private": false,
  "description": "React Class Variance Authority 🧬",
  "keywords": [
    "React Class Variance Authority",
    "react-class-variance-authority",
    "classes",
    "classname",
    "classnames",
    "css",
    "rcva",
    "stitches",
    "vanilla-extract",
    "variants",
    "windstitch"
  ],
  "homepage": "https://github.com/jlarmstrongiv/rcva/blob/main/packages/core/README.md",
  "bugs": "https://github.com/jlarmstrongiv/rcva/issues",
  "repository": "https://github.com/jlarmstrongiv/rcva.git",
  "license": "MIT",
  "author": "John L. Armstrong IV",
  "sideEffects": false,
  "type": "module",
  "exports": {
    ".": {
      "require": "./dist/index.cjs",
      "import": "./dist/index.js",
      "types": "./dist/index.d.ts"
    },
    "./package.json": "./package.json"
  },
  "main": "dist/index.cjs",
  "module": "dist/index.js",
  "source": "src/index.tsx",
  "types": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "scripts": {
    "build": "tsup-node"
  },
  "devDependencies": {
    "@types/react": "^18.0.31",
    "@types/react-dom": "^18.0.11",
    "rimraf": "^4.4.1",
    "tsup": "^6.7.0",
    "typescript": "^5.0.0"
  },
  "peerDependencies": {
    "@leafygreen-ui/polymorphic": "^1.3.0",
    "class-variance-authority": ">= 0.5.1 < 1",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "tailwind-merge": "^1.11.0"
  }
}

More specifically, Should be the first in the object as required by TypeScript. according to publint.dev:

{
  "exports": {
    ".": {
      "require": "./dist/index.cjs",
      "import": "./dist/index.js",
      "types": "./dist/index.d.ts"
    },
    "./package.json": "./package.json"
  }
}

EXPORTS_TYPES_SHOULD_BE_FIRST

Ensure types condition to be the first. The TypeScript docs recommends so, but it's also because the exports field is order-based.

For example, a scenario where both the types and import condition could be active, types should be first so that it matches and returns a .d.ts file, rather than a .js file from the import condition.

keithamus commented 1 year ago

PRs welcome!

jlarmstrongiv commented 1 year ago

@keithamus would the PR include these changes?

Let me know if I missed anything in the outline 😄 if not, it should be a straightforward fix

keithamus commented 1 year ago

That’s correct. We use semantic versioning so prefixing your commit fix: will automatically apply and release the patch version.

fisker commented 1 year ago

If we are doing this, should we also enforce the default to be the last one?