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

Feature/helper resolver #130

Merged
merged 3 commits into from
Apr 19, 2017
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## [Unreleased]

### Added
- Your feature here!
- Added `helperResolver` config option to override the default helper resolution

## [1.4.0] - 2016-09-02

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ The following query (or config) options are supported:
```js
handlebarsLoader: {
partialResolver: function(partial, callback){
// should pass the partial's path on disk to
// should pass the partial's path on disk
// to the callback. Callback accepts (err, locationOnDisk)
}
}
```

- *config.helperResolver* You can specify a function to use for resolving helpers. To do so, add to your webpack config:
```js
handlebarsLoader: {
helperResolver: function(helper, callback){
// should pass the helper's path on disk
// to the callback if one was found for the given parameter.
// Callback accepts (err, locationOnDisk)
// Otherwise just call the callback without any arguments
}
}
```
See [`webpack`](https://github.com/webpack/webpack) documentation for more information regarding loaders.

## Full examples
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,13 @@ module.exports = function(source) {
if (foundHelpers[helper]) return helperCallback();
var request = referenceToRequest(helper.substr(1), 'helper');

resolve(request, 'helper', function(err, result) {
var defaultHelperResolver = function(request, callback){
return resolve(request, 'helper', callback);
};

var helperResolver = query.helperResolver || defaultHelperResolver;

helperResolver(request, function(err, result) {
if (!err && result) {
knownHelpers[helper.substr(1)] = true;
foundHelpers[helper] = result;
Expand Down
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,51 @@ describe('handlebars-loader', function () {
'handlebarsLoaderOverride'
);

var helperResolverOverride = function helperResolverOverride(request, cb){
switch(request) {
case './image':
cb(null, './helpers/image.js');
break;
case './json':
cb(null, './helpers2/json.js');
break;
default:
cb();
}
};

var testHelperResolver = function testHelperResolver(expectation, options, config){
it(expectation, function (done) {
testTemplate(loader, './with-dir-helpers-multiple.handlebars', {
query: {
config: config
},
options: options,
data: TEST_TEMPLATE_DATA
}, function (err, output, require) {
assert.ok(output, 'generated output');
done();
});
});
};

testHelperResolver(
'should use the helperResolver if specified', {
handlebarsLoader: {
helperResolver: helperResolverOverride
}
}
);

testHelperResolver(
'honors the config query option when finding the helperResolver', {
handlebarsLoaderOverride: {
helperResolver: helperResolverOverride
}
},
'handlebarsLoaderOverride'
);

it('allows specifying additional helper search directory', function (done) {
testTemplate(loader, './with-dir-helpers.handlebars', {
query: '?helperDirs[]=' + path.join(__dirname, 'helpers'),
Expand Down