Détail du package

stylefmt

morishitter30.5kMIT6.0.3

stylefmt is a tool that automatically formats CSS according to stylelint rules

css, format, formatter, code style

readme

Modern CSS Formatter


Build Status

NPM Version

Downloads

Dependency Status

License

Gitter


stylefmt is a tool that automatically formats CSS according to stylelint rules.

stylefmt'd code is:

  • easier to write : never worry about minor formatting concerns while hacking away.
  • easier to read : when all code looks the same you need not mentally convert others' formatting style into something you can understand.
  • easier to maintain : mechanical changes to the source don't cause unrelated changes to the file's formatting; diffs show only the real changes.
  • uncontroversial : never have a debate about spacing or brace position ever again!

NOTICE: Consider other tools before adopting stylefmt

If you are using stylefmt with stylelint configuration to format according to its rules, you can now use stylelint's --fix option (from v7.11.0) to autofix.

Another on the other hand, prettier supports to format not only JavaScript but also CSS, SCSS and Less code.

Features

  • Supports the latest CSS syntax: Including custom properties, range context for media features, calc() and nesting.
  • Understands CSS-like syntaxes: stylefmt is powered by PostCSS, so it understands any syntax that PostCSS can parse, including SCSS.
  • Works well with stylelint: stylelint is a mighty, modern CSS linter. stylefmt can understand the formatting rules specified in your stylelint configuration file (.stylelintrc).

Examples

Future CSS syntax (cssnext)

Input (input.css):

/* custom properties */
:root{--fontSize: 1rem;
  --mainColor       :#12345678;
--highlightColor:hwb(190, 35%, 20%);
}

/* custom media queries */
@custom-media

--viewport-medium(width<=50rem);

/* some var() & calc() */
body{color:var(--mainColor);
    font-size:var(--fontSize);
 line-height: calc(var(--fontSize) * 1.5);
padding: calc((var(--fontSize) / 2) + 1px)}

/* custom media query usage */
@media (--viewport-medium) {
body {font-size: calc(var(--fontSize) * 1.2); }
}

/* custom selectors */
@custom-selector :--heading h1,h2,h3,    h4,h5,h6;
:--heading { margin-top:0 }

/* colors stuff */
a{
color:var(--highlightColor);
    transition:color 1s;
}
a:hover{color  :gray(255,50%) }
a:active{color : rebeccapurple }
a:any-link { color:color(var(--highlightColor) blackness(+20%)) }

/* font stuff */
h2 {font-variant-caps:small-caps;
}table{font-variant-numeric: lining-nums;
}

/* filters */
.blur{filter:blur(4px)}.sepia{
filter: sepia(.8);}

Yield:

/* custom properties */
:root {
  --fontSize: 1rem;
  --mainColor: #12345678;
  --highlightColor: hwb(190, 35%, 20%);
}

/* custom media queries */
@custom-media --viewport-medium (width <= 50rem);

/* some var() & calc() */
body {
  color: var(--mainColor);
  font-size: var(--fontSize);
  line-height: calc(var(--fontSize) * 1.5);
  padding: calc((var(--fontSize) / 2) + 1px);
}

/* custom media query usage */
@media (--viewport-medium) {
  body {
    font-size: calc(var(--fontSize) * 1.2);
  }
}

/* custom selectors */
@custom-selector :--heading h1, h2, h3, h4, h5, h6;

:--heading {
  margin-top: 0;
}

/* colors stuff */
a {
  color: var(--highlightColor);
  transition: color 1s;
}

a:hover {
  color: gray(255, 50%);
}

a:active {
  color: rebeccapurple;
}

a:any-link {
  color: color(var(--highlightColor) blackness(+20%));
}

/* font stuff */
h2 {
  font-variant-caps: small-caps;
}

table {
  font-variant-numeric: lining-nums;
}

/* filters */
.blur {
  filter: blur(4px);
}

.sepia {
  filter: sepia(.8);
}

SCSS syntax

Input (input.scss):

// mixin for clearfix


        @mixin      clearfix    ()      { &:before,
  &:after {
                content:" ";
    display              : table;  }

  &:after        {clear: both;}
   }.class
{
       padding:10px;@include        clearfix();}
     .base {  color: red;  }

// placeholder
%base
{


padding: 12px
}

.foo{
@extend      .base;}

.bar
      {     @extend            %base;

}

Yield:

// mixin for clearfix
@mixin clearfix () {
  &:before,
  &:after {
    content: " ";
    display: table;
  }

  &:after {
    clear: both;
  }
}

.class {
  padding: 10px;
  @include clearfix();
}

