-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
45 lines (37 loc) · 1.15 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
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, '/');
}
return p;
}))
.toString();
};
module.exports = function(options) {
options = options || {};
var root = options.root || '.';
return through.obj(function(file, enc, cb) {
var css = rebaseUrls(file.contents.toString(), {
currentDir: path.dirname(file.path),
root: path.join(file.cwd, root)
});
file.contents = new Buffer(css);
this.push(file);
cb();
});
};