• Stars
    star
    2,098
  • Rank 21,911 (Top 0.5 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

stylefmt is a tool that automatically formats stylesheets.

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 ❤️ 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

More Repositories

1

postcss-style-guide

PostCSS plugin to generate a style guide automatically
JavaScript
525
star
2

picostyle

Ultra small CSS in JS library in 0.4 KB
JavaScript
360
star
3

atcss

Annotations based CSS processor
JavaScript
63
star
4

scssfmt

Standard SCSS code formatter
JavaScript
57
star
5

YACP

Yet Another CSS Preprocessor
JavaScript
52
star
6

gulp-stylefmt

gulp plugin for stylefmt
JavaScript
44
star
7

gulp-csscss

gulp plugin for running csscss
JavaScript
29
star
8

postcss-ref

PostCSS plugin to refer properties from another rule (@ref rule)
JavaScript
25
star
9

is-color

Check if a string is CSS color value
JavaScript
17
star
10

picostyle-react

Picostyle for React
JavaScript
11
star
11

grunt-stylefmt

Grunt plugin for stylefmt
JavaScript
11
star
12

csscs

CSS Code Style checker
JavaScript
9
star
13

postcss-namespace

PostCSS plugin for the scope of name binding
JavaScript
9
star
14

gridbugs

A community-curated list of CSS Grid issues and cross-browser workarounds for them.
7
star
15

postcss-font-smoothing

PostCSS plugin to fallback font-smoothing property
JavaScript
6
star
16

fly-cssfmt

Fly plugin for CSSfmt
JavaScript
6
star
17

fly-stylus

Fly plugin for Stylus
JavaScript
6
star
18

rails-grape-sample

Sample REST API implemented by Rails with Grape.
Ruby
5
star
19

psg-theme-default

Default theme of postcss-style-guide
HTML
5
star
20

postcss-annotation-include

PostCSS plugin for annotations based including properties in other rule sets
JavaScript
5
star
21

sketch.css

Sketch is a CSS framework, it makes Web designing more fun.
CSS
5
star
22

css-annotation

Annotation parser for CSS
JavaScript
5
star
23

psg

Command line interface of postcss-style-guide
JavaScript
4
star
24

psg-theme-forest

'Forest': Theme of postcss-style-guide
CSS
4
star
25

postcss-important

PostCSS plugin for annotations based `!important`
JavaScript
4
star
26

normalize.styl

Stylus port of normalize.css v3.0.3
CSS
3
star
27

postcss-validator

PostCSS plugin to check if an input string is valid CSS
JavaScript
3
star
28

generator-npmodule

Yeoman generator of node packaged module
JavaScript
3
star
29

css-annotation-block

A css-annotation module to return the specific nodes
JavaScript
3
star
30

cheatah

More fast and casual CSS styleguide or cheatsheat generator
JavaScript
3
star
31

postcss-annotation-extend

PostCSS plugin for annotations based inheritance from other rule sets
JavaScript
2
star
32

masaakim-hidden

About me
2
star
33

css-utilify

Add `@base` annotations to the rules
JavaScript
2
star
34

custom-properties-sass

Transpile Custom Properties to Sass variables
JavaScript
2
star
35

cli-tree

Object tree viewer for the CLI
JavaScript
2
star
36

css-border-property

Parser and stringifier of `border` property
JavaScript
2
star
37

fly-csso

Fly plugin for csso
JavaScript
2
star
38

css-tuning

Postprocessing pack for CSS performance tuning
JavaScript
2
star
39

css-specificity

CSS selector specificity calculator
JavaScript
1
star
40

postcss-atcss-inherit

A module for ACSS to inherit other rules
JavaScript
1
star
41

inCollege

inCollege is dating site for university students. They can connect each other.
JavaScript
1
star
42

grunt-yacp

grunt plugin for YACP.
JavaScript
1
star
43

tapestry

Test Anything Protocol tools and its wrapper testing framework in Ruby
Ruby
1
star
44

is-css-length

Check if a string is CSS value of the length
JavaScript
1
star
45

morishitter.com

JavaScript
1
star
46

tecsst

CSS testing framework without taking screenshots
JavaScript
1
star
47

rework-rule-binding

Rework plugin to prohibit cascading the ruleset.
JavaScript
1
star
48

morishitter2

PHP
1
star
49

css-type-definition

List of values that the CSS property can be taken
JavaScript
1
star
50

fly-csscomb

CSSComb plugin for Fly.
JavaScript
1
star
51

YouJukeMix

https://morishitter.github.io/YouJukeMix
JavaScript
1
star
52

css-value-type

get types of CSS properties
JavaScript
1
star
53

search-Twitter-id

The script for searching Twitter ID which has two or three characters.
PHP
1
star
54

heavyarms-syntax

A theme for Atom
CSS
1
star