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

Package detail

perfectionist

ben-eb59.3kMIT2.4.0

Beautify CSS files.

beautify, css, format, postcss, postcss-plugin, pretty

readme

perfectionist Build Status NPM version Dependency Status

Beautify CSS files.

Install

With npm do:

npm install perfectionist --save

Example

Input

h1   {
         color   :  red }

Expanded output

h1 {
    color: red;
}

Compact output

h1 { color: red; }

Compressed output

h1{color:red}

API

perfectionist.process(css, [options])

css

Type: string Required option.

Pass a CSS string to beautify it.

options

cascade

Type: boolean Default: true

Set this to false to disable visual cascading of vendor prefixed properties. Note that this transform only applies to the expanded format.

/* true */
h1 {
    -webkit-border-radius: 12px;
            border-radius: 12px;
}

/* false */
h1 {
    -webkit-border-radius: 12px;
    border-radius: 12px;
}
colorCase

Type: string Default: lower

Set either lower or upper to transform hexadecimal colors to the according case.

/* lower */
p { color: #C8C8C8 }

/* upper */
p { color: #c8c8c8 }
colorShorthand

Type: boolean Default: true

Set this to true to shorten hexadecimal colors.

/* true */
p { color: #fff }

/* false */
p { color: #ffffff }
format

Type: string Default: expanded

Pass either expanded, compact or compressed. Note that the compressed format only facilitates simple whitespace compression around selectors & declarations. For more powerful compression, see cssnano.

indentChar

Type: string Default: (space)

Specify \t here instead if you would like to use tabs for indentation.

indentSize

Type: number Default: 4

This number will be used as a basis for all indent levels, using the expanded format.

trimLeadingZero

Type: boolean Default: true

Set this to true to trim leading zero for fractional numbers less than 1.

/* true */
p { line-height: .8 }

/* false */
p { line-height: 0.8 }
trimTrailingZeros

Type: boolean Default: true

Set this to true to traim trailing zeros in numbers.

/* true */
div { top: 50px }

/* false */
div { top: 50.000px }
maxAtRuleLength

Type: boolean|number Default: 80

If set to a positive integer, set a maximum width for at-rule parameters; if they exceed this, they will be split up over multiple lines. If false, this behaviour will not be performed. Note that this transform only applies to the expanded format.

maxSelectorLength

Type: boolean|number Default: 80

If set to a positive integer, set a maximum width for a selector string; if it exceeds this, it will be split up over multiple lines. If false, this behaviour will not be performed. Note that this transform is excluded from the compressed format.

maxValueLength

Type: boolean|number Default: 80

If set to a positive integer, set a maximum width for a property value; if it exceeds this, it will be split up over multiple lines. If false, this behaviour will not be performed. Note that this transform only applies to the expanded format.

sourcemap

Type: boolean Default: false

Generate a sourcemap with the transformed CSS.

syntax

Type: string

Specify scss if you would like to also format SCSS-style single line comments. This loads the postcss-scss plugin.

zeroLengthNoUnit

Type: boolean Default: true

Set this to true to trim units after zero length.

/* true */
div { padding: 0 }

/* false */
div { padding: 0px }

postcss([ perfectionist(opts) ])

perfectionist can also be consumed as a PostCSS plugin. See the documentation for examples for your environment.

CLI

perfectionist also ships with a CLI app. To see the available options, just run:

$ perfectionist --help

Usage

See the PostCSS documentation for examples for your environment.

Integrations

Contributing

Pull requests are welcome. If you add functionality, then please add unit tests to cover it.

License

MIT © Ben Briggs

changelog

2.4.0

  • Adds support for tab indentation (thanks to @solidflows).

2.3.1

  • Better handling of trailing/leading zeroes & hex colour case transformations (thanks to @vansosnin).
  • Now parses values rather than applying regular expressions to values, using postcss-value-parser.
  • Resolves an issue where fractions were being incorrectly transformed.
  • Resolves an issue where parentheses inside string literals were being erroneously transformed.
  • Resolves an issue where base64 content was being incorrectly transformed to lower case.

2.3.0

  • Add options to handle zeroes - trim leading/trailing zeroes, and removing units from zero lengths (thanks to @vansosnin).

2.2.0

  • Add options to format hex colours (thanks to @vansosnin).

2.1.4

  • Replaced internal vendor prefixes list with the vendors module.
  • Uses Babel's object assign.
  • Ensures that the expanded format is used when the option is set to undefined.

2.1.3

  • Fixes an issue where extra space was being added to commas inside strings, mangling base64 URLs (thanks to @Mottie, @silverwind, @denji).

2.1.2

  • Fixes an integration issue with PostCSS 5.0.6.

2.1.1

  • Fixes a plugin integration issue where undefined was appearing in the output.

2.1.0

  • Adds scss syntax support.
  • Better formatting of functions; rgb(0,0,0) becomes rgba(0, 0, 0) (thanks to @yisibl).

2.0.0

  • Upgrade to PostCSS 5.x.

1.4.1

  • Fixed an issue where perfectionist would add an extra space in between at rules with no child nodes (for example, @import).

1.4.0

  • Added an option to disable the visual cascade of properties.
  • Fixed an issue where at-rules were not getting an appropriate amount of newlines following the rule.
  • Fixed an issue where comments in values were being removed.
  • Where possible, perfectionist will condense multi-line selectors into a single line.

1.3.1

  • perfectionist will now not remove comments within selector strings.

1.3.0

  • Better formatting of comments inside rules.
  • Better formatting of selectors inside at-rules.
  • Added an option to configure the indent size.

1.2.2

  • Fixes a crash when a comment ended the file.

1.2.1

  • Fixes an issue where comments were being removed from inside nodes.

1.2.0

  • Adds support for configurable wrapping of property values over multiple lines.
  • Adds support for configurable wrapping of at-rule parameters.

1.1.0

  • Adds support for newlines around block comments in both expanded and compact formats.

1.0.2

  • Fixes a crash on comments inside rules.

1.0.1

  • Fixes a behaviour where the module was trying to add negative space to a property when re-aligning.

1.0.0

  • Initial release.