-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulp-cache.js
executable file
·49 lines (34 loc) · 1.07 KB
/
gulp-cache.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
var through = require('through2'),
jsesc = require('jsesc'),
gutil = require('gulp-util'),
md5 = require('md5'),
File = gutil.File;
/**
*
* @param fileName
* @param removeFromPath
*/
module.exports = function (fileName, removeFromPath) {
var codeLines = [],
varName = 'cache';
function wrap(code) {
var begin = ['\'use strict\';', 'define([], function () {', 'var ' + varName + ' = {};'].join('\n'),
end = ['return ' + varName, '});'].join('\n');
return [begin, code, end].join('\n');
}
function bufferContents(file, enc, cb) {
var path = file.path.replace(removeFromPath, ''),
data = jsesc(file.contents.toString('utf8'));
codeLines.push(varName + '["' + md5(path) + '"] = \'' + data + '\';');
cb();
}
function endContents(cb) {
var file = new File({
path: fileName,
contents: new Buffer(wrap(codeLines.join('\n')))
});
this.push(file);
cb();
}
return through.obj(bufferContents, endContents);
};