Skip to content

Instantly share code, notes, and snippets.

@joanvm
Last active December 13, 2018 16:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joanvm/1ff133c446aedc05ac38f3511a7d64f4 to your computer and use it in GitHub Desktop.
Save joanvm/1ff133c446aedc05ac38f3511a7d64f4 to your computer and use it in GitHub Desktop.

Adding Prettier into a project

Install libraries

npm install --save-dev --save-exact prettier
npm install pretty-quick husky --save-dev

prettier

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

Opinionated (əpɪnjəneɪtɪd)

If you describe someone as opinionated, you mean that they have very strong opinions and refuse to accept that they may be wrong. - COBUILD Advanced English Dictionary

Synonyms: dogmatic, prejudiced, biased, arrogant

pretty-quick

Runs Prettier on your changed files.

husky

Git hooks made easy

Add pre-commit hook

In package.json append:

  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged --verbose \"src/**/*.tsx?\""
    }

Define custom rules

Create file .prettierrc.js at the root

module.exports = {
  useTabs: false,  // Indent lines with tabs instead of spaces.
  singleQuote: true, // Use single quotes instead of double quotes.
  /**
   * Print trailing commas wherever possible.
   * Valid options:
   *   - "none" - no trailing commas
   *   - "es5" - trailing commas where valid in ES5 (objects, arrays...)
   *   - "all" - trailing commas wherever possible (function arguments)
   */
  trailingComma: 'es5',
  parser: 'typescript',
  <ruleName>: <Value>,
}

Define exclusion rules

Create .prettierignore file at the root

*.map
package.json
tsconfig.json
tslint.json
tsconfig.*.json
package-lock.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment