forked from kimjoar/gulp-css-rebase-urls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (42 loc) · 1.35 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
var rework = require('rework');
var path = require('path');
var through = require('through2');
var validator = require('validator');
var isAbsolute = function(p) {
var normal = path.normalize(p);
var absolute = path.resolve(p);
return normal == absolute;
};
var rebaseUrls = function(css, options) {
return rework(css)
.use(rework.url(function(url){
if (isAbsolute(url) && validator.isURL(url)) {
return url;
}
var absolutePath = path.join(options.currentDir, url)
var p = path.relative(options.root, absolutePath);
if (process.platform === 'win32') {
p = p.replace(/\\/g, '/');
}
if (options.convertToAbsolute) {
p = "/" + p;
}
return p;
}))
.toString();
};
module.exports = function(options) {
options = options || {};
var root = options.root || '.';
var convertToAbsolute = options.convertToAbsolute || false;
return through.obj(function(file, enc, cb) {
var css = rebaseUrls(file.contents.toString(), {
currentDir: path.dirname(file.path),
root: path.join(file.cwd, root),
convertToAbsolute: convertToAbsolute
});
file.contents = new Buffer(css);
this.push(file);
cb();
});
};