-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (60 loc) · 1.85 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
'use strict';
var path = require('path');
var through = require('through2');
var unquote = require('unquote');
var extend = require('util')._extend;
var gutil = require('gulp-util');
var PLUGIN_NAME ='gulp-irebase';
module.exports = function(opts) {
opts = opts || {};
var defaults= {
srcDir: null,
destDir: './',
search:/url\((((?!http(s?)\:).)*?)\)/ig,
replace:function(file){
return function(url,src){
return 'url("'+path.relative(opts.destDir,getDir(file,unquote(src)))+'")';
};
},
rebaseUrls: true,
removeMapUrls: true,
mapRegexp: /\/\*#\s*sourceMappingURL\s*.*?\*\//ig
}
opts= extend(defaults,opts);
function getDir(file,src){
var dir=opts.srcDir;
if(typeof opts.srcDir=='function'){
dir=opts.srcDir(file,opts);
}else if(opts.srcDir==null){
dir=path.dirname(file.path);
}
return path.join(dir, src);
}
function _rebase(file){
var data = String(file.contents);
if(opts.rebaseUrls){
data=data.replace(opts.search,opts.replace(file))
}
if(opts.removeMapUrls){
data=data.replace(opts.mapRegexp,'');
}
return data;
}
function _cleanMapUrl(data)
{
if(opts.removeMapUrl==true){
return data.replace(opts.mapRegexp,'');
}
return data;
}
return through.obj(function(file, encoding, callback) {
if(file.isNull()) return callback(null,file);
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streams not supported!'));
}
if(file.isBuffer()){
file.contents=new Buffer(_rebase(file));
return callback(null,file);
}
});
};