.base {
  color: red;
}

// placeholder
%base {
  padding: 12px;
}

.foo {
  @extend .base;
}

.bar {
  @extend %base;
}

Installation

$ npm install stylefmt

Usage

in Command Line

CLI Help:

$ stylefmt --help
Usage: stylefmt [options] input-name [output-name]

Options:

  -b, --config-basedir   Path to the directory that relative paths defining "extends"
  -c, --config           Path to a specific configuration file (JSON, YAML, or CommonJS)
  -d, --diff             Output diff against original file
  -r, --recursive        Format list of space seperated files(globs) in place
  -v, --version          Output the version number
  -h, --help             Output usage information
  -i, --ignore-path      Path to a file containing patterns that describe files to ignore.
  --stdin-filename       A filename to assign stdin input.

stylefmt can also read a file from stdin if there are no input-file as argument in CLI.

$ cat input.css | stylefmt --stdin-filename input.css

in Node.js

var fs = require('fs');
var postcss = require('postcss');
var scss = require('postcss-scss'); // when you use scss syntax
var stylefmt = require('stylefmt');

var css = fs.readFileSync('input.css', 'utf-8');

postcss([
  stylefmt
]).process(css, {
    from: 'input.css',
    syntax: scss
  })
  .then(function (result) {
    result.css; // formatted code
  });

in Task Runners

We can use stylefmt in Grunt, gulp, and Fly.

stylelint rules that stylefmt can handle

stylefmt :heart: stylelint

stylefmt supports the following stylelint rules:

and we can also format from the other stylelint's configration files or packages (e.g. stylelint-config-standard, stylelint-config-suitcss and so on) using extends property.

Default formatting rules (without stylelint config file)

Basic

  • 2 spaces indentation
  • require 1 space between a simple selector and combinator
  • require 1 space between selectors and {
  • require new line after {
  • disallow any spaces between property and :
  • require 1 space between : and values
  • require new line after declarations
  • require ; in last declaration
  • require 1 space between values and !important
  • disallow any spaces between ! and important
  • leave 1 blank line between rules
  • leave 1 blank line between rules in atrules
  • disallow any blank lines between @import

for nested selector syntax

  • leave 1 line between declarations and nested rules

SCSS

  • require 1 space between @mixin and mixin name
  • require 1 space between mixin name and (
  • require 1 space between @extend and base rules
  • require 1 space between @include and mixin name
  • disallow any spaces between $variable and :
  • require 1 space between : and name of variable

Option projects

Editor plugins

for Task Runners

License

The MIT License (MIT)

Copyright (c) 2015 Masaaki Morishita

changelog

v6.0.3

  • Replace turbocolor with colorette #338

v6.0.2

  • Replace minimist with getopts #338

v6.0.1

  • Replace chalk with turbocolor #336

v6.0.0

  • Use PostCSS v6
  • Use yarn
  • Upgrade dependencies
  • Add support for .less and .pcss files in recursive cli mode. #301

v5.3.2

  • Update formatZeros regular expressions #286

v5.3.1

  • Fixed to remove spaces for custom at-rules #283
  • Support the new param of at-rule-empty-line-before #284

v5.3.0

  • Support declaration-empty-line-before rule. #270
  • Support stylelint-order v0.4.x #267
  • Fixed #272

v5.2.0

  • Support stylelint-order plugin instead of declaration-block-properties-order. #263

v5.1.2

  • Prevent space between EOL and semicolon for @value for CSS Modules

v5.1.1

  • Bump up postcss-sorting version.

v5.1.0

  • Support .wxss extention. #255

v5.0.4

v5.0.3

v5.0.2

  • Beautify the CLI output. #241

v5.0.1

  • Fixed: path.join() before globby gets pathes.

v5.0.0

  • Added: globs support for --recursive option in CLI. #223
  • Fixed: --recursive option in CLI, to specify files by glob e.g. stylefmt --list /readdir/**/*.css instead.
  • Fixed: options --diff and --recursive in CLI can be used together.
  • Added: --stdin-filename option to CLI. same as stylelint CLI
  • Added: --ignore-path option to CLI. same as stylelint CLI
  • Added: ignorePath option to Node.js API. same as stylelint API
  • Added: ignoreFiles option to configuration file. same as stylelint configuration

v4.4.0

v4.3.1

v4.3.0

  • Drop to support Node.js v0.12
  • Support the following stylelint rules:
    • number-leading-zero
    • shorthand-property-no-redundant-values
    • number-no-trailing-zeros
    • number-leading-zero
    • length-zero-no-unit
    • color-hex-length
    • at-rule-empty-line-before
  • Fixed some bugs

Thank you for all contributors :)

v4.2.3

  • Fixed a bug.

v4.2.2

  • Support for severities with block-closing-brace-newline-after#173
  • Don't format in Sass maps
  • Don't space in sass map functions

v4.2.1

v4.2.0

  • Add support for string-quotes rule of stylelint. #164
  • Add support for groups in declaration-block-properties and fixed #165. #167

v4.1.1

v4.1.0

  • Add support for declaration-block-properties-order rule of stylelint. #161

v4.0.0

  • Remove the default formatting rules for comments. #158

v3.5.0

  • Add --config option to specific configuration file

v3.4.4

v3.4.3

v3.4.2

  • Support color-hex-case rule of stylelint
  • Fixed #146

v3.4.1

v3.4.0

Support the following stylelint rules

  • declaration-colon-space-after, #106
  • declaration-colon-space-before, #107

v3.3.1

Fixed some bugs.

v3.3.0

Add support for stylelint extends. #129

@seka implemented this feature. Thanks :) #133

v3.2.1

Fixed the bug that occurs with --recursive option.

v3.2.0

Supported the following stylelint rules

  • block-closing-brace-newline
  • block-closing-brace-newline-after

v3.1.0

Implement as an asynchronouse plugin. #85

  • Introduce cosmiconfig instead of rc-loader
  • Fixed #81

stylefmt v3.0.0

:tada: Renamed to stylefmt, and support to understand stylelint configuration file.

  • stylefmt works well with stylelint #79

@seka implemented this feature. Thanks bro :D

v2.1.5

Fixed #78

v2.1.4

Fixed some bugs.

v2.1.3

Fixed some bugs.

  • #70
  • #71
  • Do not format the value when the property is content

v2.1.2

Fixed some bugs.

v2.1.1

Fixed some bugs.

v2.1.0

  • Format properties and values to lowercase #59
  • Fixe a bug #56

v2.0.2

Fixed var() format

v2.0.1

Fixed @apply rule format, @custom-selector-params

v2.0.0

Support formatting future CSS Syntax using cssnext

v1.4.1

v1.4.0

  • Format hex color code to lowercase.

v1.3.9

  • Fixed a bug

v1.3.8

Fixed some bugs, thanks @kewah .

v1.3.7

v1.3.6

v1.3.5

v1.3.4

v1.3.3

v1.3.2

v1.3.1

v1.3.0

  • Set indentation size from .editorconfig

v1.2.3

  • Fixed some bugs

v1.2.2

  • Fix some bugs. Thanks @yisible :)

