-
Notifications
You must be signed in to change notification settings - Fork 4
/
handlebars.js
166 lines (140 loc) · 5.17 KB
/
handlebars.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
var _ = require('lodash'),
Async = require('async'),
Handlebars = require('handlebars'),
Visitor = require('../handlebars-visitor'),
LoaderUtils = require('loader-utils'),
Path = require('path'),
SourceMap = require('source-map');
module.exports = function(content) {
var callback = this.async();
this.cacheable(true);
var query = LoaderUtils.parseQuery(this.query),
helpersDir = query.helpersDir || './src/lib/helpers/',
templateExtension = query.extension || '.hbs',
externalKnown = {};
(query.knownHelpers || []).forEach(function(helper) {
externalKnown[helper] = true;
});
var componentHelpers = {},
componentPartials = {};
_.each(this.options.components, function(component) {
/* istanbul ignore else */
if (component.handlebars) {
_.extend(componentHelpers, component.handlebars.helper);
_.extend(componentPartials, component.handlebars.partial);
}
});
var self = this,
ast = Handlebars.parse(content),
scanner = new ImportScanner(),
knownHelpers = {},
lookupErrors = {};
// Do the first phase of the parse
scanner.accept(ast);
var prelude = [
'var Handlebars = require("circus-handlebars/lib/loaders/client/handlebars-shim");\n'
];
function lookupPartials(callback) {
Async.forEach(scanner.partials, function(partial, callback) {
var request = partial.request,
partialName = partial.name.name;
if (componentPartials[partialName]) {
prelude.push('require(' + JSON.stringify(componentPartials[partialName]) + ');\n');
return callback();
}
self.resolve(self.context, request + templateExtension, function(err, result) {
if (err || !result) {
self.emitWarning('Unable to resolve partial "' + request + '": ' + err);
} else {
// Attempt to generate a unique partial name
var partialName = Path.relative(self._compiler.context, result);
partialName = Path.dirname(partialName) + '/' + Path.basename(partialName, templateExtension);
prelude.push('require("' + require.resolve('./partial') + '?name=' + partialName + '!' + result + '");\n');
// Update the AST for the unique name that we generated
partial.name.name = partialName;
}
callback();
});
},
callback);
}
function lookupHelpers(callback) {
Async.forEach(_.uniq(scanner.potentialHelpers), function(helper, callback) {
if (knownHelpers[helper] || externalKnown[helper] || lookupErrors[helper]) {
callback();
} else if (componentHelpers[helper]) {
knownHelpers[helper] = true;
prelude.push('require(' + JSON.stringify(componentHelpers[helper]) + ');\n');
callback();
} else {
self.resolve(self._compiler.context, helpersDir + helper, function(err, result) {
if (!err) {
knownHelpers[helper] = true;
prelude.push('require("' + require.resolve('./helper') + '?name=' + helper + '!' + result + '");\n');
} else {
lookupErrors[helper] = err;
}
callback();
});
}
},
callback);
}
Async.parallel([
lookupPartials,
lookupHelpers
],
function() {
var name = Path.relative(self.options.context, self.resource),
template;
try {
// Render the AST to the final script
template = Handlebars.precompile(ast, {
// srcName: name,
// destName: name + '.js',
knownHelpersOnly: true,
knownHelpers: _.extend(knownHelpers, externalKnown)
});
} catch (err) {
// A bit hacky, but the mis match case simply shows less information, so we can live
// with it for now.
// console.log(err, Object.keys(err));
var msg = err.details || err.message,
toThrow = err;
/*istanbul ignore else */
if (/knownHelpersOnly.*unknown helper (\S+)/.exec(msg)) {
var lookupError = lookupErrors[RegExp.$1];
msg = 'Failed to lookup helper: ' + RegExp.$1 + '\n' + lookupError.details;
toThrow = new Error(msg);
}
return callback(toThrow);
}
var sourceNode = new SourceMap.SourceNode(1, 0, name, template);
sourceNode.setSourceContent(name, content);
var concatSrc = new SourceMap.SourceNode();
concatSrc.add(prelude);
concatSrc.add([
'\nmodule.exports = Handlebars.template(',
sourceNode,
')'
]);
var result = concatSrc.toStringWithSourceMap();
callback(undefined, result.code, result.map.toString());
});
};
function ImportScanner() {
this.partials = [];
this.potentialHelpers = [];
}
ImportScanner.prototype = new Visitor();
ImportScanner.prototype.sexpr = function(sexpr) {
var id = sexpr.id;
if (id.isSimple) {
this.potentialHelpers.push(id.original);
}
Visitor.prototype.sexpr.call(this, sexpr);
};
ImportScanner.prototype.partial = function(partial) {
this.partials.push({request: partial.partialName.name, name: partial.partialName});
Visitor.prototype.partial.call(this, partial);
};