forked from furzeface/gulp-cache-bust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (47 loc) · 1.33 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
'use strict';
var path = require('path'),
fs = require('graceful-fs'),
gutil = require('gulp-util'),
map = require('map-stream'),
tempWrite = require('temp-write'),
cachebust = require('cachebust');
module.exports = function (options) {
if(!options){
options = {};
}
return map(function (file, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new gutil.PluginError('gulp-cachebust', 'Streaming not supported'));
}
if(!options.basePath){
options.basePath = path.dirname(path.resolve(file.path))+'/';
}
tempWrite(file.contents, path.extname(file.path))
.then(function (tempFile, err) {
if (err) {
return cb(new gutil.PluginError('gulp-cachebust', err));
}
fs.stat(tempFile, function (err, stats) {
if (err) {
return cb(new gutil.PluginError('gulp-cachebust', err));
}
options = options || {};
fs.readFile(tempFile, { encoding : 'UTF-8'}, function(err, data) {
if (err) {
return cb(new gutil.PluginError('gulp-cachebust', err));
}
// Call the Node module
var processedContents = cachebust.busted(data, options);
if (options.showLog) {
gutil.log('gulp-cachebust:', gutil.colors.green('✔ ') + file.relative);
}
file.contents = new Buffer(processedContents);
cb(null, file);
});
});
});
});
};