Files
test-repo/docs/configuration.md
T

130 lines
3.0 KiB
Markdown
Raw Normal View History

2017-11-07 14:39:07 +11:00
---
id: configuration
title: Configuration File
---
2017-11-22 19:27:40 +08:00
Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support. This means you can configure prettier via:
2017-11-07 14:39:07 +11:00
- A `.prettierrc` file, written in YAML or JSON, with optional extensions: `.yaml/.yml/.json`.
- A `.prettierrc.toml` file, written in TOML (the `.toml` extension is _required_).
- A `prettier.config.js` or `.prettierrc.js` file that exports an object.
2018-05-27 15:37:44 -03:00
- A `"prettier"` key in your `package.json` file.
2017-11-07 14:39:07 +11:00
2017-11-22 19:27:40 +08:00
The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn't) found.
2017-11-07 14:39:07 +11:00
2017-11-20 12:26:33 -08:00
The options to the configuration file are the same as the [API options](options.md).
2017-11-07 14:39:07 +11:00
## Basic Configuration
JSON:
```json
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
2017-11-07 14:39:07 +11:00
}
```
JS:
```js
// prettier.config.js or .prettierrc.js
module.exports = {
trailingComma: "es5",
tabWidth: 4,
semi: false,
singleQuote: true
};
```
2017-11-07 14:39:07 +11:00
YAML:
```yaml
# .prettierrc or .prettierrc.yaml
trailingComma: "es5"
tabWidth: 4
semi: false
singleQuote: true
2017-11-07 14:39:07 +11:00
```
TOML:
```toml
# .prettierrc.toml
trailingComma = "es5"
tabWidth = 4
semi = false
singleQuote = true
```
2017-11-07 14:39:07 +11:00
## Configuration Overrides
2017-11-22 19:27:40 +08:00
Prettier borrows eslint's [override format](http://eslint.org/docs/user-guide/configuring#example-configuration). This allows you to apply configuration to specific files.
2017-11-07 14:39:07 +11:00
JSON:
```json
{
"semi": false,
2017-11-22 19:27:40 +08:00
"overrides": [
{
"files": "*.test.js",
"options": {
"semi": true
}
2017-11-07 14:39:07 +11:00
}
2017-11-22 19:27:40 +08:00
]
2017-11-07 14:39:07 +11:00
}
```
YAML:
```yaml
semi: false
overrides:
2018-07-29 15:44:21 +08:00
- files: "*.test.js"
options:
semi: true
2017-11-07 14:39:07 +11:00
```
2017-11-22 19:27:40 +08:00
`files` is required for each override, and may be a string or array of strings. `excludeFiles` may be optionally provided to exclude files for a given rule, and may also be a string or array of strings.
2017-11-07 14:39:07 +11:00
## Setting the [parser](options.md#parser) option
By default, Prettier automatically infers which parser to use based on the input file extension. Combined with `overrides` you can teach Prettier how to parse files it does not recognize.
For example, to get Prettier to format its own `.prettierrc` file, you can do:
2017-11-07 14:39:07 +11:00
```json
{
2017-11-22 19:27:40 +08:00
"overrides": [
{
"files": ".prettierrc",
"options": { "parser": "json" }
}
]
2017-11-07 14:39:07 +11:00
}
```
2018-12-27 08:05:19 -05:00
You can also switch to the `flow` parser instead of the default `babel` for .js files:
```json
{
"overrides": [
{
"files": "*.js",
"options": {
"parser": "flow"
}
}
]
}
```
**Note:** _Never_ put the `parser` option at the top level of your configuration. _Only_ use it inside `overrides`. Otherwise you effectively disable Prettier's automatic file extension based parser inference. This forces Prettier to use the parser you specified for _all_ types of files – even when it doesn't make sense, such as trying to parse a CSS file as JavaScript.
2017-11-07 14:39:07 +11:00
## Configuration Schema
If you'd like a JSON schema to validate your configuration, one is available here: http://json.schemastore.org/prettierrc.