-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
69 lines (59 loc) · 1.95 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
var cons = require('consolidate');
var utils = require('loader-utils');
var path = require('path');
var fs = require('fs');
module.exports = function(content) {
this.cacheable && this.cacheable();
var callback = this.async();
var opt = utils.getOptions(this);
function exportContent(content) {
if (opt.raw) {
callback(null, content);
} else {
callback(null, "module.exports = " + JSON.stringify(content));
}
}
// with no engine given, use the file extension as engine
if(!opt.engine) {
opt.engine = path.extname(this.request).substr(1).toLowerCase();
}
if(!cons[opt.engine]) {
throw new Error("Engine '"+ opt.engine +"' isn't available in Consolidate.js");
}
// for relative includes
opt.filename = this.resourcePath;
opt.dirname = path.dirname(this.resourcePath);
const self = this;
if(opt.dataFiles instanceof Array) {
opt.dataFiles.reverse().forEach(function(file) {
file = path.join(opt.dirname, file);
if(!fs.existsSync(file)) { // if the given name doesn't exist, check if it needs an extension
file += '.json';
if(!fs.existsSync(file)) {
throw new Error("Data file '"+ file +"' does not exist");
}
}
opt = Object.assign(JSON.parse(fs.readFileSync(file)), opt); // ensure that opt takes precedence
self.addDependency(file);
})
delete opt.dataFiles;
}
if(opt.partialsFiles instanceof Object) {
Object.keys(opt.partialsFiles).forEach(function(key) {
var file = path.join(opt.dirname, opt.partialsFiles[key]);
if(!fs.existsSync(file)) {
throw new Error("Partials file '"+ file +"' does not exist");
}
opt.partials = opt.partials || {};
opt.partials[key] = fs.readFileSync(file).toString();
self.addDependency(file);
})
delete opt.extraFiles;
}
cons[opt.engine].render(content, opt, function(err, html) {
if(err) {
throw err;
}
exportContent(html);
});
};