-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.23 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
'use strict'
const Transform = require('readable-stream/transform')
global.Buffer = require('buffer').Buffer
function appendQueryString (contents, type, query) {
const search = new RegExp('\\.' + type + '"', 'g')
const replacement = '.' + type + '?' + query + '"'
return global.Buffer.from(String(contents).replace(search, replacement))
}
function createRandomString (length) {
let str = ''
while (str.length < length) {
str += Math.random().toString(36).substr(2)
}
return str.substr(0, length)
}
module.exports = function (options = {}) {
return new Transform({
objectMode: true,
transform: function appendQueryStringTransform (file, enc, cb) {
if (file.isNull()) {
return cb(null, file)
}
const isCSS = 'css' in options ? options.css : true
const isJS = 'js' in options ? options.js : true
const length = 'length' in options ? options.length : 8
if (file.isBuffer()) {
const query = createRandomString(length)
if (isCSS) {
file.contents = appendQueryString(file.contents, 'css', query)
}
if (isJS) {
file.contents = appendQueryString(file.contents, 'js', query)
}
}
return cb(null, file)
}
})
}