/
index.js
279 lines (209 loc) · 6.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
const fs = require('fs');
const path = require('path');
const requireGlob = require('require-glob');
const toString = Object.prototype.toString;
const ESCAPE_CHARACTERS = /[-/\\^$*+?.()|[\]{}]/g;
const NON_WORD_CHARACTERS = /\W+/g;
const PATH_SEPARATOR = '/';
const PATH_SEPARATORS = /[\\/]/g;
const WHITESPACE_CHARACTERS = /\s+/g;
const WORD_SEPARATOR = '-';
const TYPE_FUNCTION = 'fun';
const TYPE_OBJECT = 'obj';
// Utilities
function escapeRx(str) {
return str.replace(ESCAPE_CHARACTERS, '\\$&');
}
function getTypeOf(value) {
return toString
.call(value)
.substr(8, 3)
.toLowerCase();
}
function hookRequire(handlebars, extensions) {
// istanbul ignore next
extensions = extensions || [];
let originalHooks; // eslint-disable-line prefer-const
function compileFile(module, filename) {
const templateString = fs.readFileSync(filename, 'utf8');
module.exports = handlebars.compile(templateString);
}
function cacheHook(extension) {
const originalHook = require.extensions[extension];
require.extensions[extension] = compileFile;
return originalHook;
}
function uncacheHook(extension) {
require.extensions[extension] = originalHooks[extension];
}
function unhookRequire() {
extensions.forEach(uncacheHook);
}
// Hook
originalHooks = extensions.map(cacheHook);
// Unhook
return unhookRequire;
}
// Map Reduce
function keygenPartial(options, file) {
const resolvedFilePath = fs.realpathSync(file.path);
const resolvedFileBase = fs.realpathSync(file.base);
const fullPath = resolvedFilePath.replace(PATH_SEPARATORS, PATH_SEPARATOR);
const basePath =
resolvedFileBase.replace(PATH_SEPARATORS, PATH_SEPARATOR) +
PATH_SEPARATOR;
const shortPath = fullPath.replace(
new RegExp('^' + escapeRx(basePath), 'i'),
''
);
const extension = path.extname(shortPath);
return shortPath
.substr(0, shortPath.length - extension.length)
.replace(WHITESPACE_CHARACTERS, WORD_SEPARATOR);
}
function keygenHelper(options, file) {
return keygenPartial(options, file).replace(
NON_WORD_CHARACTERS,
WORD_SEPARATOR
);
}
function keygenDecorator(options, file) {
return keygenHelper(options, file);
}
function reducer(options, obj, fileObj) {
let value = fileObj.exports;
if (!value) {
return obj;
}
if (getTypeOf(value.register) === TYPE_FUNCTION) {
value = value.register(options.handlebars, options);
if (getTypeOf(value) === TYPE_OBJECT) {
return Object.assign(obj, value);
}
return obj;
}
if (getTypeOf(value) === TYPE_OBJECT) {
return Object.assign(obj, value);
}
obj[options.keygen(fileObj)] = value;
return obj;
}
function resolveValue(options, value) {
if (!value) {
return {};
}
if (getTypeOf(value) === TYPE_FUNCTION) {
value = value(options.handlebars, options);
if (getTypeOf(value) === TYPE_OBJECT) {
return value;
}
return {};
}
if (getTypeOf(value) === TYPE_OBJECT) {
return reducer(options, {}, {exports: value});
}
return requireGlob.sync(value, options);
}
// Wax
function HandlebarsWax(handlebars, options) {
const defaults = {
handlebars,
bustCache: true,
cwd: process.cwd(),
compileOptions: null,
extensions: ['.handlebars', '.hbs', '.html'],
templateOptions: null,
parsePartialName: keygenPartial,
parseHelperName: keygenHelper,
parseDecoratorName: keygenDecorator,
parseDataName: null
};
this.handlebars = handlebars;
this.config = Object.assign(defaults, options);
this.context = Object.create(null);
this.engine = this.engine.bind(this);
}
HandlebarsWax.prototype.partials = function (partials, options) {
options = Object.assign({}, this.config, options);
options.keygen = options.parsePartialName;
options.reducer = options.reducer || reducer;
const unhookRequire = hookRequire(options.handlebars, options.extensions);
options.handlebars.registerPartial(resolveValue(options, partials));
unhookRequire();
return this;
};
HandlebarsWax.prototype.helpers = function (helpers, options) {
options = Object.assign({}, this.config, options);
options.keygen = options.parseHelperName;
options.reducer = options.reducer || reducer;
options.handlebars.registerHelper(resolveValue(options, helpers));
return this;
};
HandlebarsWax.prototype.decorators = function (decorators, options) {
options = Object.assign({}, this.config, options);
options.keygen = options.parseDecoratorName;
options.reducer = options.reducer || reducer;
options.handlebars.registerDecorator(resolveValue(options, decorators));
return this;
};
HandlebarsWax.prototype.data = function (data, options) {
options = Object.assign({}, this.config, options);
options.keygen = options.parseDataName;
Object.assign(this.context, resolveValue(options, data));
return this;
};
HandlebarsWax.prototype.compile = function (template, compileOptions) {
const config = this.config;
const context = this.context;
compileOptions = Object.assign({}, config.compileOptions, compileOptions);
if (getTypeOf(template) !== TYPE_FUNCTION) {
template = this.handlebars.compile(template, compileOptions);
}
return function (data, templateOptions) {
templateOptions = Object.assign(
{},
config.templateOptions,
templateOptions
);
templateOptions.data = Object.assign({}, templateOptions.data);
// {{@global.foo}} and {{@global._parent.foo}}
templateOptions.data.global = Object.assign(
{_parent: context},
templateOptions.data.global || context
);
// {{@local.foo}} and {{@local._parent.foo}}
templateOptions.data.local = Object.assign(
{_parent: context},
templateOptions.data.local || data
);
// {{foo}} and {{_parent.foo}}
return template(
Object.assign({_parent: context}, context, data),
templateOptions
);
};
};
HandlebarsWax.prototype.engine = function (file, data, callback) {
const config = this.config;
const cache = this.cache || (this.cache = {});
try {
let template = cache[file];
// istanbul ignore else
if (!template || config.bustCache) {
template = this.compile(fs.readFileSync(file, 'utf8'));
cache[file] = template;
}
callback(null, template(data));
} catch (err) {
// istanbul ignore next
callback(err);
}
return this;
};
// API
function handlebarsWax(handlebars, config) {
return new HandlebarsWax(handlebars, config);
}
module.exports = handlebarsWax;
module.exports.HandlebarsWax = HandlebarsWax;