forked from kimjoar/gulp-css-rebase-urls
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
76 lines (61 loc) · 1.82 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
var path = require('path');
var rework = require('rework');
var reworkUrl = require('rework-plugin-url');
var through = require('through2');
var validator = require('validator');
var isAbsolute = function (p) {
var normal = path.normalize(p);
var absolute = path.resolve(p);
if (process.platform === 'win32') {
absolute = absolute.substr(2);
}
return normal === absolute;
};
var isUrl = function (url) {
if (!url) { return false; }
// protocol relative URLs
if (url.indexOf('//') === 0 && validator.isURL(url, { allow_protocol_relative_urls: true })) {
return true;
}
return validator.isURL(url, { require_protocol: true });
}
var rebaseUrls = function (css, options) {
return rework(css)
.use(reworkUrl(function (url) {
if (isAbsolute(url) || isUrl(url) || /^(data:.*;.*,)/.test(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 || '.';
var reroot = options.reroot || '';
return through.obj(function (file, enc, cb) {
var fileDir = path.dirname(file.path);
// Allows placing the processed CSS in a different root directory while
// leaving image resources alone.
if (reroot) {
var rerootPath = path.join(
path.relative(root, reroot),
path.relative(root, fileDir)
);
} else {
rerootPath = '';
}
var css = rebaseUrls(file.contents.toString(), {
currentDir: fileDir,
root: path.join(file.cwd, root, rerootPath)
});
file.contents = new Buffer(css);
this.push(file);
cb();
});
};