A PostCSS runner is a tool that processes CSS through a user-defined list
of plugins; for example, postcss-cli
or gulp‑postcss
.
These rules are mandatory for any such runners.
For single-plugin tools, like gulp-autoprefixer
,
these rules are not mandatory but are highly recommended.
See also ClojureWerkz’s recommendations for open source projects.
If your runner uses a config file, it must be written in JavaScript, so that
it can support plugins which accept a function, such as postcss-assets
:
module.exports = [
require('postcss-assets')({
cachebuster: function (file) {
return fs.statSync(file).mtime.getTime().toString(16)
}
})
]
from
and to
processing optionsTo ensure that PostCSS generates source maps and displays better syntax errors,
runners must specify the from
and to
options. If your runner does not handle
writing to disk (for example, a gulp transform), you should set both options
to point to the same file:
processor.process({ from: file.path, to: file.path })
PostCSS runners must use only the asynchronous API. The synchronous API is provided only for debugging, is slower, and can’t work with asynchronous plugins.
processor.process(opts).then(result => {
// processing is finished
});
PostCSS runners 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.
CssSyntaxError
PostCSS runners must not show a stack trace for CSS syntax errors, as the runner can be used by developers who are not familiar with JavaScript. Instead, handle such errors gracefully:
processor.process(opts).catch(error => {
if (error.name === 'CssSyntaxError') {
process.stderr.write(error.message + error.showSourceCode())
} else {
throw error
}
})
result.warnings()
PostCSS runners must output warnings from result.warnings()
:
result.warnings().forEach(warn => {
process.stderr.write(warn.toString())
})
See also postcss-log-warnings and postcss-messages plugins.
PostCSS by default will inline source maps in the generated file; however, PostCSS runners must provide an option to save the source map in a different file:
if (result.map) {
fs.writeFile(opts.to + '.map', result.map.toString())
}
PostCSS runners must have their README.md
wrote 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
).
PostCSS runners must describe changes of all releases in a separate file,
such as ChangeLog.md
, History.md
, or with GitHub Releases.
Visit Keep A Changelog for more information on how to write one of these.
Of course, you should use SemVer.
postcss-runner
keyword in package.json
PostCSS runners written for npm must have the postcss-runner
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 recommended if the package format is allowed to contain keywords.