Is it possible to exclude/ignore a file when using Husky/lint-staged hooks?
Looking through the docs atm but not having any luck finding anything on this.
Was hoping there was something like an
/*ignore*/
tag that I could add.
To make lint-staged ignore certain files that were causing formatting issues.
Any thought on this greatly appreciated :)
Ended up adding
.prettierignore
file.
Not ideal but seems to do the job ok.
While configuring lint-staged in package.json or If you're using any other IDE, in order to ignore/exclude files by lint-Staged and husky hooks, you can add an "ignore" key in the lint-staged object to make it ignore whatever packages or files you want to ignore. Use the following extensible syntax:
{
"lint-staged": {
"linters": {
"src/**/*.js": ["formatter --write", "git add"],
},
"ignore": ["node_modules", "dist", "package-lock.json"] }
}
Just add the target pattern to 'linters' object and all the ignored files which you might be adding previously to .prettierignore to "ignore" key of lint-Staged object. And there you go!
I finally found out how to do this (at least as of lint-staged v11.1.2
)
In package.json
:
"lint-staged": {
"!(path/to/excluded/dir/**/*)*.ts": [
"eslint --fix",
"prettier --write"
]
}
Note the globstar pattern is within the negation pattern and not outside it. This ensures that subdirectories are also excluded.
If anyone still looking, take a look at this https://github.com/okonet/lint-staged#filtering-files It has good examples.
Linter commands work on a subset of all staged files, defined by a glob pattern. `lint-staged´ uses micromatch for matching files with the following rules:
/
), micromatch's matchBase
option will enabled, so globs match a file's basename regardless of directory:
"*.js"
will match all JS files, like /test.js
and /foo/bar/test.js
"!(*test).js"
. will match all JS files, except those ending in test.js
, so foo.js
but not foo.test.js
/
), it will match for paths as well:
"./*.js"
will match all JS files in the git repo root, so /test.js
but not /foo/bar/test.js
"foo/**/\*.js"
will match all JS files inside the/foo
directory, so/foo/bar/test.js
but not/test.js
So I've been trying to find an answer for this for an entire day and looking at all the forums suggested that they use minimatch for glob check which might have been correct for older versions but they use micromatch for new version and to solve this issue we can use their pattern to exclude certain directories So in your .lintstagedrc you can add the following pattern to avoid certain folders
{
"*.{json,md,html,scss}": ["prettier --write", "git add"],
["**/!(folder1|folder2)/*.ts"]: ["tslint --project tsconfig.json -c tslint.commit.json --fix", "prettier --write", "git add"]
}
So the glob here is an actual array and make sure not to pass this array within a string else it won't recognize the patterns also do not include **/*.ts the reason being lint-staged automatically converts this into a matchBase comparision if it finds / in the pattern so including this will also match against your folder1|folder2 files.
can fix in three ways:
more info : https://github.com/okonet/lint-staged/issues/797
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With