Skip to content

Commit 77e6bfc

Browse files
committed
Simplify object assignment generation logic
1 parent 00f7442 commit 77e6bfc

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lib/precompiler.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ module.exports.cli = function(opts) {
9494
if (opts.simple && opts.min) {
9595
throw new Handlebars.Exception('Unable to minimize simple output');
9696
}
97-
if (opts.simple && (opts.templates.length !== 1 || opts.hasDirectory)) {
97+
98+
const multiple = opts.templates.length !== 1 || opts.hasDirectory;
99+
if (opts.simple && multiple) {
98100
throw new Handlebars.Exception('Unable to output multiple templates in simple mode');
99101
}
100102

@@ -109,6 +111,8 @@ module.exports.cli = function(opts) {
109111
}
110112
}
111113

114+
const objectName = opts.partial ? 'Handlebars.partials' : 'templates';
115+
112116
let output = new SourceNode();
113117
if (!opts.simple) {
114118
if (opts.amd) {
@@ -151,28 +155,19 @@ module.exports.cli = function(opts) {
151155

152156
if (opts.simple) {
153157
output.add([precompiled, '\n']);
154-
} else if (opts.partial) {
155-
if (opts.amd && (opts.templates.length == 1 && !opts.hasDirectory)) {
156-
output.add('return ');
157-
}
158-
output.add(['Handlebars.partials[\'', template.name, '\'] = template(', precompiled, ');\n']);
159158
} else {
160-
if (opts.amd && (opts.templates.length == 1 && !opts.hasDirectory)) {
159+
if (opts.amd && !multiple) {
161160
output.add('return ');
162161
}
163-
output.add(['templates[\'', template.name, '\'] = template(', precompiled, ');\n']);
162+
output.add([objectName, '[\'', template.name, '\'] = template(', precompiled, ');\n']);
164163
}
165164
});
166165

167166
// Output the content
168167
if (!opts.simple) {
169168
if (opts.amd) {
170-
if (opts.templates.length > 1 || (opts.templates.length == 1 && opts.hasDirectory)) {
171-
if (opts.partial) {
172-
output.add('return Handlebars.partials;\n');
173-
} else {
174-
output.add('return templates;\n');
175-
}
169+
if (multiple) {
170+
output.add(['return ', objectName, ';\n']);
176171
}
177172
output.add('});');
178173
} else if (!opts.commonjs) {

spec/precompiler.js

+7
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,12 @@ describe('precompiler', function() {
196196
done(err);
197197
});
198198
});
199+
200+
it('should complete when no args are passed', function(done) {
201+
Precompiler.loadTemplates({}, function(err, opts) {
202+
equal(opts.templates.length, 0);
203+
done(err);
204+
});
205+
});
199206
});
200207
});

0 commit comments

Comments
 (0)