diff --git a/bin/handlebars.js b/bin/handlebars.js index ee1156c8..4ff97563 100755 --- a/bin/handlebars.js +++ b/bin/handlebars.js @@ -11,10 +11,11 @@ const yargs = require('yargs') type: 'string', description: 'Source Map File', }) - .option('a', { - type: 'boolean', - description: 'Exports amd style (require.js)', - alias: 'amd', + .option('esm', { + type: 'string', + description: + 'Exports ECMAScript module style, path to Handlebars module (eg. "handlebars/lib/handlebars")', + default: null, }) .option('c', { type: 'string', @@ -22,11 +23,18 @@ const yargs = require('yargs') alias: 'commonjs', default: null, }) + .option('a', { + type: 'boolean', + description: 'Exports AMD style (require.js)', + alias: 'amd', + deprecated: true, + }) .option('h', { type: 'string', - description: 'Path to handlebar.js (only valid for amd-style)', + description: 'Path to handlebar.js (only valid for AMD-style)', alias: 'handlebarPath', default: '', + deprecated: true, }) .option('k', { type: 'string', diff --git a/lib/precompiler.js b/lib/precompiler.js index 2b3fb69d..7875bb69 100644 --- a/lib/precompiler.js +++ b/lib/precompiler.js @@ -209,6 +209,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'); } diff --git a/spec/expected/help.menu.txt b/spec/expected/help.menu.txt index 750aa30c..2c30e14d 100644 --- a/spec/expected/help.menu.txt +++ b/spec/expected/help.menu.txt @@ -5,9 +5,11 @@ Options: --help Outputs this message [boolean] -f, --output Output File [string] --map Source Map File [string] - -a, --amd Exports amd style (require.js) [boolean] + --esm Exports ECMAScript module style, path to Handlebars module (eg. "handlebars/lib/handlebars") + [string] [default: null] -c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null] - -h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""] + -a, --amd Exports AMD style (require.js) [deprecated] [boolean] + -h, --handlebarPath Path to handlebar.js (only valid for AMD-style) [deprecated] [string] [default: ""] -k, --known Known helpers [string] -o, --knownOnly Known helpers only [boolean] -m, --min Minimize output [boolean] diff --git a/spec/precompiler.js b/spec/precompiler.js index 857f64e2..1f3b2563 100644 --- a/spec/precompiler.js +++ b/spec/precompiler.js @@ -197,6 +197,13 @@ describe('precompiler', function () { equal(/return Handlebars\.partials\[/.test(log), false); equal(/template\(amd\)/.test(log), true); }); + it('should output es module templates', function () { + Handlebars.precompile = function () { + return 'esm'; + }; + Precompiler.cli({ templates: [emptyTemplate], esm: true }); + equal(/template\(esm\)/.test(log), true); + }); it('should output commonjs templates', function () { Handlebars.precompile = function () { return 'commonjs';