Load Plugins
Autoload Plugins for PostCSS
Install
npm i -D postcss-load-plugins
Usage
Install plugin as usual and make sure saving them to your package.json dependencies and/or devDependencies.
npm i -S postcss-plugin
npm i -D postcss-plugin
After installing your plugins there a two common ways to declare your plugins and options.
- Create postcss section in your projects package.json.
- Create a postcss.config.js or postcssrc.json file.
Options
Plugin options can either take false
or an object literal
{}
as value.
false
: Plugin loads with no options (defaults)
[Object]
: Plugin loads with set options.
Order
Plugin order is determined by declaration in the plugins section.
postcss: {
plugins: {
'postcss-plugin1': false,
'postcss-plugin2': false,
'postcss-plugin3': {}
}
}
[
require('postcss-plugin1')(),
require('postcss-plugin2')(),
require('postcss-plugin3')(options)
]
Examples
package.json
{
"dependencies": {
"postcss-bem": "^0.2.2",
"postcss-nested": "^1.0.0",
"postcss-import": "^8.1.2"
},
"postcss": {
"plugins": {
"postcss-import": false,
"postcss-nested": false,
"postcss-bem": {
"style": "bem",
"separators": {
"namespace": "-",
"descendent":"__",
"modifier": "--"
}
}
}
}
}
postcss.config.js
module.exports = {
plugins: {
'postcss-import': false,
'postcss-nested': false,
'postcss-bem': {
style: 'bem',
separators: {
namespace: '-',
descendent: '__',
modifier: '--'
}
}
}
}
postcssrc.json
{
"plugins": {
"postcss-import": false,
"postcss-nested": false,
"postcss-bem": {
"style": "bem",
"separators": {
"namespace": "-",
"descendent":"__",
"modifier": "--"
}
}
}
}
const { readFileSync } = require('fs')
const postcss = require('postcss')
const pluginsrc = require('postcss-load-plugins')()
const css = readFileSync('./index.css', 'utf8')
pluginsrc.then((plugins) => {
postcss(plugins)
.process(css)
.then(result => console.log(result.css))
}))
LICENSE
License (MIT)
Copyright (c) 2016 Michael Ciniawsky michael.ciniawsky@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.