Détail du package

suitcss-preprocessor

suitcss559MIT4.0.0

A CSS preprocessor for working with SUIT CSS

css, preprocessor, stylelint, pure

readme

suitcss-preprocessor

Build Status 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 stylelint. Minification is provided by cssnano.

Additional plugins can be added via the configuration options.

Installation

npm install suitcss-preprocessor

Usage

suitcss input.css output.css

API

Command Line

Options are documented below

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

Options:

  -h, --help                output usage information
  -c, --config [path]       a custom PostCSS config file
  -i, --import-root [path]  the root directory for imported css files
  -s, --encapsulate         encapsulate component styles
  -w, --watch               watch the input file and any imports for changes
  -m, --minify              minify output with cssnano
  -e, --throw-error         throw an error when any warnings are found
  -L, --no-lint             disable stylelint and postcss-bem-linter
  -v, --verbose             log verbose output for debugging
  -V, --version             output the version number

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

preprocessor(css: String [, options: Object] [, filename: String]);
  • css: CSS input (required)
  • options: Options to the preprocessor (see below) (optional)
  • filename: Filename of the input CSS file (optional)

Example

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

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

preprocessor(css, {
  root: 'path/to/css',
  minify: true,
}, filename).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.

debug

  • Type: Function
  • Default: identity (it does nothing)

Before preprocessing debug is invoked on the postcss plugins array. This allows you to pass a postcss-debug instance.

var preprocessor = require('suitcss-preprocessor');
var createDebugger = require('postcss-debug').createDebugger;
var debug = createDebugger();

preprocessor(css, {
  debug: debug
}).then(function () {
  debug.inspect();
});

N.B. debug should always take one argument that is plugins and eventually return it:

function debug(plugins) {
  // do something with plugins here
  return plugins;
}

encapsulate

(experimental)

  • Type: Boolean
  • Default: false

Resets CSS properties to their initial values to effectively allow a component to opt out of CSS inheritance and be encapsulated from the rest of the application similar to the Shadow DOM. There are two types of CSS properties that affect components, inherited (e.g. font-size, color) and non-inherited (e.g. margin, background). This option works so that:

  • Root elements (e.g. .Component) have both inherited and non-inherited properties reset to default values.
  • Descendants (e.g. .Component-item) only have non-inherited properties reset as this allows properties set on the root element to be inherited by its descendants.

This means that components are isolated from styles outside the component root element but should an inheritable property such as font-size be applied on the component root element it will be inherited by the component descendants as normal. This prevents the need to redeclare properties on every descendant in a component.

The same rules also apply to nested components.

Rationale

One of the difficulties with CSS components is predictability. Unwanted styles can be inherited from parent components and this can make it difficult to reuse components in different contexts.

Methodologies such as SUIT and BEM exist to solve problems around the cascade and specificity but they cannot protect components from inheriting unwanted styles. What would really help is to allow inheritance to be 'opt-in' and let component authors decide what properties are inherited. This creates a more predictable baseline for styling components and promoting easier reuse.

Examples

What about all: initial?

The all: initial declaration will reset both inherited and non-inherited properties but this can be too forceful. For example display is reset to inline on block elements and as mentioned earlier, descendants of a component should only have non-inherited properties reset to allow declarations to be inherited from the root element.

For example, if an author specifies all: initial on an element it will block all inheritance and reset all properties, as if no rules appeared in the author, user, or user-agent levels of the cascade.

https://www.w3.org/TR/css3-cascade/#all-shorthand

Instead a subset of properties are reset to allow more granular control over what parts of a component use inheritance.

To achieve this the preprocessor uses postcss-autoreset with the SUIT preset and a custom set of CSS properties that are reset to their initial values. Only selectors conforming to the SUIT naming conventions are affected.

Caveats

Selectors must be present in the component CSS

If an element is present in the HTML but not styled in the component CSS (perhaps relying on utility classes) it will not be reset. In this instance an empty ruleset can be added to ensure it is correctly reset:

<div class="Component u-posRelative u-textCenter">
  <div class="Component-item"></div>
</div>
/**
 * Empty ruleset required.
 * Note the disabling of stylelint
 */

/* stylelint-disable-next-line block-no-empty */
.Component {}

.Component-item {
  color: red;
}
Global styles can still override descendants

Because component descendants only have non-inheritable properties reset it can lead to specific global rules still applying:

/* global.css */
span {
  color: red;
}

/* component.css */
.Component-text {
  font-style: bold;
}
<div class="Component">
  <span class="Component-text">
    <!-- this text is red -->
  <span>
</div>

The solution to this is to minimise or avoid entirely the use of global styles which is the recommended approach in a SUIT CSS application.

lint

  • Type: Boolean
  • Default: true

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

Stylelint configuration options can also be overridden but this requires the stylelint-config-suitcss to be installed locally in your package.

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

