website/docs/guides/examples/prettier.mdx
import AddDepsTabs from '@site/src/components/AddDepsTabs'; import HeadingApiLink from '@site/src/components/Docs/HeadingApiLink';
<HeadingApiLink to="https://github.com/moonrepo/examples/blob/master/.moon/tasks/node.yml#L41" />In this guide, you'll learn how to integrate Prettier into moon.
Begin by installing prettier in your root. We suggest using the same version across the entire
repository.
Since code formatting is a universal workflow, add a format task to
.moon/tasks/**/* with the following parameters.
tasks:
format:
command:
- 'prettier'
# Use the same config for the entire repo
- '--config'
- '@in(4)'
# Use the same ignore patterns as well
- '--ignore-path'
- '@in(3)'
# Fail for unformatted code
- '--check'
# Run in current dir
- '.'
inputs:
# Source and test files
- 'src/**/*'
- 'tests/**/*'
# Config and other files
- '**/*.{md,mdx,yml,yaml,json}'
# Root configs, any format
- '/.prettierignore'
- '/.prettierrc.*'
The root-level Prettier config is required, as it defines conventions and standards to apply to the entire repository.
module.exports = {
arrowParens: 'always',
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
useTabs: true,
};
The .prettierignore file must also be defined at the root, as
only 1 ignore file can
exist in a repository. We ensure this ignore file is used by passing --ignore-path above.
node_modules/
*.min.js
*.map
*.snap
We suggest against project-level configurations, as the entire repository should be formatted using the same standards. However, if you're migrating code and need an escape hatch, overrides in the root will work.
--write?Unfortunately, this isn't currently possible, as the prettier binary itself requires either the
--check or --write options, and since we're configuring --check in the task above, that takes
precedence. This is also the preferred pattern as checks will run (and fail) in CI.
To work around this limitation, we suggest the following alternatives:
format-write.