forked from smitt04/postcss-prefix-url
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (49 loc) · 1.38 KB
/
index.js
File metadata and controls
61 lines (49 loc) · 1.38 KB
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
'use strict';
var postcss = require('postcss');
var functions = require('postcss-functions');
var consistent = require('consistent');
var url = require('url');
module.exports = postcss.plugin('prefix', function (options) {
options = options || {};
var prefix = options.prefix || '';
var useUrl = options.useUrl || false;
var exclude = options.exclude || null;
if (Array.isArray(prefix)) {
if (prefix.length === 1) {
prefix = prefix[0];
} else if (prefix.length === 0) {
prefix = '';
}
}
var ring;
if (Array.isArray(prefix)) {
ring = new consistent({
members: prefix
});
}
var getPrefix = function (path) {
if (typeof prefix === 'string') {
return prefix;
}
return ring.get(path);
}
var formatUrl = function (path, includePrefix) {
includePrefix = typeof includePrefix !== 'undefined' ? includePrefix : true;
var sanitizedPath = path.replace(/['"]/g, '');
if ((exclude && exclude.test(path)) || /^([a-z]+:\/\/|\/\/)/i.test(path)) {
includePrefix = false;
}
var prefix = includePrefix ? getPrefix(sanitizedPath) : '';
return 'url('+url.resolve(prefix, sanitizedPath)+')';
};
return postcss().use(functions({
functions: {
cdn: function(path) {
return formatUrl(path);
},
url: function(path) {
return formatUrl(path, useUrl);
}
}
}));
});