Simplify object assignment generation logic
This commit is contained in:
+9
-14
@@ -94,7 +94,9 @@ module.exports.cli = function(opts) {
|
||||
if (opts.simple && opts.min) {
|
||||
throw new Handlebars.Exception('Unable to minimize simple output');
|
||||
}
|
||||
if (opts.simple && (opts.templates.length !== 1 || opts.hasDirectory)) {
|
||||
|
||||
const multiple = opts.templates.length !== 1 || opts.hasDirectory;
|
||||
if (opts.simple && multiple) {
|
||||
throw new Handlebars.Exception('Unable to output multiple templates in simple mode');
|
||||
}
|
||||
|
||||
@@ -109,6 +111,8 @@ module.exports.cli = function(opts) {
|
||||
}
|
||||
}
|
||||
|
||||
const objectName = opts.partial ? 'Handlebars.partials' : 'templates';
|
||||
|
||||
let output = new SourceNode();
|
||||
if (!opts.simple) {
|
||||
if (opts.amd) {
|
||||
@@ -151,28 +155,19 @@ module.exports.cli = function(opts) {
|
||||
|
||||
if (opts.simple) {
|
||||
output.add([precompiled, '\n']);
|
||||
} else if (opts.partial) {
|
||||
if (opts.amd && (opts.templates.length == 1 && !opts.hasDirectory)) {
|
||||
output.add('return ');
|
||||
}
|
||||
output.add(['Handlebars.partials[\'', template.name, '\'] = template(', precompiled, ');\n']);
|
||||
} else {
|
||||
if (opts.amd && (opts.templates.length == 1 && !opts.hasDirectory)) {
|
||||
if (opts.amd && !multiple) {
|
||||
output.add('return ');
|
||||
}
|
||||
output.add(['templates[\'', template.name, '\'] = template(', precompiled, ');\n']);
|
||||
output.add([objectName, '[\'', template.name, '\'] = template(', precompiled, ');\n']);
|
||||
}
|
||||
});
|
||||
|
||||
// Output the content
|
||||
if (!opts.simple) {
|
||||
if (opts.amd) {
|
||||
if (opts.templates.length > 1 || (opts.templates.length == 1 && opts.hasDirectory)) {
|
||||
if (opts.partial) {
|
||||
output.add('return Handlebars.partials;\n');
|
||||
} else {
|
||||
output.add('return templates;\n');
|
||||
}
|
||||
if (multiple) {
|
||||
output.add(['return ', objectName, ';\n']);
|
||||
}
|
||||
output.add('});');
|
||||
} else if (!opts.commonjs) {
|
||||
|
||||
@@ -196,5 +196,12 @@ describe('precompiler', function() {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should complete when no args are passed', function(done) {
|
||||
Precompiler.loadTemplates({}, function(err, opts) {
|
||||
equal(opts.templates.length, 0);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user