-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (47 loc) · 1.8 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
'use strict';
var path = require('path');
var namedRegexp = require('named-js-regexp');
module.exports = function (css, resourcePath, aliases) {
var IMPORT_REGEX = namedRegexp(/^(:<import>@import\s+)(:<less_import_options>\((reference|inline|less|css|once|multiple|optional)\)\s)?\s*(:<url_tag_open>url\()?\s*(:<quote_open>"|')(:<url>.+)\s*(:<quote_close>"|')(:<url_tag_close>\))?(:<context>.*);$/gm);
var URL_REGEX = namedRegexp(/(:<url_tag_open>url\()\s*(:<quote_open>"|')?(:<url>.+?)\s*(:<quote_close>"|')?(:<url_tag_close>\))/gm);
css = parse(css, IMPORT_REGEX);
css = parse(css, URL_REGEX);
return css;
function resolve(url, resourcePath) {
for (var aliasName in aliases) {
var regExp = new RegExp('^' + aliasName + '(.*)');
if (regExp.test(url)) {
return path.relative(path.dirname(resourcePath), path.resolve(aliases[aliasName] + url.replace(regExp, '$1'))).replace(/\\/g, '/');
}
}
return url;
}
function parse(css, regex) {
var m, importOriginal, importReplace, replace = [];
while ((m = regex.exec(css)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
replace.push(m);
}
for (var item in replace) {
m = replace[item];
importOriginal = '';
importReplace = '';
var mathGroups = m.groups();
for (var groupName in mathGroups) {
var match = mathGroups[groupName];
importOriginal += typeof match !== 'undefined' ? match : '';
if (groupName !== 'url') {
importReplace += typeof match !== 'undefined' ? match : '';
} else {
importReplace += resolve(match, resourcePath);
}
}
if (importOriginal !== importReplace) {
css = css.replace(importOriginal, importReplace);
}
}
return css;
}
};