-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
79 lines (72 loc) · 3.13 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
"use strict";
const postcss = require("postcss");
const gzipSize = require("gzip-size");
const fs = require("fs");
const prettyBytes = require("pretty-bytes");
module.exports = postcss.plugin("postcss-extract-to-file", function postcssExtractToFile(options) {
return function (css) {
return new Promise(function (resolve, reject) {
options = options || {};
// Create two arrays, one for the extracted rules, another for the remaining ones
var extracted = [];
var remaining = []
css.walkRules(function (rule, index) {
// We check each rule against the possible overrides. If it matches an override it gets moved to the extracted file
if (checkOverrides(options.extractors, rule.selector)) {
extracted.push(rule);
}
// Otherwise it gets written to the 'remaining' rule
else {
remaining.push(rule);
}
});
return Promise.all([writeExtractedToFile(extracted), writeRemainingToFile(remaining)]).then(resolve, reject);
});
}
function writeExtractedToFile (extracted) {
return new Promise(function (resolve, reject) {
fs.writeFile(options.extracted, extracted, function (err) {
if (err) reject(err);
// Let"s create a message we can log and write to file
const message = createMessage(options.extracted);
// Append message to file
fs.appendFile(options.extracted, message);
// Log the message
console.log("Saved the extracted rules." + message);
resolve();
});
});
}
// Utility function to create a message with file sizes
function createMessage (filename) {
const message = `\n/* ${filename} — size ${fs.statSync(filename).size} bytes/${prettyBytes(gzipSize.sync(fs.readFileSync(filename, 'utf8')))} gzipped, generated on ${fs.statSync(filename).mtime} */ \n`;
return message;
};
function writeRemainingToFile (remaining) {
return new Promise(function (resolve, reject) {
fs.writeFile(options.remaining, remaining, function (err) {
if (err) reject(err);
// Let"s create a message we can log and write to file
const message = createMessage(options.remaining);
// Append the message to file
fs.appendFile(options.remaining, message);
// Log the message
console.log("Saved the remaining rules." + message);
resolve();
});
});
}
// List is our array of possible overrides to check against
function checkOverrides (list, selector) {
if (!list) {
console.log("You have not supplied any selectors to extract against");
}
for (var i = 0, len = list.length; i < len; i++) {
let item = list[i];
if (selector.indexOf(item) > -1) {
return true;
}
}
return false;
}
});