Skip to content

Commit 934fff8

Browse files
committed
adds support for es2015 output format
1 parent 06c9ba4 commit 934fff8

File tree

1 file changed

+96
-64
lines changed

1 file changed

+96
-64
lines changed

tasks/handlebars.js

Lines changed: 96 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = function(grunt) {
2525
// filename conversion for partials
2626
var defaultProcessPartialName = function(filepath) {
2727
var pieces = _.last(filepath.split('/')).split('.');
28-
var name = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
28+
var name = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
2929
if (name.charAt(0) === '_') {
3030
name = name.substr(1, name.length); // strips leading _ character
3131
}
@@ -61,10 +61,22 @@ module.exports = function(grunt) {
6161
wrapped: true,
6262
amd: false,
6363
commonjs: false,
64+
es2015: false,
65+
node: false,
6466
knownHelpers: [],
65-
knownHelpersOnly: false
67+
knownHelpersOnly: false,
6668
});
6769

70+
if (options.es2015) {
71+
// ES2015 doesn't allow to use "this" as root element
72+
options.root = options.root || 'templates';
73+
}
74+
if (options.es2015 || options.node) {
75+
// when using es2015 or node output format, the namespace option must be defined
76+
// Otherwise, it defaults to 'JST'
77+
options.namespace = options.namespace || 'JST';
78+
}
79+
6880
// assign regex for partials directory detection
6981
var partialsPathRegex = options.partialsPathRegex || /./;
7082

@@ -96,7 +108,11 @@ module.exports = function(grunt) {
96108
var nsDeclarations = {};
97109

98110
// nsdeclare options when fetching namespace info
99-
var nsDeclareOptions = {response: 'details', declared: nsDeclarations};
111+
var nsDeclareOptions = {
112+
response: 'details',
113+
declared: nsDeclarations,
114+
root: options.root
115+
};
100116

101117
// Just get the namespace info for a given template
102118
var getNamespaceInfo = _.memoize(function(filepath) {
@@ -111,81 +127,84 @@ module.exports = function(grunt) {
111127

112128
// iterate files, processing partials and templates separately
113129
f.src.filter(function(filepath) {
114-
// Warn on and remove invalid source files (if nonull was set).
115-
if (!grunt.file.exists(filepath)) {
116-
grunt.log.warn('Source file "' + filepath + '" not found.');
117-
return false;
118-
}
119-
return true;
120-
})
121-
.forEach(function(filepath) {
122-
var src = processContent(grunt.file.read(filepath), filepath);
123-
124-
var Handlebars = require('handlebars');
125-
try {
126-
// parse the handlebars template into it's AST
127-
ast = processAST(Handlebars.parse(src));
128-
compiled = Handlebars.precompile(ast, compilerOptions);
129-
130-
// if configured to, wrap template in Handlebars.template call
131-
if (options.wrapped === true) {
132-
compiled = 'Handlebars.template(' + compiled + ')';
130+
// Warn on and remove invalid source files (if nonull was set).
131+
if (!grunt.file.exists(filepath)) {
132+
grunt.log.warn('Source file "' + filepath + '" not found.');
133+
return false;
133134
}
134-
} catch (e) {
135-
grunt.log.error(e);
136-
grunt.fail.warn('Handlebars failed to compile ' + filepath + '.');
137-
}
138-
139-
// register partial or add template to namespace
140-
if (partialsPathRegex.test(filepath) && isPartialRegex.test(_.last(filepath.split('/')))) {
141-
filename = processPartialName(filepath);
142-
if (options.partialsUseNamespace === true) {
143-
nsInfo = getNamespaceInfo(filepath);
144-
if (nsInfo.declaration) {
145-
declarations.push(nsInfo.declaration);
135+
return true;
136+
})
137+
.forEach(function(filepath) {
138+
var src = processContent(grunt.file.read(filepath), filepath);
139+
140+
var Handlebars = require('handlebars');
141+
try {
142+
// parse the handlebars template into it's AST
143+
ast = processAST(Handlebars.parse(src));
144+
compiled = Handlebars.precompile(ast, compilerOptions);
145+
146+
// if configured to, wrap template in Handlebars.template call
147+
if (options.wrapped === true) {
148+
compiled = 'Handlebars.template(' + compiled + ')';
146149
}
147-
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + nsInfo.namespace +
148-
'[' + JSON.stringify(filename) + '] = ' + compiled + ');');
149-
} else {
150-
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + compiled + ');');
150+
} catch (e) {
151+
grunt.log.error(e);
152+
grunt.fail.warn('Handlebars failed to compile ' + filepath + '.');
151153
}
152-
} else {
153-
if ((options.amd || options.commonjs) && !useNamespace) {
154-
compiled = 'return ' + compiled;
155-
}
156-
filename = processName(filepath);
157-
if (useNamespace) {
158-
nsInfo = getNamespaceInfo(filepath);
159-
if (nsInfo.declaration) {
160-
declarations.push(nsInfo.declaration);
154+
155+
// register partial or add template to namespace
156+
if (partialsPathRegex.test(filepath) && isPartialRegex.test(_.last(filepath.split('/')))) {
157+
filename = processPartialName(filepath);
158+
if (options.partialsUseNamespace === true) {
159+
nsInfo = getNamespaceInfo(filepath);
160+
if (nsInfo.declaration) {
161+
declarations.push(nsInfo.declaration);
162+
}
163+
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + nsInfo.namespace +
164+
'[' + JSON.stringify(filename) + '] = ' + compiled + ');');
165+
} else {
166+
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + compiled + ');');
161167
}
162-
templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';');
163-
} else if (options.commonjs === true) {
164-
templates.push(compiled + ';');
165168
} else {
166-
templates.push(compiled);
169+
if ((options.amd || options.commonjs) && !useNamespace) {
170+
compiled = 'return ' + compiled;
171+
}
172+
filename = processName(filepath);
173+
if (useNamespace) {
174+
nsInfo = getNamespaceInfo(filepath);
175+
if (nsInfo.declaration) {
176+
declarations.push(nsInfo.declaration);
177+
}
178+
templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';');
179+
} else if (options.commonjs === true) {
180+
templates.push(compiled + ';');
181+
} else {
182+
templates.push(compiled);
183+
}
167184
}
168-
}
169-
});
185+
});
170186

171187
var output = declarations.concat(partials, templates);
172188
if (output.length < 1) {
173189
grunt.log.warn('Destination not written because compiled files were empty.');
174190
} else {
175-
if (useNamespace) {
176-
if (options.node) {
177-
output.unshift('Handlebars = glob.Handlebars || require(\'handlebars\');');
178-
output.unshift('var glob = (\'undefined\' === typeof window) ? global : window,');
179191

180-
var nodeExport = 'if (typeof exports === \'object\' && exports) {';
181-
nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
192+
if (nsDeclareOptions.root) {
193+
output.unshift('var ' + nsDeclareOptions.root + ' = ' + nsDeclareOptions.root + ' || {};');
194+
}
182195

183-
output.push(nodeExport);
184-
}
196+
if (options.node) {
197+
output.unshift('Handlebars = glob.Handlebars || require(\'handlebars\');');
198+
output.unshift('var glob = (\'undefined\' === typeof window) ? global : window,');
185199

200+
var nodeExport = 'if (typeof exports === \'object\' && exports) {';
201+
nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
202+
203+
output.push(nodeExport);
186204
}
187205

188206
if (options.amd) {
207+
189208
// Wrap the file in an AMD define fn.
190209
if (typeof options.amd === 'boolean') {
191210
output.unshift('define([\'handlebars\'], function(Handlebars) {');
@@ -203,9 +222,6 @@ module.exports = function(grunt) {
203222

204223
amdString += '\'' + options.amd[i] + '\'';
205224
}
206-
207-
// Wrap the file in an AMD define fn.
208-
output.unshift('define([' + amdString + '], function(Handlebars) {');
209225
}
210226

211227
if (useNamespace) {
@@ -217,6 +233,7 @@ module.exports = function(grunt) {
217233
}
218234

219235
if (options.commonjs) {
236+
220237
if (useNamespace) {
221238
output.push('return ' + nsInfo.namespace + ';');
222239
}
@@ -225,6 +242,21 @@ module.exports = function(grunt) {
225242
output.push('};');
226243
}
227244

245+
if (options.es2015) {
246+
247+
if (Array.isArray(options.es2015)) {
248+
options.es2015.forEach(function(dependency) {
249+
output.unshift('import ' + dependency.variable + ' from \'' + dependency.path + '\';');
250+
});
251+
} else {
252+
output.unshift('import Handlebars from \'handlebars\';');
253+
}
254+
255+
output.push('var ' + options.namespace + ' = ' + nsInfo.namespace + ';');
256+
output.push('export {' + options.namespace + '};');
257+
output.push('export default ' + options.namespace + ';');
258+
}
259+
228260
filesCount++;
229261
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
230262
grunt.verbose.writeln('File ' + chalk.cyan(f.dest) + ' created.');

0 commit comments

Comments
 (0)