Hi, Andrey!
I have few questions about migration to PostCSS 8.
In stylelint every rule is in fact a PostCSS plugin. In stylelint-order I use postcss-sorting for autofixing. So I call PostCSS plugin from another plugin.
Here's very simplified part of stylelint-order rule code looks:
let postcssSorting = require('postcss-sorting');
// ...
root.walk(function processRulesAndAtrules(node) {
// ... skip node, which doesn't have nodes insides
// ...
let tempRoot = postcss.root({ nodes: [node] });
postcssSorting(sortingOptions)(tempRoot);
});
Assume that postcss-sorting
still uses PostCSS 7, so it exports a function, which returns a function.
It takes a node (rule or at-rule, or maybe even root if it's parsed with postcss-css-in-js
or postcss-html
) and then run postcss-sorting over it.
postcss.root({ nodes: [node] })
ok to do? API docs are empty about this.Result
.exports
in package.json
? I tried the following, but I’m getting an error about an "incorrect type":"exports": {
"node": {
"require": "./dist/index.cjs"
}
}
class Plugin {
get postcssPlugin() {
return 'to-red'
}
Declaration(decl) {
console.log(decl.toString())
decl.value = 'red'
}
static get postcss() {
return true
}
}
postcss([ Plugin ]).process('a { color: black }', { from: '!' }).then(({ css }) => console.log(css))
postcss/lib/processor.js:40
i = i()
^
TypeError: Class constructor Plugin cannot be invoked without 'new'
Hi, folks, trying to add some declaration to each rule, isn't this considered as right usage?
module.exports = opts => ({
postcssPlugin: 'postcss-plugin',
Rule (rule) {
rule.prepend({ prop: 'background-image', value: `url('path/url')` });
}
});
It goes through each rule but does not modify the results (rule.nodes after prepend() remain the same) :(
Is the only option to use Root () {} method and walk through each rule explicitly?
Rule (rule, { Declaration }) {
let color = new Declaration({ prop: 'color', value: 'black' })
rule.append(color)
}