minify

  • Type: Boolean
  • Default: false

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

postcss

  • Type: Object
  • Default: undefined

Options that are passed directly to postcss, as per the documentation.

{
  postcss: {from: 'filename.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. They will be appended to the existing list of plugins.

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-custom-properties',
  'postcss-calc',
  'postcss-color-function',
  'postcss-custom-media',
  'postcss-apply',
];

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

var result = [
  'postcss-custom-properties',
  'postcss-color-function',
  'postcss-custom-media',
  'postcss-apply',
  'postcss-at2x',
  'postcss-calc',
];

Note Some core plugins such as postcss-easy-import and autoprefixer cannot be re-ordered. This is to ensure components are preprocessed correctly.

Autoprefixer: vendor prefixes

By default the preprocessor uses the SUIT browserslist configuration:

> 1%, last 2 versions, safari > 6, ie > 9, ios > 6, android > 4.3, samsung > 3, chromeandroid > 50

The preprocessor doesn't attempt to find any browserslist config file.

Instead you can customise the browsers list via configuration file.

Acknowledgements

Based on Myth by Segment.io.

changelog

HEAD

4.0.0 (April 18, 2017)

  • Drop support for Node 0.12
  • Update postcss-easy-import to 2.0.0
  • Update fs-extra to 2.1.2
  • Update autoprefixer to 6.7.7
  • Update postcss-apply to 0.6.1
  • Update postcss-color-function to 3.0.0
  • Update postcss-reporter to 3.0.0
  • Update postcss to 5.2.17
  • Update stylelint-config-suitcss to 11.0.0
  • Update stylelint to 7.10.1
  • Update devDependencies to latest

3.0.1 (December 09, 2016)

  • Fix: Allow --importRoot to override root config option #70
  • Fix: Setting lint to false also disables postcss-bem-linter #68
  • Allow config file to be arbitrarily named #66
  • Fix: undefined cli flags don't override config #62
  • Update postcss-reporter to ^2.0.0 #63

3.0.0 (October 20, 2016)

  • Pin core plugins order:

    postcss-easy-import [ user plugins ] [ encapsulation plugins ] autoprefixer postcss-reporter

  • Add experimental encapsulate option/feature
  • CLI flags override config file options
  • Input file is now linted (previously only imported files were passed through the linting tools)
  • Add --throw-error (-e) to CLI
  • Enable lint option by default
  • Add postcss-apply
  • Add postcss-color-function
  • Add debug option to pass an postcss-debug instance
  • Allow usage of transform and onImport in postcss-easy-import
  • Remove beforeLint feature
  • Add default browsers list for autoprefixer
  • Use stylelint-config-suitcss from within the preprocessor. No longer needs to be installed locally by the consumer.

2.0.0 (April 12, 2016)

  • Switch to postcss-easy-import
  • Major release to correct previous upgrade to stylelint 5.x

1.0.2 (March 17, 2016)

  • Upgrade stylelint to ^5.0.1

1.0.1 (February 17, 2016)

  • Fix issue with CSS returned from beforeLint not being used correctly - a07bb8c
  • Fix incorrect date in CHANGELOG.md

1.0.0 (February 08, 2016)

  • Upgrade stylelint to version 4.2.0
  • Upgrade postcss-import to 8.0.2
  • Improve tests
  • Use stylelint-config-suitcss as a dev dependency as it must be installed where the preprocessor is being used.

0.8.0 (December 01, 2015)

0.7.0 (November 27, 2015)

  • Add beforeLint function which allows imported CSS to be manipulated before linting
  • Flatten configuration options so that PostCSS specific settings are no longer inside config: {}.

0.6.0 (November 25, 2015)

  • Allow re-ordering of the use plugins array from config
  • Update autoprefixer to ^6.1.1
  • Update postcss-bem-linter to ^2.3.0

0.5.1 (November 24, 2015)

  • Fix issue with options not being passed to postcss-bem-linter and postcss-reporter

0.5.0 (November 23, 2015)

  • Switch from Rework to PostCSS
  • Add support for configuration file (-c)
  • Move --compress to --minify and use cssnano
  • Fix --watch to run when imported files change
  • Allow additional PostCSS plugins to be added to the pipeline
  • Node API now returns a promise
  • Upgrade existing package.json dependencies

0.4.0 (July 1, 2014)

  • Update 'rework-suit' to ^4.0.0.

0.3.0 (June 21, 2014)

  • Add 'import-root' CLI option.
  • Add 'compress' CLI option.
  • Update 'autoprefixer' to ^1.3.0.
  • Update 'rework-suit' to ^3.0.0.
  • Update 'rework' to ^1.0.0.
  • Support configuration.

0.2.0 (April 21, 2014)

  • Update 'autoprefixer' to ^1.1.20140410.
  • Update 'rework-suit' to 2.0.x.

0.1.1 (March 24, 2014)

  • Initial release