-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (44 loc) · 1.53 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
'use strict';
var through = require('through2');
var path = require('path');
/**
* 把ejs转成js
* 目的是为了可以和js一起打包,避免额外的请求
* 会把文件名作为ejs的名字
* @param {[type]} opt [description]
* @return {[type]} [description]
*/
module.exports = function(opt) {
opt = opt || {minify: false};
function doSomething(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(createError(file, 'Streaming not supported'));
}
//do something
var fileName = path.basename(file.path);
fileName = fileName.replace(".", "");
var newContents = 'var ' + fileName + ' = new EJS({text:';
var contents = file.contents.toString();
contents = contents.replace(/"+/g, '\\"');
contents = contents.replace(/'+/g, "\\'");
//是否压缩文件
if(opt.minify === true){
newContents += '"' + contents.replace(/\s+/g, " ") + '"';
}else{
var texts = [];
contents = contents.split(/\r?\n/ig);
contents.forEach(function(line, index){
texts[index] = '"' + line + '"\n';
});
newContents += texts.join('+');
}
newContents += '});'
file.contents = new Buffer(newContents);
file.path = file.path.replace(".ejs", ".js");
callback(null, file);
}
return through.obj(doSomething);
};