Compare commits

...

1 Commits

Author SHA1 Message Date
Jakob Linskeseder 88f789e419 Add possibility to precompile as ES module 2023-09-04 23:35:36 +02:00
4 changed files with 26 additions and 7 deletions
+13 -5
View File
@@ -11,10 +11,11 @@ const yargs = require('yargs')
type: 'string', type: 'string',
description: 'Source Map File', description: 'Source Map File',
}) })
.option('a', { .option('esm', {
type: 'boolean', type: 'string',
description: 'Exports amd style (require.js)', description:
alias: 'amd', 'Exports ECMAScript module style, path to Handlebars module (eg. "handlebars/lib/handlebars")',
default: null,
}) })
.option('c', { .option('c', {
type: 'string', type: 'string',
@@ -22,11 +23,18 @@ const yargs = require('yargs')
alias: 'commonjs', alias: 'commonjs',
default: null, default: null,
}) })
.option('a', {
type: 'boolean',
description: 'Exports AMD style (require.js)',
alias: 'amd',
deprecated: true,
})
.option('h', { .option('h', {
type: 'string', type: 'string',
description: 'Path to handlebar.js (only valid for amd-style)', description: 'Path to handlebar.js (only valid for AMD-style)',
alias: 'handlebarPath', alias: 'handlebarPath',
default: '', default: '',
deprecated: true,
}) })
.option('k', { .option('k', {
type: 'string', type: 'string',
+2
View File
@@ -209,6 +209,8 @@ module.exports.cli = function (opts) {
); );
} else if (opts.commonjs) { } else if (opts.commonjs) {
output.add('var Handlebars = require("' + opts.commonjs + '");'); output.add('var Handlebars = require("' + opts.commonjs + '");');
} else if (opts.esm) {
output.add(`import handlebars from "${opts.esm}";`);
} else { } else {
output.add('(function() {\n'); output.add('(function() {\n');
} }
+4 -2
View File
@@ -5,9 +5,11 @@ Options:
--help Outputs this message [boolean] --help Outputs this message [boolean]
-f, --output Output File [string] -f, --output Output File [string]
--map Source Map 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] -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] -k, --known Known helpers [string]
-o, --knownOnly Known helpers only [boolean] -o, --knownOnly Known helpers only [boolean]
-m, --min Minimize output [boolean] -m, --min Minimize output [boolean]
+7
View File
@@ -197,6 +197,13 @@ describe('precompiler', function () {
equal(/return Handlebars\.partials\[/.test(log), false); equal(/return Handlebars\.partials\[/.test(log), false);
equal(/template\(amd\)/.test(log), true); 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 () { it('should output commonjs templates', function () {
Handlebars.precompile = function () { Handlebars.precompile = function () {
return 'commonjs'; return 'commonjs';