v1.2.1

  • Support formatting pseudo-element(::before, ::after)

v1.2.0

  • Support formatting @import
  • Fix sass function format
  • Introduce repeat-string package
  • Fix some bugs

v1.1.2

  • Hanele multiline comments. #24

Thanks @kewah .

v1.1.1

  • Fix atrule for comments format

v1.1.0

  • Change to open 1 brank line between rules. #16

v1.0.2

  • Support formatting @font-face

v1.0.1

v1.0.0

Major release.

  • Update PostCSS to v5.0.
  • Support all SCSS syntax.

    • inline comments
    • @function
  • Fix some bugs.

v0.8.5

  • Fix a bug #17

v0.8.4

  • Fix a bug #15

v0.8.3

  • Support formatting comments

v0.8.2

  • Fix a bug using --recursive option.

v0.8.1

  • Fix a bug using --recursive option.

v0.8.0

  • Add --recursive option in CLI.

v0.7.0

  • Read file from stdin.

v0.6.5

  • Fix a bug. #9

v0.6.4

  • Fix a bug. #6

v0.6.3

  • Implement --diff option.

v0.6.2

  • Open 1 space after , in values.

v0.6.1

  • Open 1 line between rules in atrules.

v0.6.0

Support formatting some Sass functions.

  • Variables
  • @mixin
  • @include
  • @extend (include % selector)

v0.5.3

  • Support formatting Sass Mixin.

v0.5.2

  • Support formatting !important.

v0.5.1

  • Remove spaces between values and semicolon.

v0.5.0

  • Support nested selector syntax like SCSS, Less, Stylus, and processor using postcss-nested.

v0.4.0

  • Can use as a PostCSS plugin.

v0.3.0

  • Changed stylefmt() a parameter. (filepath -> file)
  • Updated package description.

v0.2.0

  • Sort values of border shorthand property.

v0.1.3

  • Format media queries into detail.
  • Update example in README.

v0.1.2

  • Format declaration in media queries.

v0.1.1

  • Fix a CLI bug.

v0.1.0

  • Initial release.