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