This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
74 lines (54 loc) · 1.76 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
'use strict';
var path = require('path');
var join = path.join;
var fs = require('fs');
var exists = fs.existsSync;
var read = fs.readFileSync;
var defaults = require('defaults');
var Handlebars = require('handlebars');
var partials = [];
var runtime = runtimeFile();
exports = module.exports = function(opts) {
opts = defaults(opts, {
extname: 'hbs',
partialRegex: /^_/
});
partials = [];
return function builderHandlebars(file, done) {
if (file.extension !== opts.extname) return done();
file.read(function(err, string) {
if (err) return done(err);
var filename = path.basename(file.path, '.' + file.extension);
var compiled = Handlebars.precompile(string);
var output = 'Handlebars.template(' + compiled + ')';
if (opts.partialRegex.test(filename)) {
// Register the partial as: moduleName/_fileName
output = 'Handlebars.registerPartial("'
+ file.branch.name + '/' + file.path.replace('.' + file.extension, '')
+ '", ' + output + ');\n';
partials.push(file.branch.canonical + '/' + file.path);
} else {
output = 'module.exports = ' + output;
}
file.string = output;
done();
});
};
};
exports.includeRuntime = function includeRuntime() {
var string = runtime;
string += 'this.Handlebars = window.Handlebars = Handlebars;\n';
if (!partials.length) return string;
// Auto requires partials.
partials.forEach(function(p) {
string += 'require("' + p + '");\n';
});
return string;
};
function runtimeFile() {
var runtimePath = join(__dirname, 'node_modules/handlebars/dist/handlebars.runtime.js');
if (!exists(runtimePath)) {
runtimePath = join(__dirname, 'handlebars.runtime.js');
}
return read(runtimePath, 'utf-8');
}