-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
48 lines (43 loc) · 1.41 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
var path = require('path');
var fs = require('fs');
var pug = require('pug');
var debug = require('debug')('epch:cache');
module.exports = function(app, opts) {
var options = Object.assign({
pugExt: '.pug' // the leading dot is very important
}, opts);
if (options.force) {
app.enable('view cache');
}
app.on('mount', function() {
if ( ! app.enabled('view cache')) {
return;
}
var compileSettings = Object.assign({cache: true}, options.pugCompileOptions);
var compilePugFromDir = function(dir, subDir) {
var files = fs.readdirSync(dir);
files.forEach(function(file) {
var filepath = path.join(dir, file);
if (fs.statSync(filepath).isDirectory()) {
compilePugFromDir(filepath, path.join(subDir, file));
return;
}
if (path.extname(filepath) !== options.pugExt) {
debug(file + ' is not a .pug file!');
return;
}
pug.compileFile(filepath, compileSettings);
var fileKey = path.join(subDir, path.basename(filepath, options.pugExt));
var View = app.get('view');
app.cache[fileKey] = new View(fileKey, {
defaultEngine: app.get('view engine'),
root: app.get('views'),
engines: app.engines
});
debug('template compiled, put in cache (key): ' + fileKey);
});
};
compilePugFromDir(app.get('views'), '');
});
return app;
};