-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
125 lines (94 loc) · 3.47 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
var crypto = require('crypto');
var through2 = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var slash = require('slash');
var PluginError = gutil.PluginError;
module.exports = CacheBuster;
function CacheBuster(options) {
if (!(this instanceof CacheBuster)) {
return new CacheBuster(options);
}
this.checksumLength = (options && options.checksumLength) || 8;
this.random = (options && options.random) || false;
if (this.random) {
var shasum = crypto.createHash('sha1');
shasum.update(crypto.randomBytes(50).toString());
this.hash = shasum.digest('hex').substr(0, this.checksumLength);
}
this.pathFormatter = options && options.pathFormatter ? options.pathFormatter : function(dirname, basename, extname, checksum) {
return path.join(dirname, basename + '.' + checksum + extname);
};
this.mappings = {};
}
CacheBuster.prototype.getChecksum = function getChecksum(file) {
var hash = crypto.createHash('md5');
if (file.isNull()) {
return;
}
if (file.isStream()) {
file.pipe(hash);
hash.end();
}
if (file.isBuffer()) {
hash.end(file.contents);
}
return hash.read().toString('hex').substr(0, this.checksumLength);
}
CacheBuster.prototype.getBustedPath = function getBustedPath(file) {
var checksum = this.random ? this.hash : this.getChecksum(file);
if (!checksum) {
return file.path;
}
var extname = path.extname(file.path);
var basename = path.basename(file.path, extname);
var dirname = path.dirname(file.path);
return slash(this.pathFormatter(dirname, basename, extname, checksum));
};
CacheBuster.prototype.getRelativeMappings = function getRelativeMappings() {
var mappings = [];
for (var original in this.mappings) {
var cachebusted = this.mappings[original];
mappings.push({original: original, cachebusted: cachebusted});
}
return mappings;
};
CacheBuster.prototype.resources = function resources() {
var cachebuster = this;
return through2.obj(function transform(file, encoding, callback) {
var bustedPath = cachebuster.getBustedPath(file);
if (file.path === bustedPath) {
this.push(file);
return callback();
}
var original = slash(file.relative);
file.path = bustedPath;
cachebuster.mappings[original] = slash(file.relative);
this.push(file);
return callback();
});
};
CacheBuster.prototype.references = function references() {
var cachebuster = this;
return through2.obj(function transform(file, encoding, callback) {
if (file.isNull()) {
this.push(file);
return callback();
}
if (!file.isBuffer()) {
this.emit('error', new PluginError('gulp-cachebust', 'Non buffer files are not supported for reference rewrites.'));
this.push(file);
return callback();
}
var contents = file.contents.toString(encoding);
var mappings = cachebuster.getRelativeMappings(file.path);
for (var i=0; i < mappings.length; i++) {
var original = mappings[i].original;
var cachebusted = mappings[i].cachebusted;
contents = contents.replace(new RegExp('\\b' + original + '(?!\\.)\\b', 'g'), cachebusted);
}
file.contents = new Buffer(contents, encoding);
this.push(file);
return callback();
});
};