This repository was archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconcatCSS.js
54 lines (43 loc) · 1.41 KB
/
concatCSS.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
// everything is terrible.
var Promise = require('bluebird'),
pathlib = require('path'),
fs = Promise.promisifyAll(require('fs'));
var chunkString = function (str, re) {
var match,
lastIndex = 0,
ret = [];
while (match = re.exec(str)) {
if (match.index > lastIndex) {
ret.push([str.slice(lastIndex, match.index)]);
}
lastIndex = match.index + match[0].length;
ret.push(match);
}
if (lastIndex < str.length) {
ret.push([str.slice(lastIndex)]);
}
return ret;
};
var processFile = exports.processFile = function (path) {
return fs.readFileAsync(path).then(function (imported) {
return processString(imported, pathlib.dirname(path));
});
};
var processString = exports.processString = function (data, path) {
// this is incomplete by design and lack of will to make it complete.
var importRegex = /^@import\s+url\(\s*"(.+)"\s*\);$/gm;
return Promise.map(chunkString(data, importRegex), function (part) {
// part is either the match, or an array containing just a string.
if (!part[1]) {
return part[0];
}
var importPath = part[1],
relativePath;
if (path) {
importPath = pathlib.resolve(path, importPath);
}
return processFile(importPath);
}).reduce(function (ret, val) {
return ret + val;
}, '');
};