Skip to content

Commit 5790773

Browse files
committed
Add possibility to precompile as ES module
1 parent 30bd78d commit 5790773

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

bin/handlebars.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,30 @@ const yargs = require('yargs')
1111
type: 'string',
1212
description: 'Source Map File'
1313
})
14-
.option('a', {
15-
type: 'boolean',
16-
description: 'Exports amd style (require.js)',
17-
alias: 'amd'
14+
.option('esm', {
15+
type: 'string',
16+
description:
17+
'Exports ECMAScript module style, path to Handlebars module (eg. "handlebars/lib/handlebars")',
18+
default: null
1819
})
1920
.option('c', {
2021
type: 'string',
2122
description: 'Exports CommonJS style, path to Handlebars module',
2223
alias: 'commonjs',
2324
default: null
2425
})
26+
.option('a', {
27+
type: 'boolean',
28+
description: 'Exports AMD style (require.js)',
29+
alias: 'amd',
30+
deprecated: true
31+
})
2532
.option('h', {
2633
type: 'string',
27-
description: 'Path to handlebar.js (only valid for amd-style)',
34+
description: 'Path to handlebar.js (only valid for AMD-style)',
2835
alias: 'handlebarPath',
29-
default: ''
36+
default: '',
37+
deprecated: true
3038
})
3139
.option('k', {
3240
type: 'string',

lib/precompiler.js

+2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ module.exports.cli = function(opts) {
206206
);
207207
} else if (opts.commonjs) {
208208
output.add('var Handlebars = require("' + opts.commonjs + '");');
209+
} else if (opts.esm) {
210+
output.add(`import handlebars from "${opts.esm}";`);
209211
} else {
210212
output.add('(function() {\n');
211213
}

spec/expected/help.menu.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ Options:
55
--help Outputs this message [boolean]
66
-f, --output Output File [string]
77
--map Source Map File [string]
8-
-a, --amd Exports amd style (require.js) [boolean]
8+
--esm Exports ECMAScript module style, path to Handlebars module (eg. "handlebars/lib/handlebars")
9+
[string] [default: null]
910
-c, --commonjs Exports CommonJS style, path to Handlebars module [string] [default: null]
10-
-h, --handlebarPath Path to handlebar.js (only valid for amd-style) [string] [default: ""]
11+
-a, --amd Exports AMD style (require.js) [deprecated] [boolean]
12+
-h, --handlebarPath Path to handlebar.js (only valid for AMD-style) [deprecated] [string] [default: ""]
1113
-k, --known Known helpers [string]
1214
-o, --knownOnly Known helpers only [boolean]
1315
-m, --min Minimize output [boolean]

spec/precompiler.js

+7
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ describe('precompiler', function() {
197197
equal(/return Handlebars\.partials\[/.test(log), false);
198198
equal(/template\(amd\)/.test(log), true);
199199
});
200+
it('should output es module templates', function() {
201+
Handlebars.precompile = function() {
202+
return 'esm';
203+
};
204+
Precompiler.cli({ templates: [emptyTemplate], esm: true });
205+
equal(/template\(esm\)/.test(log), true);
206+
});
200207
it('should output commonjs templates', function() {
201208
Handlebars.precompile = function() {
202209
return 'commonjs';

0 commit comments

Comments
 (0)