-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
386 lines (316 loc) · 10.9 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var mkdirp = require('mkdirp');
var postcss = require('postcss');
var MapGenerator = require('postcss/lib/map-generator');
var Result = require('postcss/lib/result');
var pkg = require('./package.json');
var helpers = require('./lib/helpers');
/**
* Plugin default options
* @const
*/
var defaults = {
maxSelectors: 4000,
fileName: '%original%-%i%',
fileNameStartIndex: 0,
writeFiles: true,
writeSourceMaps: true,
writeImport: true,
quiet: false
};
/**
* At-Rules, contents should not be separated between roots
* @type {Array.<String>}
*/
var unbreakableAtRules = ['keyframes'];
var roots;
var treeSelectors;
var selectors;
var options;
var messages;
/**
* Moves rules from start to, including, end nodes from root to new postcss.root
* @param {Node} startNode
* @param {Node} endNode
* @param {Root} root
* @returns {Root}
*/
var moveNodes = function (startNode, endNode, root) {
var newRoot = postcss.root({ raws: root.raws });
var foundStart;
var parentsRelations = [
[root, newRoot]
];
root.walk(function (node) {
if (node.type !== 'decl' && node.type !== 'rule') return true;
if (node.type === 'decl' && node.parent.type !== 'atrule') return true;
if (node === startNode) foundStart = true;
if (foundStart) {
var newParent = helpers.find(node.parent, parentsRelations);
if (!newParent) {
var nodeParent = node.parent;
var copyParent, prevCopyParent;
// copy parents
while (nodeParent.type !== 'root') {
copyParent = nodeParent.clone({
nodes: [],
raws: nodeParent.raws
});
parentsRelations.push([nodeParent, copyParent]);
prevCopyParent && copyParent.append(prevCopyParent);
!newParent && (newParent = copyParent);
prevCopyParent = copyParent;
nodeParent = nodeParent.parent;
}
newRoot.append(copyParent);
}
// move node, remove its parents if it was the only one node here
var nodeParents = helpers.parents(node);
node.remove();
newParent.append(node);
nodeParents.forEach(function (parent) {
!parent.nodes.length && parent.remove();
});
}
if (node === endNode) {
return false;
}
});
return newRoot;
};
/**
* Checks if passed node is At-Rule and it exists in unbreakable at-rules list
* @param {Node} node
* @returns {boolean}
*/
var isUnbreakableAtRule = function (node) {
return node.type === 'atrule' && unbreakableAtRules.indexOf(node.name) !== -1;
};
/**
* Splits rule selectors by provided position
*
* @param {Rule} node
* @param {Number} position
* @returns {undefined|Rule}
*/
var splitRule = function (node, position) {
if (!position) return null;
var newNode = node.clone({
selector: node.selectors.slice(position).join(',')
});
node.selector = node.selectors.slice(0, position).join(',');
node.parent.insertAfter(node, newNode);
return node;
};
/**
* Walking by css nodes and if passed selectors count are below
* the maxSelectors option move them to another style file. Recursive.
*
* @param {Root} css
*/
var processTree = function (css) {
var startingNode, endNode, prevNode;
css.walk(function (node) {
if (isUnbreakableAtRule(node)) {
prevNode = node.last;
treeSelectors += 1;
selectors += 1;
return true;
}
if (isUnbreakableAtRule(node.parent)) {
return true;
}
if (!node.selector) return true;
!startingNode && (startingNode = node);
if ((treeSelectors += node.selectors.length) > options.maxSelectors) {
var selInSource = node.selectors.length - (treeSelectors - options.maxSelectors);
endNode = splitRule(node, selInSource) || prevNode;
selectors += selInSource ? endNode.selectors.length : 0;
var newFile = moveNodes(startingNode, endNode, css);
roots.push(newFile);
treeSelectors = 0;
processTree(css);
return false;
} else {
selectors += node.selectors.length;
}
prevNode = node;
});
};
/**
* Log message to PostCSS
* Arguments are used for filling template
*
* @param {string} msg
*/
var log = function (msg/* ...args */) {
Array.prototype.slice.call(arguments, 1, arguments.length).forEach(function (data) {
msg = msg.replace(/%s/, data);
}, this);
messages.push({
type: 'info',
plugin: pkg.name,
text: msg
});
if (options.quiet) return;
var args = [
chalk.green('>>') + ' ' + pkg.name + ': ' + msg
];
console.log.apply(this, args);
};
/**
* fs.writeFile which returns Promise
*
* @param {string} [dest] destination path
* @param {string} [src] contents of the file
* @param {boolean} [mkDir=false] recursive make dir to dest
* @returns {Promise}
*/
var writePromise = function (dest, src, mkDir) {
return new Promise(function (res, rej) {
var writeFileHandler = function () {
fs.writeFile(dest, src, function (err) {
err ? rej(err) : res();
});
};
if (mkDir) {
mkdirp(path.dirname(dest), function (errMkdir) {
errMkdir ? rej(errMkdir) : writeFileHandler();
});
} else {
writeFileHandler();
}
});
};
var processRoot = function (processor, root, index, destination, result) {
return new Promise(function (resolve, reject) {
var filesForWrite = [];
var fileNameStartIndex = Number(options.fileNameStartIndex);
fileNameStartIndex = isNaN(fileNameStartIndex) ? 0 : fileNameStartIndex;
var fileIndex = index + fileNameStartIndex;
var fileName = helpers.getFileName(options.fileName, destination, fileIndex);
var rootOpts = helpers.extend({}, result.opts, { to: fileName });
var rootResult = new Result(processor, root, rootOpts);
var rootData = new MapGenerator(postcss.stringify, root, result.opts).generate();
rootResult.css = rootData[0];
rootResult.map = rootData[1];
if (!rootResult.opts.to) {
if (options.writeFiles) {
result.warn(
'Destination is not provided, ' +
'splitted css files would not be written'
);
}
return resolve(rootResult);
}
if (!options.writeFiles) return resolve(rootResult);
filesForWrite.push(
writePromise(
rootResult.opts.to, rootResult.css, true
)
);
if (options.writeSourceMaps && rootResult.map) {
filesForWrite.push(
writePromise(
rootResult.opts.to + '.map',
rootResult.map.toString(),
true
)
);
}
Promise.all(filesForWrite).then(function () {
resolve(rootResult);
}).catch(reject);
});
};
/**
* Export plugin
*/
module.exports = postcss.plugin(pkg.name, function (opts) {
opts = opts || {};
// merging default settings
helpers.defaults(opts, defaults);
options = opts;
return function (css, result) {
messages = [];
// ensure plugin running in the end
if (result.processor.plugins[result.processor.plugins.length - 1] !== result.lastPlugin) {
// remove plugin duplicates
result.processor.plugins = result.processor.plugins.filter(function (plugin) {
return plugin.postcssPlugin === pkg.name ? plugin === result.lastPlugin : true;
});
// adding it to the end of the plugins array
result.processor.plugins.push(result.lastPlugin);
return false;
}
roots = [];
treeSelectors = selectors = 0;
processTree(css);
result.root = css;
// for that old node.js
var destination = result.opts.to ?
(path.parse ? path.parse : require('path-parse'))(result.opts.to) :
null;
var rootsProcessing = [];
roots.forEach(function (root, index) {
rootsProcessing.push(
processRoot(result.processor, root, index, destination, result)
);
}, this);
if (roots.length) {
log('Divided into %s style files %s (Found %s selectors)',
roots.length,
result.opts.to ? 'from ' + result.opts.to : '',
selectors
);
} else {
log('Found %s selectors, skipping %s',
selectors,
result.opts.to || ''
);
}
return new Promise(function (pluginDone, pluginFailed) {
Promise.all(rootsProcessing).then(function (processedRoots) {
result.roots = processedRoots;
if (options.writeImport && processedRoots.length) {
// find charset node
var charsetRule;
css.walkAtRules('charset', function (rule) {
if (!charsetRule) {
charsetRule = rule;
return false;
}
});
if (charsetRule) {
charsetRule.remove();
css.prepend(charsetRule);
}
var destinationError;
for (var i = processedRoots.length - 1; i >= 0; i--) {
if (processedRoots[i].opts.to) {
var importNode = postcss.atRule({
name: 'import',
params: 'url(' + path.basename(processedRoots[i].opts.to) + ')'
});
charsetRule ?
css.insertAfter(charsetRule, importNode) :
css.prepend(importNode);
} else {
destinationError = true;
}
}
if (destinationError) {
result.warn(
'Destination is not provided, ' +
'@import directive will be not written'
);
}
}
result.messages = result.messages.concat(messages);
pluginDone();
}).catch(pluginFailed);
});
};
});