forked from brunch/handlebars-brunch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (63 loc) · 2.26 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
var handlebars = require('handlebars');
var umd = require('umd-wrapper');
var sysPath = require('path');
function HandlebarsCompiler(cfg) {
if (cfg == null) cfg = {};
this.optimize = cfg.optimize;
var config = cfg.plugins && cfg.plugins.handlebars;
if (config) {
var overrides = config.overrides;
if (typeof overrides === 'function') overrides(handlebars);
this.namespace = config.namespace;
this.pathReplace = config.pathReplace || this.pathReplace;
if (config.include) this.includeSettings = config.include;
}
this.setInclude();
}
HandlebarsCompiler.prototype.setInclude = function() {
var include = this.includeSettings || {};
if (include.enabled === false) {
delete HandlebarsCompiler.prototype.include;
return;
}
var includeFile = 'handlebars';
if (include.runtime || include.runtime == null) includeFile += '.runtime';
if (include.amd) includeFile += '.amd';
if (this.optimize) includeFile += '.min';
includeFile += '.js';
HandlebarsCompiler.prototype.include = [
sysPath.join(__dirname, 'dist', includeFile),
sysPath.join(__dirname, 'ns.js')
];
};
HandlebarsCompiler.prototype.brunchPlugin = true;
HandlebarsCompiler.prototype.type = 'template';
HandlebarsCompiler.prototype.extension = 'hbs';
HandlebarsCompiler.prototype.pattern = /\.(?:hbs|handlebars)$/;
HandlebarsCompiler.prototype.pathReplace = /^.*templates\//;
HandlebarsCompiler.prototype.compile = function(data, path, callback) {
if (this.optimize) {
data = data.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
data = data.replace(/^[\r\n]+/, '').replace(/[\r\n]*$/, '\n');
}
var error, key, ns, result, source, header;
try {
source = "Handlebars.template(" + (handlebars.precompile(data)) + ")";
if (typeof this.namespace === 'function') {
ns = this.namespace(path);
} else {
ns = this.namespace;
}
if (ns) {
key = ns + '.' + path.replace(/\\/g,'/').replace(this.pathReplace, '').replace(/\..+?$/, '').replace(/\//g,'.');
result = "Handlebars.initNS( '" + key + "' ); " + key + " = " + source;
} else {
result = umd(source);
}
} catch (_error) {
error = _error;
}
if (error) return callback(error);
return callback(null, result);
};
module.exports = HandlebarsCompiler;