Denji

Prettier

Integrate Denji with Prettier for opinionated code formatting

Prettier is an opinionated code formatter that enforces a consistent style by parsing and re-printing code with its own rules.

Installation

npm install -D prettier

Basic Setup

Add Prettier to your Denji hooks to automatically format icons after adding them:

denji.json
{
  "output": "./src/icons.tsx",
  "framework": "react",
  "typescript": true,
  "hooks": {
    "postAdd": [
      "npx prettier --write ./src/icons.tsx"
    ]
  }
}

Configuration Examples

Format with Log Level

Control output verbosity:

denji.json
{
  "hooks": {
    "postAdd": [
      "npx prettier --write --log-level warn ./src/icons.tsx"
    ]
  }
}

Multiple Hooks

Format after various operations:

denji.json
{
  "hooks": {
    "postAdd": [
      "npx prettier --write ./src/icons.tsx"
    ],
    "postRemove": [
      "npx prettier --write ./src/icons.tsx"
    ],
    "postClear": [
      "npx prettier --write ./src/icons.tsx"
    ]
  }
}

Check Before, Format After

Validate formatting before adding, then fix:

denji.json
{
  "hooks": {
    "preAdd": [
      "npx prettier --check ./src/icons.tsx"
    ],
    "postAdd": [
      "npx prettier --write ./src/icons.tsx"
    ]
  }
}

Common Commands

npx prettier --write ./src/icons.tsx

Formats the icons file according to Prettier rules.

npx prettier --check ./src/icons.tsx

Checks if files are formatted without modifying them. Returns exit code 1 if unformatted. Useful for CI pipelines.

npx prettier --write --config .prettierrc.json ./src/icons.tsx

Uses a specific Prettier configuration file.

npx prettier --write --parser typescript ./src/icons.tsx

Explicitly specifies the parser to use.

Prettier Options

Common Flags

FlagDescription
--writeEdit files in place
--checkCheck if files are formatted
--configPath to config file
--log-levelLog level (silent, error, warn, log, debug)
--parserParser to use (typescript, babel, etc.)

Examples with Options

denji.json
{
  "hooks": {
    "postAdd": [
      "npx prettier --write --log-level error ./src/icons.tsx"
    ]
  }
}

Combining with ESLint

Prettier handles formatting, while ESLint handles code quality:

denji.json
{
  "hooks": {
    "postAdd": [
      "npx eslint --fix ./src/icons.tsx",
      "npx prettier --write ./src/icons.tsx"
    ]
  }
}

When using Prettier with ESLint, install eslint-config-prettier to disable ESLint formatting rules that conflict with Prettier:

npm install -D eslint-config-prettier

Then add it to your ESLint config:

eslint.config.js
import prettier from "eslint-config-prettier";

export default [
  // ... other configs
  prettier,
];

Package Manager Alternatives

{
  "hooks": {
    "postAdd": ["npx prettier --write ./src/icons.tsx"]
  }
}
{
  "hooks": {
    "postAdd": ["pnpm exec prettier --write ./src/icons.tsx"]
  }
}
{
  "hooks": {
    "postAdd": ["yarn exec prettier --write ./src/icons.tsx"]
  }
}
{
  "hooks": {
    "postAdd": ["bunx prettier --write ./src/icons.tsx"]
  }
}

Using Package Scripts

Define a script in package.json:

package.json
{
  "scripts": {
    "format": "prettier --write .",
    "format:icons": "prettier --write ./src/icons.tsx",
    "format:check": "prettier --check ."
  }
}

Reference it in your Denji config:

denji.json
{
  "hooks": {
    "postAdd": ["npm run format:icons"]
  }
}

Performance Tips

  1. Target specific file - Format only the icons file:

    "postAdd": ["npx prettier --write ./src/icons.tsx"]
  2. Use log level - Reduce output noise:

    "postAdd": ["npx prettier --write --log-level error ./src/icons.tsx"]
  3. Cache in CI - Use caching in CI pipelines:

    npx prettier --check --cache ./src/icons.tsx

Troubleshooting

Prettier Not Found

Install Prettier as a dev dependency:

npm install -D prettier

Formatting Doesn't Match Editor

Ensure your editor is using the same Prettier version and configuration:

  • VS Code: Install the Prettier extension
  • Check that "editor.formatOnSave": true is enabled
  • Verify .prettierrc is in the project root

Conflicting with ESLint

Install eslint-config-prettier to disable conflicting rules:

npm install -D eslint-config-prettier

Different Formatting in CI

Ensure the same Prettier version is installed in CI and locally. Pin the version in package.json:

{
  "devDependencies": {
    "prettier": "3.2.5"
  }
}

On this page