diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ef4a8615..c2199a2df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index cba603156..d44eeeb4e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 7c9077fa7..53888ac58 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/test.js b/test/test.js index ad1b2e1e7..a6e05c820 100644 --- a/test/test.js +++ b/test/test.js @@ -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'),