-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·49 lines (41 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import postcss from "postcss";
import pluginImports from "postcss-import";
import pluginDefineProps from "postcss-define-property";
import pluginNestedVars from "postcss-nested-vars";
import pluginAtFor from "postcss-for";
import pluginAtIf from "postcss-conditionals";
import extendInternal from "./lib/extend";
import { atRules, rules, cleanup, rulesPreprocess } from "./lib/plugin";
/**
* opts: { allowShorthands: ?boolean, webpack: ?boolean }
*/
module.exports = postcss.plugin("postcss-react-bender", function(opts) {
const pluginOptions = { allowShorthand:true, webpack:false, ...opts };
return postcss([
pluginImports,
pluginDefineProps,
pluginNestedVars,
pluginAtFor({
resolve: pluginOptions.webpack ? require('./resolve-id'): undefined
}),
pluginAtIf,
extendInternal
]).use((root, result) => {
// Bender style tree
let tree = {};
// process all atrule typed nodes in order to resolve all
// @extend or @css-selector definitions first.
root.walkAtRules(rule => atRules({ rule, tree }, pluginOptions));
// Preprocess combined/multiple component level rule definitions
// and re-aling them as one unique rule.
root.each(rule => rulesPreprocess({ rule, tree }, pluginOptions));
// Process nodes to extract Bender styles to style tree
// or modify original rule definition to create css values.
root.walkRules(rule => rules({ rule, tree }, pluginOptions));
// Final cleanup process to remove unnecessary empty nodes
// and non-processed leftovers.
root.walkRules(rule => cleanup(rule, pluginOptions));
// expose generated tree to plugin consumers.
result.bender = tree;
});
});