Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

stylelint-prettier

prettier3.6mMIT5.0.3

Runs prettier as an stylelint rule

stylelint, stylelint-plugin, prettier

readme

stylelint-prettier Build Status

Runs Prettier as a Stylelint rule and reports differences as individual Stylelint issues.

Sample

Given the input file style.css:

.insert {
  display: block
}

.alter:after {color: red; content: 'example'}

.delete {
  display: block;;
}

Running ./node_modules/.bin/stylelint style.css shall output:

style.css
 2:17  ✖  Insert ";"                                          prettier/prettier
 5:15  ✖  Replace "color:·red;·content:·'example'" with       prettier/prettier
          "⏎··color:·red;⏎··content:·"example";⏎"
 8:17  ✖  Delete ";"                                          prettier/prettier

Installation

npm install --save-dev stylelint-prettier prettier

stylelint-prettier does not install Prettier or Stylelint for you. You must install these yourself.

Then, in your .stylelintrc:

{
  "plugins": ["stylelint-prettier"],
  "rules": {
    "prettier/prettier": true
  }
}

Alternatively you can extend from the stylelint-prettier/recommended config, which does the same thing:

{
  "extends": ["stylelint-prettier/recommended"]
}

Disabling rules that may conflict with Prettier

As of Stylelint v15, Stylelint deprecated all stylistic rules that conflict with prettier, and removed these rules in Stylelint v16. If you are using Stylelint v15 or above and are not using any of these deprecated rules then you do not need to do anything extra; this section does not apply to you.

If you are using Stylelint's stylistic rules, then many of them shall conflict with Prettier. This plugin works best if you disable all other Stylelint rules relating to stylistic opinions. If another active Stylelint rule disagrees with prettier about how code should be formatted, it will be impossible to avoid lint errors. You should use stylelint-config-prettier to disable all stylistic Stylelint rules.

To integrate this plugin with stylelint-config-prettier:

  1. In addition to the above installation instructions, install stylelint-config-prettier:

    npm install --save-dev stylelint-config-prettier
  2. Then add stylelint-config-prettier to the list of extended configs in your .stylelintrc that you added in the prior section. stylelint-config-prettier should go last in the array so that it will override other configs:

    {
      "extends": [
        "stylelint-prettier/recommended",
        "stylelint-config-prettier"
      ]
    }

You can then set Prettier's own options inside a .prettierrc file.

Options

stylelint-prettier will honor your .prettierrc file by default. You only need this section if you wish to override those settings.

Note: While it is possible to pass options to Prettier via your Stylelint configuration file, it is not recommended because editor extensions such as prettier-atom and prettier-vscode will read .prettierrc, but won't read settings from Stylelint, which can lead to an inconsistent experience.

Objects are passed directly to Prettier as options. Example:

{
  "rules": {
    "prettier/prettier": [true, {"singleQuote": true, "tabWidth": 4}]
  }
}

NB: This option will merge and override any config set with .prettierrc files (for Prettier < 1.7.0, config files are ignored)


Contributing

See CONTRIBUTING.md

Inspiration

The layout for this codebase and base configuration of prettier was taken from https://github.com/prettier/eslint-plugin-prettier

changelog

Changelog

5.0.3 (2025-01-26)

