Search code examples
javascripteslintlint-staged

Running `lint-staged` takes a lot of time even for a single changed file


Running npm run pre-commit takes a lot of time even if only one file has been changed.

package.json script: "pre-commit": "lint-staged",

and lint-staged commands:

  "lint-staged": {
    "src/**/*.{ts,js,json}": [
      "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
    ],
    "src/**/*.{js,ts,json}": [
      "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
    ]
  },

It feels like eslint runs in the entire project How can I make it run only in the changed files?


Solution

  • You're asking eslint (and prettier) to run over the whole codebase by providing a glob. If you skip out the glob then each will run only on the file matched by lint-staged.

      "lint-staged": {
        "src/**/*.{ts,js,json}": [
          "eslint --fix"
        ],
        "src/**/*.{js,ts,json}": [
          "prettier --write"
        ]
      },
    

    You can make this even better by combining the operations. Since they all run on the exact same set of files putting them in an array runs the operations sequentially and prevents conflicts between the two operations.

      "lint-staged": {
        "src/**/*.{ts,js,json}": [
          "eslint --fix", "prettier --write"
        ],
        "test/**/*.{js,ts,json}": [
          "prettier --write"
        ]
      },