Skip to content

Commit

Permalink
Merge pull request #141 from ianmetcalf/hbs-precompile-options
Browse files Browse the repository at this point in the history
Adds config to pass options to handlebars precompile
  • Loading branch information
pcardune committed Sep 1, 2017
2 parents c76a656 + 72027f4 commit 029dabb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Added
- Added `ignoreHelpers` option to skip automatic lookup/bundling of helpers
- Added `precompileOptions` to pass options to handlebars precompile
- Your feature here!

## [1.5.0] - 2017-04-20

Expand All @@ -19,7 +21,6 @@
- Fixed resolving relative helpers on first pass when helper directories are given
- Added `ignorePartials` option to skip automatic lookup/bundling of partials
- Added `compat` option to enable Mustache lookup compatibility.
- Your feature here!
- Added `config` option to query so that configs can be specified in webpack
config object or the loader query. Defaults to `handlebarsLoader`
- Added `partialResolver` config option to override the default partial
Expand Down
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -61,10 +61,9 @@ The following query (or config) options are supported:
- *exclude*: Defines a regex that will exclude paths from resolving. This can be used to prevent helpers from being resolved to modules in the `node_modules` directory.
- *debug*: Shows trace information to help debug issues (e.g. resolution of helpers).
- *partialDirs*: Defines additional directories to be searched for partials. Allows partials to be defined in a directory and used globally without relative paths. See [example](https://github.com/altano/handlebars-loader/tree/master/examples/partialDirs)
- *preventIndent*: Prevent partials from being indented inside their parent template.
- *ignorePartials*: Prevents partial references from being fetched and bundled. Useful for manually loading partials at runtime.
- *ignoreHelpers*: Prevents helper references from being fetched and bundled. Useful for manually loading helpers at runtime.
- *compat*: Enables recursive field lookup for Mustache compatibility. See the Handlebars.js [documentation](https://github.com/wycats/handlebars.js#differences-between-handlebarsjs-and-mustache) for more information.
- *precompileOptions*: Options passed to handlebars precompile. See the Handlebars.js [documentation](http://handlebarsjs.com/reference.html#base-precompile) for more information.
- *config*: Tells the loader where to look in the webpack config for configurations for this loader. Defaults to `handlebarsLoader`.
- *config.partialResolver* You can specify a function to use for resolving partials. To do so, add to your webpack config:
```js
Expand Down
23 changes: 13 additions & 10 deletions index.js
Expand Up @@ -36,6 +36,8 @@ module.exports = function(source) {
throw new Error('Handlebars compiler version does not match runtime version');
}

var precompileOptions = query.precompileOptions || {};

// Possible extensions for partials
var extensions = query.extensions;
if (!extensions) {
Expand All @@ -55,12 +57,11 @@ module.exports = function(source) {
var foundUnclearStuff = {};
var knownHelpers = {};

var queryKnownHelpers = query.knownHelpers;
if (queryKnownHelpers) {
[].concat(queryKnownHelpers).forEach(function(k) {
[].concat(query.knownHelpers, precompileOptions.knownHelpers).forEach(function(k) {
if (k && typeof k === 'string') {
knownHelpers[k] = true;
});
}
}
});

var inlineRequires = query.inlineRequires;
if (inlineRequires) {
Expand Down Expand Up @@ -168,12 +169,14 @@ module.exports = function(source) {

try {
if (source) {
template = hb.precompile(source, {
knownHelpersOnly: firstCompile ? false : true,
template = hb.precompile(source, assign({
knownHelpersOnly: !firstCompile,
// TODO: Remove these in next major release
preventIndent: !!query.preventIndent,
compat: !!query.compat
}, precompileOptions, {
knownHelpers: knownHelpers,
preventIndent: query.preventIndent,
compat: query.compat ? true : false
});
}));
}
} catch (err) {
return loaderAsyncCallback(err);
Expand Down

0 comments on commit 029dabb

Please sign in to comment.