@acdh-oeaw/prettier-config
v2.0.1
Published
Shared configuration preset for [`prettier`](https://prettier.io/).
Downloads
416
Readme
acdh-oeaw prettier config
Shared configuration preset for prettier
.
How to install
npm install -D prettier @acdh-oeaw/prettier-config
Add the config to package.json
:
{
"prettier": "@acdh-oeaw/prettier-config"
}
If you absolutely need to extend the shared config, create a .prettierrc.js
file in the root
folder of your project:
const sharedConfig = require("@acdh-oeaw/prettier-config");
const config = {
...sharedConfig,
overrides: [
{
files: ["*.svg"],
options: {
parser: "html",
},
},
],
};
module.exports = config;
How to use
Add a script to package.json
:
{
"scripts": {
"format:check": "prettier . --cache --check --ignore-path .gitignore",
"format:fix": "npm run format:check -- --write"
}
}
You can then run npm run format:check
to check code formatting, and npm run format:fix
to
auto-fix formatting errors.
How to auto-format with Git hooks
npm install -D lint-staged simple-git-hooks
To auto-format on every Git commit, and to check formatting on every Git push, add the following to
package.json
:
{
"scripts": {
"format:check": "prettier . --cache --check --ignore-path .gitignore",
"format:fix": "npm run format:check -- --write",
"prepare": "npm run setup",
"setup": "simple-git-hooks || exit 0",
"validate": "npm run format:check"
},
"lint-staged": {
"*": "prettier --cache --ignore-unknown --write"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"pre-push": "npm run validate"
}
}
Editor integrations
VS Code
Install the VS Code prettier-vscode
plugin by pressing Ctrl+P and pasting
the following command:
ext install esbenp.prettier-vscode
Also, make sure to add prettier-vscode
to the list of recommended plugins for your project in
.vscode/extensions.json
:
{
"recommendations": ["esbenp.prettier-vscode"]
}
You may also want to enable "Format on Save" in .vscode/settings.json
:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
WebStorm
The WebStorm default installation should already contain the JetBrains Prettier plugin.
If you install this acdh config via the embedded terminal (Alt+F12) as suggested here the plugin should configure itself automagically. Alternately you may adjust its settings via the Settings/Preferences dialog (Ctrl+Alt+S), under Languages & Frameworks | JavaScript | Prettier . It's highly recommended to check both On code reformat and On Save there as well. (See this section for alternate/additional automation possibilities).
By default the set glob pattern will only match JavaScript, TypeScript, JSX and TSX files. Depending on your stack it's recommended to extend it to
{**/*,*}.{js,json,ts,jsx,tsx,vue}
To ship this config with your project, commit/include the prettier.xml
in your .idea
config
folder:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PrettierConfiguration">
<option name="myRunOnSave" value="true" />
<option name="myRunOnReformat" value="true" />
<option name="myFilesPattern" value="{**/*,*}.{js,json,ts,jsx,tsx,vue,css,scss,sass}" />
</component>
</project>
For more detailed instructions see the WebStorm docs on Prettier and Vue.js formatting
How to adjust tab width
We use tabs, not spaces, for indentation because of their accessibility benefits. Users are then free to configure their preferred tab width in their text editors.
Adding an .editorconfig
file to the root of a project provides a way
to adjust the tab width, which is understood by most text editors automatically (VS Code requires a
separate plugin).
In VS Code, you can adjust the tab width by changing the following in a user's settings.json
:
{
"editor.insertSpaces": false,
"editor.tabSize": 2
}
On https://github.com you can adjust the tab width in the appearance settings.
In your terminal, you should be able to use the tabs
command to set tab width, e.g. tabs 4
.
For the output of git diff
, you need to adjust the tab width of your pager. When using the default
less
, you can set git config --global core.pager 'less -x4'
.