Avoid deprecation warning when encountering parsing errors in Stylelint >=16.13.0 (#385)

5.0.2 (2024-07-16)

Fix case where less files were incorrectly mangled when using v5.0.1 (#363)

5.0.1 (2024-07-13)

Fix case where when autofixing multiple other rules in addition to prettier, the other autofixes would be thrown away (#360)

5.0.0 (2023-12-10)

Increase the minimum required versions of stylelintand node. Stylelint v16 has converted its codebase to esm and has deprecated its commonjs apis. We've followed their suggested migration guide and stylelint-prettier is now an esm-only package. This has required dropping support for v15 at the same time.

  • Minimum stylelint version is now v16 (drop support for v15.x)
  • Minimum node requirements is now >=18.12.0 (drop support for v14.x and v16.x)

4.1.0 (2023-11-24)

  • Added end indexes for violations - the squiggly line in vscode will now cover more than one character. (#341)
  • Do not trigger prettier for .astro files. (#340)

4.0.2 (2023-07-27)

Fix case where an error is thrown when no prettier configuration file is found (#311)

4.0.1 (2023-07-26)

Fix prettier-plugin-svelte support by ensuring .svelte files are ignored. The regression of them not being ignored only affected v4.0.0. (#309)

4.0.0 (2023-07-07)

Add support for Prettier v3.

In order to make Prettier v3 work, we've had to drop support for Prettier v2.x and Stylelint less that v15.8.0. Maintaining temporary support for Prettier v2 and v3 at the same time isn't worth the complication that results thanks to prettier's migration to es modules. When you update to prettier v3, ensure that you update stylelint-prettier at the same time.

  • Minimum supported prettier version is now v3.0.0.
  • Minimum supported stylelint version is now v15.8.0.

3.0.0 (2023-02-22)

Remove stylelint-config-prettier from the recommended config. Stylelint v15 deprecated the rules that stylelint-config-prettier disabled, thus if you do not use those deprecated rules then you do not need stylelint-config-prettier. If you are still using these deprecated rules then you should install and configure stylelint-config-prettier separately.

Increase the mimimum required node version. No code changes have been required and v2.0.0 works with stylelint v15, however continuing to test with the unsupported node 12 is a burden that is not worth carrying.

  • Minimum node requirement is now "^14.17.0 || >=16.0.0" (drop support for v12.x)
  • If you extended from the stylelint-prettier/recommended in v2 then you must add an explict extend from stylelint-config-prettier to retain the same behaviour.

2.0.0 (2021-11-05)

Increase the minimum required versions of stylelint, prettier and node. No code changes have been required and v1.2.0 works with stylelint v14, however continuing to test old these old unsupported versions is getting more difficult thanks to internal API changes. (#198)

  • Minimum stylelint version is now v14 (drop support for v9.5 through v13.x)
  • Minimum prettier version is now v2 (drop support for v1.x)
  • Minimum node requirements is now ^12.22.0 || ^14.17.0 || >=16.0.0 (drop support for v8.x and v10.x)

1.2.0 (2021-02-27)

  • Do not trigger prettier for .svelte and .component.html (angular) files. This avoids errors. Prettifying a subset of the file (i.e. just contents of <style> tags) using stylelint feels like unneeded work if you're about to run prettier over the whole file anyway (#160)

1.1.2 (2019-12-14)

  • Account for user defined parser overrides when working out what files to skip (#39)
  • Add an extra check to quickly skip formatting CSS-in-JS object literals (#39)
  • Bump dev dependencies (#39)

1.1.1 (2019-05-30)

  • Fix incorrect trailing whitespace on Windows (#24)

1.1.0 (2019-05-12)

  • Do not trigger prettier for js, ts, vue, html and markdown files. In stylelint >=9.9.0 these files sometimes worked and sometimes threw errors. In stylelint >=9.10.0 they would either error or have incorrect indentation. Prettifying a subset of the file (i.e. just contents of <style> tags) using stylelint feels like unneeded work if you're about to run prettier over the whole file anyway (#22)

1.0.7 (2019-05-07)

  • Add stylelint >9.2.1 as a peerDependency to help Yarn PNP support

1.0.6 (2019-01-05)

  • Report unparsable code as linting issues instead of crashing (#14)

1.0.5 (2018-11-16)

  • Specifying an explict syntax with --syntax will no longer crash when autofixing (#11)

1.0.4 (2018-11-11)

  • Do not pass stylelint built-in options severity and messsage to prettier (#10)

1.0.3 (2018-10-01)

  • Use the CSS parser when no filename is specified
  • Use prettier-linter-helpers instead of depending on eslint-plugin-prettier (#7)

1.0.2 (2018-09-28)

  • Update documentation and package.json to reflect the new repo location as part of the prettier GitHub organization (#5)

1.0.1 (2018-09-05)

  • Allow using overrides in .prettierrc to lint non-standard file extensions (#3)

1.0.0 (2018-09-05)

The code for v1.0.0 is identical to v0.2.2. It has been used in production for a little while and I consider it stable.

  • Minor readme tweaks

0.2.2 (2018-07-29)

  • Better formatting of \r in reporting
  • Bump minimum required eslint-plugin-prettier version to 2.6.2
  • Ignore files that are specified in a .prettierignore file

0.2.1 (2018-07-03)

  • Ensure non-css languages (e.g. scss) would be parsed correctly when autofixing
  • Bump minimum required stylelint version to 9.2.1
  • Ensure trailing whitespace is always output when autofixing, even if the input file lacked trailing whitespace

0.2.0 (2018-07-02)

  • Add support for autofixing issues using stylelint --fix

0.1.0 (2018-07-02)

  • Initial functionality
  • Exposes a stylelint plugin that runs prettier
  • It reads config from .prettierrc by default but you can also pass config options into the rule