-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (83 loc) · 3.39 KB
/
index.js
File metadata and controls
113 lines (83 loc) · 3.39 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
var path = require('path'),
fs = require('graceful-fs'),
gutil = require('gulp-util'),
map = require('map-stream'),
tempWrite = require('temp-write'),
cheerio = require('cheerio');
function loadAttribute(content) {
if (content.name.toLowerCase() === 'link') {
return content.attribs.href;
}
if (content.name.toLowerCase() === 'script') {
return content.attribs.src;
}
throw "No content awaited in this step of process";
}
var Busted = function(fileContents, options){
var self = this, $ = cheerio.load(fileContents);
self.timestamp = function(fileContents, originalAttrValue, options) {
var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0];
return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?t=' + options.currentTimestamp);
};
self.customTag = function(fileContents, originalAttrValue, options) {
var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0];
return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?t=' + options.customTag);
};
options = {
basePath : options.basePath || "",
type : options.type || "timestamp",
currentTimestamp : new Date().getTime(),
customTag: options.type
};
var protocolRegEx = /^http(s)?/, elements = $('script[src], link[rel=stylesheet][href]');
for (var i = 0, len = elements.length; i < len; i++) {
var originalAttrValue = loadAttribute(elements[i]);
// Test for http(s) and don't cache bust if (assumed) served from CDN
if (!protocolRegEx.test(originalAttrValue)) {
if (options.type === "timestamp") {
fileContents = self[options.type](fileContents, originalAttrValue, options);
}
else {
fileContents = self.customTag(fileContents, originalAttrValue, options);
}
}
}
return fileContents;
};
var cachebust = {
busted: Busted
};
module.exports = function (options) {
return map(function (file, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new gutil.PluginError('gulp-cache-refresh', 'Streaming not supported'));
}
tempWrite(file.contents, path.extname(file.path), function (err, tempFile) {
if (err) {
return cb(new gutil.PluginError('gulp-cache-refresh', err));
}
fs.stat(tempFile, function (err, stats) {
if (err) {
return cb(new gutil.PluginError('gulp-cache-refresh', err));
}
options = options || {};
fs.readFile(tempFile, { encoding : 'UTF-8'}, function(err, data) {
if (err) {
return cb(new gutil.PluginError('gulp-cache-refresh', err));
}
// Call the Node module
var processedContents = cachebust.busted(data, options);
if (options.showLog) {
gutil.log('gulp-cache-refresh:', gutil.colors.green('✔ ') + file.relative);
}
file.contents = new Buffer(processedContents);
cb(null, file);
});
});
});
});
};