Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ESM for helpers #146

Merged
merged 3 commits into from
Mar 14, 2018
Merged
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
7 changes: 4 additions & 3 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ internals.Manager.prototype._loadHelpers = function (engine) {
delete require.cache[require.resolve(file)]; // bust require's cache
}

const helper = require(file);
const required = require(file);
const offset = path.slice(-1) === Path.sep ? 0 : 1;
const name = file.slice(path.length + offset, -Path.extname(file).length);
const helper = required[name] || required.default || required;
if (typeof helper === 'function') {
const offset = path.slice(-1) === Path.sep ? 0 : 1;
const name = file.slice(path.length + offset, -Path.extname(file).length);
engine.module.registerHelper(name, helper);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"node": ">=8.9.0"
},
"scripts": {
"test": "lab -a code -t 100 -L",
"test": "lab -a code -t 100 -L -I '__core-js_shared__'",
"test-cov-html": "lab -a code -r html -o coverage.html",
"lint": "lab -L -d"
},
Expand Down
24 changes: 24 additions & 0 deletions test/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,30 @@ describe('Manager', () => {
expect(rendered).to.equal('Nav:{{> nav}}|{{> nested/nav}}');
});

it('loads ESM default export helpers and render them', async () => {

const tempView = new Manager({
engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests
path: __dirname + '/templates/valid',
helpersPath: __dirname + '/templates/valid/helpers'
});

const rendered = await tempView.render('testHelpersESMDefault', { something: 'uppercase' });
expect(rendered).to.equal('<p>This is all UPPERCASE and this is how we like it!</p>\n');
});

it('loads ESM named export helpers and render them', async () => {

const tempView = new Manager({
engines: { html: { module: Handlebars.create() } }, // Clear environment from other tests
path: __dirname + '/templates/valid',
helpersPath: __dirname + '/templates/valid/helpers'
});

const rendered = await tempView.render('testHelpersESMNamedExport', { something: 'uppercase' });
expect(rendered).to.equal('<p>This is all UPPERCASE and this is how we like it!</p>\n');
});

it('loads helpers and render them', async () => {

const tempView = new Manager({
Expand Down
6 changes: 6 additions & 0 deletions test/templates/valid/helpers/uppercaseESMDefault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports.default = function uppercaseESMDefault(context) {

return context.toUpperCase();
};
6 changes: 6 additions & 0 deletions test/templates/valid/helpers/uppercaseESMNamedExport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports.uppercaseESMNamedExport = function uppercaseESMNamedExport(context) {

return context.toUpperCase();
};
1 change: 1 addition & 0 deletions test/templates/valid/testHelpersESMDefault.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is all {{uppercaseESMDefault this.something}} and this is {{long "how"}} we like it!</p>
1 change: 1 addition & 0 deletions test/templates/valid/testHelpersESMNamedExport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is all {{uppercaseESMNamedExport this.something}} and this is {{long "how"}} we like it!</p>