-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (44 loc) · 1.25 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
var postcss = require('postcss')
var pathReplacePlugin = postcss.plugin('postcss-path-replace', pathReplaceProcess)
module.exports = pathReplacePlugin
/**
* plugin process handler.
* @param {object} options
*/
function pathReplaceProcess(options) {
var publicPath = options.publicPath || ''
var matched = options.matched
var mode = options.mode || 'replace'
var exec = options.exec
// want a regexp to replace all.
if (typeof matched === 'string') {
var escapeMatched = escapeRegExp(matched)
matched = new RegExp(escapeMatched, 'g')
}
return function (root) {
root.walkDecls(function (decl) {
if (/url\(['"]?.*?['"]?\)/.test(decl.value)) {
decl.value = decl.value.replace(matched, function() {
var args = [].slice.call(arguments)
if (exec && typeof exec === 'function') {
return exec.apply(null, args)
}
if (mode === 'replace') {
return publicPath
}
if (mode === 'append') {
return publicPath + args[0]
}
return publicPath
})
}
})
}
}
/**
* escape special char, like '.', '/'
* @param {string} str
*/
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')
}