Giter Site home page Giter Site logo

joshua-chavanne / preprocessor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from suitcss/preprocessor

1.0 2.0 0.0 68 KB

A future-facing CSS preprocessor (used by SUIT CSS)

Home Page: http://suitcss.github.io

License: MIT License

JavaScript 100.00%

preprocessor's Introduction

suitcss-preprocessor

Build Status

SUIT CSS preprocessor.

Provides a CLI and Node.js interface for a preprocessor that combines various PostCSS plugins.

Compiles CSS packages with:

Each imported file is linted with postcss-bem-linter and minification is provided by cssnano. Code style can also be checked with stylelint

Additional plugins can be added via the configuration options.

Installation

npm install suitcss-preprocessor

Usage

suitcss input.css output.css

API

Command Line

Usage: suitcss [<input>] [<output>]

Options:

  -h, --help                output usage information
  -V, --version             output the version number
  -m, --minify              minify output with cssnano
  -l, --lint                ensure code adheres to the SUIT code style
  -i, --import-root [path]  the root directory for imported css files
  -c, --config [path]       a custom PostCSS config file
  -v, --verbose             log verbose output for debugging
  -w, --watch               watch the input file and any imports for changes

Examples:

  # pass an input and output file:
  $ suitcss input.css output.css

  # configure the import root directory:
  $ suitcss --import-root src/css input.css output.css

  # watch the input file and imports for changes:
  $ suitcss --watch input.css output.css

  # configure postcss plugins with a config file:
  $ suitcss --config config.js input.css output.css

  # unix-style piping to stdin and stdout:
  $ cat input.css | suitcss | grep background-color

Node.js

Returns a PostCSS promise

var preprocessor = require('suitcss-preprocessor');
var fs = require('fs');

var css = fs.readFileSync('src/components/index.css', 'utf8');

preprocessor(css, {
  root: 'path/to/css',
  minify: true,
}).then(function(result) {
  fs.writeFileSync('build/bundle.css', result.css);
});

Options

root
  • Type: String
  • Default: process.cwd()

Where to resolve imports from. Passed to postcss-import.

lint
  • Type: Boolean
  • Default: false

Ensure code conforms to the SUIT code style by using the stylelint-config-suitcss package.

Stylelint configuration options can also be overridden:

{
  lint: true,
  stylelint: {
  extends: 'stylelint-config-suitcss',
    rules: {
      indentation: [4, 'tab'],
    }
  }
}

The preprocessor will also pick up any rules added to a .stylelintrc in your project root. For example:

{
  "extends": "stylelint-config-suitcss"
}

Note: This works the same with the CLI -l flag.

minify
  • Type: Boolean
  • Default: false

If set to true then the output is minified by cssnano.

beforeLint
  • Type: Function
  • Default: undefined

Called with the imported CSS before it's passed to postcss-bem-linter. This allows you to transform the CSS first and it must return the css string.

Third paramater is the options object containing any PostCSS configuration you may need.

{
  beforeLint(css, filename, options) {
    // Do something to the imported css
    return css;
  }
}
use
  • Type: Array
  • Default: undefined

A list of plugins that are passed to PostCSS. This can be used to add new plugins and/or reorder the defaults

{
  use: ['postcss-at2x', 'postcss-property-lookup']
}
<plugin-name>
  • Type: Object
  • Default: undefined

Property matching the name of a PostCSS plugin that has options for that plugin

{
  autoprefixer: {
    browsers: ['> 1%', 'IE 7'],
    cascade: false
  },
  'postcss-calc': { preserve: true }
}

Plugin configuration

Creating a configuration file allows options to be passed to the individual PostCSS plugins. It can be passed to the suitcss CLI via the -c flag and can be either JavaScript or JSON

module.exports = {
  root: 'path/to/css',
  autoprefixer: { browsers: ['> 1%', 'IE 7'], cascade: false },
  'postcss-calc': { preserve: true }
}
{
  "root": "path/to/css",
  "autoprefixer": { "browsers": ["> 1%", "IE 7"], "cascade": false },
  "postcss-calc": { "preserve": true }
}

Options are merged recursively with the defaults. For example, adding new plugins to the use array will result in them being merged alongside the existing ones.

Adding additional plugins

By default the preprocessor uses all necessary plugins to build SUIT components. However additional plugins can be installed into a project and then added to the use array.

Note: This will not work with the preprocessor installed globally. Instead rely on the convenience of npm run <script>

module.exports = {
  use: [
    'postcss-property-lookup'
  ]
};
{
  "name": "my-pkg",
  "version": "0.1.0",
  "dependencies": {
    "postcss-property-lookup": "^1.1.3",
    "suitcss-preprocessor": "^0.5.0"
  },
  "scripts": {
    "preprocess": "suitcss -c myconfig.js index.css build/built.css"
  }
}
npm run preprocess

Changing plugin order

If duplicate plugins are used they will be removed, but the new order will be respected. This is useful if you need to change the default order:

// Default order
var defaults = [
  'postcss-import',
  'postcss-custom-properties',
  'postcss-calc',
  'postcss-custom-media',
  'autoprefixer',
  'postcss-reporter'
];

// config
module.exports = {
  use: [
    'postcss-at2x',
    'postcss-calc',
    'autoprefixer',
    'postcss-reporter'
  ]
};

var result = [
  'postcss-import',
  'postcss-custom-properties',
  'postcss-custom-media',
  'postcss-at2x',
  'postcss-calc',
  'autoprefixer',
  'postcss-reporter'
];

Acknowledgements

Based on Myth by Segment.io.

preprocessor's People

Contributors

simonsmith avatar necolas avatar giuseppeg avatar

Stargazers

joshua chavanne avatar

Watchers

James Cloos avatar joshua chavanne avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.