A PostCSS plugin is a function that receives and, usually, transforms a CSS AST from the PostCSS parser.
The rules below are mandatory for all PostCSS plugins.
See also ClojureWerkz’s recommendations for open source projects.
postcss-
prefixThe plugin’s purpose should be clear just by reading its name.
If you wrote a transpiler for CSS 4 Custom Media, postcss-custom-media
would be a good name. If you wrote a plugin to support mixins,
postcss-mixins
would be a good name.
The prefix postcss-
shows that the plugin is part of the PostCSS ecosystem.
This rule is not mandatory for plugins that can run as independent tools, without the user necessarily knowing that it is powered by PostCSS — for example, cssnext and Autoprefixer.
Do not create multitool plugins. Several small, one-purpose plugins bundled into a plugin pack is usually a better solution.
For example, cssnext contains many small plugins, one for each W3C specification. And cssnano contains a separate plugin for each of its optimization.
Preprocessors libraries like Compass provide an API with mixins.
PostCSS plugins are different. A plugin cannot be just a set of mixins for postcss-mixins.
To achieve your goal, consider transforming valid CSS or using custom at-rules and custom properties.
postcss.plugin
By wrapping your function in this method, you are hooking into a common plugin API:
module.exports = postcss.plugin('plugin-name', function (opts) {
return function (root, result) {
// Plugin code
};
});
A CI service like Travis is also recommended for testing code in different environments. You should test in (at least) Node.js 0.12 and stable.
For example, use fs.writeFile
instead of fs.writeFileSync
:
postcss.plugin('plugin-sprite', function (opts) {
return function (root, result) {
return new Promise(function (resolve, reject) {
var sprite = makeSprite();
fs.writeFile(opts.file, function (err) {
if ( err ) return reject(err);
resolve();
})
});
};
});
node.source
for new nodesEvery node must have a relevant source
so PostCSS can generate
an accurate source map.
So if you add new declaration based on some existing declaration, you should
clone the existing declaration in order to save that original source
.
if ( needPrefix(decl.prop) ) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop });
}
You can also set source
directly, copying from some existing node:
if ( decl.prop === 'animation' ) {
var keyframe = createAnimationByName(decl.value);
keyframes.source = decl.source;
decl.root().append(keyframes);
}
PostCSS plugins must not rely on undocumented properties or methods, which may be subject to change in any minor release. The public API is described in API docs.
node.error
on CSS relevant errorsIf you have an error because of input CSS (like an unknown name
in a mixin plugin) you should use node.error
to create an error
that includes source position:
if ( typeof mixins[name] === 'undefined' ) {
throw decl.error('Unknown mixin ' + name, { plugin: 'postcss-mixins' });
}
result.warn
for warningsDo not print warnings with console.log
or console.warn
,
because some PostCSS runner may not allow console output.
if ( outdated(decl.prop) ) {
result.warn(decl.prop + ' is outdated', { node: decl });
}
If CSS input is a source of the warning, the plugin must set the node
option.
PostCSS plugins must have their README.md
written in English. Do not be afraid
of your English skills, as the open source community will fix your errors.
Of course, you are welcome to write documentation in other languages;
just name them appropriately (e.g. README.ja.md
).
The plugin's README.md
must contain example input and output CSS.
A clear example is the best way to describe how your plugin works.
The first section of the README.md
is a good place to put examples.
See postcss-opacity for an example.
Of course, this guideline does not apply if your plugin does not transform the CSS.
PostCSS plugins must describe the changes of all their releases
in a separate file, such as CHANGELOG.md
, History.md
, or GitHub Releases.
Visit Keep A Changelog for more information about how to write one of these.
Of course, you should be using SemVer.
postcss-plugin
keyword in package.json
PostCSS plugins written for npm must have the postcss-plugin
keyword
in their package.json
. This special keyword will be useful for feedback about
the PostCSS ecosystem.
For packages not published to npm, this is not mandatory, but is recommended if the package format can contain keywords.