forked from antaranian/vibrissae
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (83 loc) · 2.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Load dependencies.
*/
var fs = require('fs'),
path = require('path');
var htmlmin = require('html-minifier');
/**
* Load `daub`.
*/
var daub = require('./lib/daub.js');
var read = require('./lib/core/read.js');
/**
* Options for minification.
*/
var minOpts = {
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
};
/**
* Setup cache, initially empty.
*/
var cache = {};
var production = process.env.NODE_ENV == 'production';
/**
* Expose `daub` itself.
*/
module.exports = daub;
/**
* Expose low level `readFile` method.
*/
daub.readFile = read;
/**
* Expose `renderFile` method,
* aliased for Express.
*
* @param {String} path
* @param {Object} options [optional]
* @param {Function} fn [optional]
* @async
* @api public
*/
daub.renderFile =
daub.__express = function(root, options, fn) {
if (typeof options == 'function')
fn = options,
options = {};
if (typeof fn !== 'function')
return void 0;
compile(root, options, function(err, compiled){
if (err) return fn(err);
fn(null, compiled(options));
});
};
/**
* Compile file at given path to templating function expression,
* optionally serving from `cache` if already compiled before.
*
* @param {String} path
* @param {Object} options
* @param {Function} fn
* @async
* @api private
*/
function compile(path, options, fn) {
var compiled = cache[path];
// cached
if (options.cache != false && compiled)
return fn(null, compiled);
// read with partials
read(path, options, function(err, markup){
if (err) return fn(err);
// minify on demand
if (options.compress || production)
markup = htmlmin.minify(markup, minOpts);
compiled = daub.compile(markup, path);
if (options.cache)
cache[path] = compiled;
fn(null, compiled);
});
}