Skip to content

Commit

Permalink
Add possibility to precompile as ES module
Browse files Browse the repository at this point in the history
  • Loading branch information
jaylinski committed Sep 4, 2023
1 parent 8ce2be4 commit 88f789e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
18 changes: 13 additions & 5 deletions bin/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ 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',
description: 'Exports CommonJS style, path to Handlebars module',
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',
Expand Down
2 changes: 2 additions & 0 deletions lib/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
6 changes: 4 additions & 2 deletions spec/expected/help.menu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions spec/precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 88f789e

Please sign in to comment.