Skip to content

Commit

Permalink
Simplify object assignment generation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Aug 4, 2015
1 parent 00f7442 commit 77e6bfc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
23 changes: 9 additions & 14 deletions lib/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions spec/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit 77e6bfc

Please sign in to comment.