1 | import postcss from 'postcss';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const getSelLength = (node) => {
|
9 | if (node.type === 'rule') {
|
10 | return node.selectors.length;
|
11 | }
|
12 | if (node.type === 'atrule') {
|
13 | return 1 + node.nodes.reduce((memo, n) => {
|
14 | return memo + getSelLength(n);
|
15 | }, 0);
|
16 | }
|
17 | return 0;
|
18 | };
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export default postcss.plugin('postcss-chunk', ({
|
28 | size = 4000,
|
29 | result: genResult = () => {
|
30 | return {};
|
31 | },
|
32 | } = {}) => {
|
33 | return (css, result) => {
|
34 | const chunks = [];
|
35 | let count;
|
36 | let chunk;
|
37 |
|
38 |
|
39 | const nextChunk = () => {
|
40 | count = 0;
|
41 | chunk = css.clone({nodes: []});
|
42 | chunks.push(chunk);
|
43 | };
|
44 |
|
45 |
|
46 |
|
47 | css.nodes.forEach((n) => {
|
48 | const selCount = getSelLength(n);
|
49 | if (!chunk || count + selCount > size) {
|
50 | nextChunk();
|
51 | }
|
52 | chunk.nodes.push(n);
|
53 | count += selCount;
|
54 | });
|
55 |
|
56 |
|
57 | result.chunks = chunks.map((c, i) => {
|
58 | return c.toResult(genResult(i, c));
|
59 | });
|
60 | };
|
61 | });
|