1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _postcss = require('postcss');
|
8 |
|
9 | var _postcss2 = _interopRequireDefault(_postcss);
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var getSelLength = function getSelLength(node) {
|
19 | if (node.type === 'rule') {
|
20 | return node.selectors.length;
|
21 | }
|
22 | if (node.type === 'atrule') {
|
23 | return 1 + node.nodes.reduce(function (memo, n) {
|
24 | return memo + getSelLength(n);
|
25 | }, 0);
|
26 | }
|
27 | return 0;
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | exports.default = _postcss2.default.plugin('postcss-chunk', function () {
|
38 | var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
39 |
|
40 | var _ref$size = _ref.size;
|
41 | var size = _ref$size === undefined ? 4000 : _ref$size;
|
42 | var _ref$result = _ref.result;
|
43 | var genResult = _ref$result === undefined ? function () {
|
44 | return {};
|
45 | } : _ref$result;
|
46 |
|
47 | return function (css, result) {
|
48 | var chunks = [];
|
49 | var count = void 0;
|
50 | var chunk = void 0;
|
51 |
|
52 |
|
53 | var nextChunk = function nextChunk() {
|
54 | count = 0;
|
55 | chunk = css.clone({ nodes: [] });
|
56 | chunks.push(chunk);
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 | css.nodes.forEach(function (n) {
|
62 | var selCount = getSelLength(n);
|
63 | if (!chunk || count + selCount > size) {
|
64 | nextChunk();
|
65 | }
|
66 | chunk.nodes.push(n);
|
67 | count += selCount;
|
68 | });
|
69 |
|
70 |
|
71 | result.chunks = chunks.map(function (c, i) {
|
72 | return c.toResult(genResult(i, c));
|
73 | });
|
74 | };
|
75 | });
|
76 |
|
\ | No newline at end of file |