A handlebars
template loader for webpack
.
{
...
module: {
loaders: [
...
{ test: /\.handlebars$/, loader: "handlebars-loader" }
]
}
}
var template = require("./file.handlebars");
// => returns file.handlebars content as a template function
The loader resolves partials and helpers automatically. They are looked up relative to the current directory (this can be modified with the rootRelative
option) or as a module if you prefix with $
.
The following query options are supported:
- helperDirs: Defines additional directories to be searched for helpers. Allows helpers to be defined in a directory and used globally without relative paths.
- runtime: Specify the path to the handlebars runtime library. Defaults to look under the local handlebars npm module, i.e.
handlebars/runtime
. - extensions: Searches for templates with alternate extensions. Defaults are .handlebars, .hbs, and '' (no extension).
- inlineRequires: Defines a regex that identifies strings within helper/partial parameters that should be replaced by inline require statements.
- rootRelative: When automatically resolving partials and helpers, use an implied root path if none is present. Default =
./
. Setting this to be empty effectively turns off automatically resolving relative handlebars resources for items like{{helper}}
.{{./helper}}
will still resolve as expected.
See webpack
documentation for more information regarding loaders.
See example folder in this repo. The example is now fully runnable and demonstrates a number of concepts (using partials and helpers) -- just run webpack
in that directory to produce dist/bundle.js in the same folder.