/
handlebars.js
133 lines (112 loc) · 4.7 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
/*
* grunt-contrib-handlebars
* http://gruntjs.com/
*
* Copyright (c) 2012 Tim Branyen, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
var _ = grunt.util._;
var helpers = require('grunt-lib-contrib').init(grunt);
// content conversion for templates
var defaultProcessContent = function(content) { return content; };
// filename conversion for templates
var defaultProcessName = function(name) { return name; };
// filename conversion for partials
var defaultProcessPartialName = function(filePath) {
var pieces = _.last(filePath.split('/')).split('.');
var name = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
return name.substr(1, name.length); // strips leading _ character
};
grunt.registerMultiTask('handlebars', 'Compile handlebars templates and partials.', function() {
var options = this.options({
namespace: 'JST',
separator: grunt.util.linefeed + grunt.util.linefeed,
wrapped: true,
amd: false
});
grunt.verbose.writeflags(options, 'Options');
var nsInfo;
if(options.namespace !== false){
nsInfo = helpers.getNamespaceDeclaration(options.namespace);
}
// assign regex for partial detection
var isPartial = options.partialRegex || /^_/;
// assign transformation functions
var processContent = options.processContent || defaultProcessContent;
var processName = options.processName || defaultProcessName;
var processPartialName = options.processPartialName || defaultProcessPartialName;
this.files.forEach(function(f) {
var partials = [];
var templates = [];
// iterate files, processing partials and templates separately
f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
})
.forEach(function(filepath) {
var src = processContent(grunt.file.read(filepath));
var compiled, filename;
try {
compiled = require('handlebars').precompile(src);
// if configured to, wrap template in Handlebars.template call
if (options.wrapped) {
compiled = 'Handlebars.template('+compiled+')';
}
// if configured for amd and the namespace has been explicitly set
// to false, the handlebars template will be directly returned
if (options.wrapped && options.amd && options.namespace === false) {
compiled = 'return ' + compiled;
}
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('Handlebars failed to compile '+filepath+'.');
}
// register partial or add template to namespace
if (isPartial.test(_.last(filepath.split('/')))) {
filename = processPartialName(filepath);
partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
} else {
filename = processName(filepath);
if (options.namespace !== false) {
templates.push(nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';');
} else {
templates.push(compiled);
}
}
});
var output = partials.concat(templates);
if (output.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
if (options.namespace !== false) {
output.unshift(nsInfo.declaration);
if (options.node) {
output.unshift('var Handlebars = Handlebars || require(\'Handlebars\');');
var nodeExport = 'if (typeof exports === \'object\' && exports) {';
nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
output.push(nodeExport);
}
}
if (options.amd) {
// Wrap the file in an AMD define fn.
output.unshift("define(['handlebars'], function(Handlebars) {");
if (options.namespace !== false) {
// Namespace has not been explicitly set to false; the AMD
// wrapper will return the object containing the template.
output.push("return "+nsInfo.namespace+";");
}
output.push("});");
}
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
grunt.log.writeln('File "' + f.dest + '" created.');
}
});
});
};