-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
124 lines (103 loc) · 3.66 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
var PluginError = require("plugin-error");
var crypto = require("crypto");
var fs = require("fs");
var through = require("through2");
const PLUGIN_NAME = "gulp-wp-cache-bust";
function md5(str) {
var hash = crypto.createHash("md5");
hash.update(fs.readFileSync(str));
return hash.digest("hex");
}
function removePreviousVersioning(filePath) {
// Remove any previous cache busting references
return filePath.lastIndexOf("?v=") > 0
? filePath.substring(0, filePath.lastIndexOf("?v=")) + "'"
: filePath;
}
function gulpWPCacheBust(options) {
options = options || {};
if (!options.rootPath) {
options.rootPath = "";
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, "Streaming not supported!");
}
if (file.isBuffer()) {
file.contents = Buffer.from(String(file.contents));
var regex = /(define\(([^;]+)\))/g;
var m;
var buffer = Buffer.from(String(file.contents));
while ((m = regex.exec(buffer)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if (m[2]) {
var ogDefinition = m[2];
var nonVersionedDefinition = removePreviousVersioning(ogDefinition);
var params = nonVersionedDefinition.split(",");
if (params[1]) {
filePath = params[1].trim();
// Only check local files
if (!/(http:\/\/|https:\/\/)/.test(filePath)) {
// remove ticks/quotes
var r = /(['"]+)/g;
cleanFilePath = filePath.replace(r, "");
// if using get_template_directory_uri or get_stylesheet_directory_uri
// swap for themeFolder
var r = /(get_template_directory_uri\(\)[ .]+)/g;
var j = /(get_stylesheet_directory_uri\(\)[ .]+)/g;
if (r.test(cleanFilePath)) {
if (options.themeFolder) {
cleanFilePath = cleanFilePath
.replace(r, options.themeFolder)
.replace(/\/\//g, "/");
} else {
throw new PluginError(
PLUGIN_NAME,
"Theme folder is not defined."
);
}
} else if (j.test(cleanFilePath)) {
if (options.themeFolder) {
cleanFilePath = cleanFilePath
.replace(j, options.themeFolder)
.replace(/\/\//g, "/");
} else {
throw new PluginError(
PLUGIN_NAME,
"Theme folder is not defined."
);
}
} else if (cleanFilePath[0] === "/") {
// Absolute path
cleanFilePath = cleanFilePath.replace("/", options.rootPath);
} else {
return;
}
var hashCSS = md5(cleanFilePath);
var slashSplits = cleanFilePath.split("/");
var fileName = slashSplits[slashSplits.length - 1];
var versionedFileName = fileName + "?v=" + hashCSS;
var newDefinition = nonVersionedDefinition.replace(
fileName,
versionedFileName
);
file.contents = Buffer.from(
String(file.contents).replace(ogDefinition, newDefinition)
);
}
}
}
}
this.push(file);
}
cb(null, file);
});
}
module.exports = gulpWPCacheBust;