-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (58 loc) · 1.88 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
var path = require('path');
var fs = require('fs');
var ejs = require('ejs');
function ejsBuilder(options) {
this.options = options || {};
}
ejsBuilder.prototype.apply = function(compiler) {
var files = this.options.files || [];
var root = this.options.root;
compiler.plugin('emit', function(compilation, callback) {
if (typeof root == 'undefined') {
root = path.join(__dirname,'../../');
}
files.forEach(function(file) {
var filename = '';
var compileOptions = {
sourceName: '',
sourceDir: root,
targetName: '',
targetDir: root,
parameters: file.parameters || {},
encoding: file.encoding || 'utf8',
};
if (typeof file == 'string') {
compileOptions.sourceName = file;
compileOptions.sourceDir = root;
} else if (typeof file == 'object') {
if (typeof file.source != 'undefined') {
compileOptions.sourceName = file.source.name || '';
compileOptions.sourceDir = file.source.dir || root;
}
if (typeof file.target != 'undefined') {
compileOptions.targetName = file.target.name || '';
compileOptions.targetDir = file.target.dir || '';
}
}
if (compileOptions.sourceName.length > 0) {
var sourceFilePath=path.join(compileOptions.sourceDir, compileOptions.sourceName);
var sourceFile = fs.readFileSync(sourceFilePath, { encoding: compileOptions.encoding});
compileOptions.parameters.filename=sourceFilePath;
var targetFile = ejs.render(sourceFile, compileOptions.parameters);
var targetFileName = (compileOptions.targetName.length > 0)
? path.join(compileOptions.targetDir, compileOptions.targetName)
: compileOptions.sourceName.replace('.ejs','.html');
compilation.assets[targetFileName] = {
source: function() {
return targetFile;
},
size: function () {
return targetFile.length;
}
};
}
});
callback();
});
};
module.exports = ejsBuilder;