Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const yargs = require('yargs')
alias: 'commonjs',
default: null,
})
.option('esm', {
type: 'string',
description: 'Exports ES Modules style, path to Handlebars module',
alias: 'module',
default: null,
})
.option('h', {
type: 'string',
description: 'Path to handlebar.js (only valid for amd-style)',
Expand Down
5 changes: 4 additions & 1 deletion lib/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module.exports.cli = function (opts) {
if (
!opts.amd &&
!opts.commonjs &&
!opts.esm &&
opts.templates.length === 1 &&
!opts.templates[0].name
) {
Expand Down Expand Up @@ -209,6 +210,8 @@ module.exports.cli = function (opts) {
);
} else if (opts.commonjs) {
output.add('var Handlebars = require("' + opts.commonjs + '");');
} else if (opts.esm) {
output.add('import Handlebars from "' + opts.esm + '";');
} else {
output.add('(function() {\n');
}
Expand Down Expand Up @@ -274,7 +277,7 @@ module.exports.cli = function (opts) {
output.add(['return ', objectName, ';\n']);
}
output.add('});');
} else if (!opts.commonjs) {
} else if (!opts.commonjs && !opts.esm) {
output.add('})();');
}
}
Expand Down
1 change: 1 addition & 0 deletions spec/expected/help.menu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Options:
--map Source Map File [string]
-a, --amd Exports amd style (require.js) [boolean]
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
--esm, --module Exports ES Modules style, path to Handlebars module [string] [default: null]
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
-k, --known Known helpers [string]
-o, --knownOnly Known helpers only [boolean]
Expand Down
11 changes: 10 additions & 1 deletion spec/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ describe('precompiler', function () {
Handlebars.precompile = function () {
return 'commonjs';
};
Precompiler.cli({ templates: [emptyTemplate], commonjs: true });
Precompiler.cli({ templates: [emptyTemplate], commonjs: 'handlebars' });
equal(/template\(commonjs\)/.test(log), true);
equal(log.indexOf('var Handlebars = require("handlebars");') > -1, true);
});
it('should output esm templates', function () {
Handlebars.precompile = function () {
return 'esm';
};
Precompiler.cli({ templates: [emptyTemplate], esm: 'handlebars' });
equal(/template\(esm\)/.test(log), true);
equal(log.indexOf('import Handlebars from "handlebars";') > -1, true);
});

it('should set data flag', function () {
Expand Down