-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
100 lines (79 loc) · 2.83 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
var Handlebars = require('handlebars');
var HbsRuntime = require('./runtime');
var loaderUtils = require('loader-utils');
var path = require('path');
// Parsers
var attributeParser = require('./lib/attributeParser');
var macroParser = require('./lib/macroParser');
// Helpers
var _extend = function(obj, from) {
for (var key in from) {
if (!from.hasOwnProperty(key)) continue;
obj[key] = from[key];
}
return obj;
};
// Extendable arguments
var macros = _extend({}, require('./lib/macros'));
module.exports = function(content) {
if (this.cacheable) this.cacheable();
var callback = this.async();
// Default arguments
var root,
parseMacros = true,
attributes = ['img:src'],
parseDynamicRoutes = false,
runtimePath = require.resolve('handlebars/runtime').replace(/\\/g, '/');
// Parse arguments
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query);
if (typeof(query) === 'object') {
if (query.attributes !== undefined) {
attributes = Array.isArray(query.attributes) ? query.attributes : [];
}
root = query.root;
if (query.parseMacros !== undefined) {
parseMacros = !!query.parseMacros;
}
// Prepend a html comment with the filename in it
if (query.prependFilenameComment) {
var filenameRelative = path.relative(query.prependFilenameComment, this.resource);
content = '\n<!-- ' + filenameRelative + ' -->\n' + content;
}
// Check if dynamic routes must be parsed
if (query.parseDynamicRoutes !== undefined) {
parseDynamicRoutes = !!query.parseDynamicRoutes;
}
if (query.runtimePath) {
runtimePath = query.runtimePath;
}
}
// Include additional macros
if (this.options && this.options.macros instanceof Object) {
_extend(macros, this.options.macros);
}
// Parser contexts
var macrosContext, attributesContext;
// Parse macros
if (parseMacros) {
macrosContext = macroParser(content, function (macro) {
return macros[macro] !== undefined && typeof(macros[macro]) === 'function';
}, 'MACRO');
content = macrosContext.replaceMatches(content);
}
// Parse attributes
attributesContext = attributeParser(content, function (tag, attr) {
return attributes.indexOf(tag + ':' + attr) !== -1;
}, 'ATTRIBUTE', root, parseDynamicRoutes);
content = attributesContext.replaceMatches(content);
// Compile template
var source = Handlebars.precompile(content, query);
// Resolve macros
if (parseMacros) {
source = macrosContext.resolveMacros(source, macros);
}
// Resolve attributes
source = attributesContext.resolveAttributes(source);
callback(null, 'var Handlebars = require(\'' + runtimePath + '\');\n' +
'module.exports = (Handlebars[\'default\'] || Handlebars).template(' + source + ');');
};
module.exports.Handlebars = HbsRuntime;