You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add ability to use function as config (#913)
* feat: add ability to use function as config
* fix: test
* test: add specific test for config formatter
* docs: clarify function based configuration option
* Apply suggestions for README.md from code review
Co-authored-by: Iiro Jäppinen <[email protected]>
Co-authored-by: Iiro Jäppinen <[email protected]>
Copy file name to clipboardExpand all lines: README.md
+30-3Lines changed: 30 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -189,14 +189,40 @@ For example:
189
189
190
190
going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
191
191
192
-
## Using JS functions to customize tasks
192
+
## Using JS configuration file
193
193
194
-
When supplying configuration in JS format it is possible to define the task as a function, which will receive an array of staged filenames/paths and should return the completecommand as a string. It is also possible to return an array of completecommand strings, for example when the task supports only a single file input. The functioncan be either sync or async.
194
+
Writing the configuration file in JavaScript is the most powerful way to configure _lint-staged_ (`lint-staged.config.js`, [similar](https://github.com/okonet/lint-staged/README.md#configuration), or passed via `--config`). From the configuration file, you can export either a single function, or an object.
195
+
196
+
If the `exports` value is a function, it will receive an array of all staged filenames. You can then build your own matchers for the files, and return a command string, or an array or command strings. These strings are considered complete and should include the filename arguments, if wanted.
197
+
198
+
If the `exports` value is an object, its keys should be glob matches (like in the normal non-js config format). The values can either be like in the normal config, or individual functions like described above. Instead of receiving all matched files, the functions in the exported object will only receive the staged files matching the corresponding glob key.
### Example: Wrap filenames in single quotes and run once per file
201
227
202
228
```js
@@ -226,6 +252,7 @@ module.exports = {
226
252
```
227
253
228
254
### Example: Use your own globs
255
+
It's better to use the [function-based configuration (seen above)](https://github.com/okonet/lint-staged/README.md#example-export-a-function-to-build-your-own-matchers), if your use case is this.
0 commit comments