Détail du package

handlebars-loader

pcardune581.8kMIT1.7.3

handlebars loader module for webpack

readme

latest version downloads Build Status Coverage Status Reviewed by Hound

handlebars-loader

A handlebars template loader for webpack.

Handlebars 4 now supported

Installation

npm i handlebars-loader --save

General Usage

webpack configuration

{
  ...
  module: {
    rules: [
      ...
      { test: /\.handlebars$/, loader: "handlebars-loader" }
    ]
  }
}

Your JS making use of the templates

var template = require("./file.handlebars");
// => returns file.handlebars content as a template function

Details

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 $.

A file "/folder/file.handlebars".
{{> partial}} will reference "/folder/partial.handlebars".
{{> ../partial}} will reference "/partial.handlebars".
{{> $module/partial}} will reference "/folder/node_modules/module/partial.handlebars".
{{helper}} will reference the helper "/folder/helper.js" if this file exists.
{{[nested/helper] 'helper parameter'}} will reference the helper "/folder/nested/helper.js" if this file exists, passes 'helper parameter' as first parameter to helper.
{{../helper}} {{$module/helper}} are resolved similarly to partials.

The following query (or config) 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. You must surround helpers in subdirectories with brackets (Handlerbar helper identifiers can't have forward slashes without this). See example
  • 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. Note: For this to work, you'll have to disable the esModule Option in the corresponding file-loader entry in your webpack config.
  • 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.
  • knownHelpers: Array of helpers that are registered at runtime and should not explicitly be required by webpack. This helps with interoperability for libraries like Thorax helpers.
  • 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
  • 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.
  • precompileOptions: Options passed to handlebars precompile. See the Handlebars.js documentation 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:
    handlebarsLoader: {
        partialResolver: function(partial, callback){
            // 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 documentation for more information regarding loaders.

Full examples

See the examples folder in this repo. The examples are fully runnable and demonstrate a number of concepts (using partials and helpers) -- just run webpack in that directory to produce dist/bundle.js in the same folder, open index.html.

Change Log

See the CHANGELOG.md file.

License

MIT (http://www.opensource.org/licenses/mit-license)

changelog

Change Log

[1.7.3] - 2022-12-07

Fixed

  • Upgraded loader-utils dependendency to 1.4.2, which fixes a critical vulnerability

[1.7.2] - 2022-05-18

Fixed

  • Upgraded async dependency to 3.2.2 (#207)

[1.7.1] - 2018-12-18

Fixed

  • Fixed use stringifyRequest instead of absolute paths in loader output (#167)

[1.7.0] - 2018-03-20

Fixed

  • Use loaderContext.rootContext instead of loaderContext.options when used with Webpack 4
  • Fixed resolving of inline partials and partial blocks with failover content (#106, #135)

[1.6.0] - 2017-09-01

Added

  • Added ignoreHelpers option to skip automatic lookup/bundling of helpers
  • Added precompileOptions to pass options to handlebars precompile

[1.5.0] - 2017-04-20

Added

  • Added helperResolver config option to override the default helper resolution

Fixed

  • Fixed webpack deprecation warnings

[1.4.0] - 2016-09-02

Added

  • 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.
  • 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 resolution

Fixed

  • Previously, if a partial name began with an @, it was ignored and treated as an internal to handlebars partial. Now that checks specifically for partials named @partial-block so that {{> @a/b/c.hbs }} is a valid partial reference

[1.3.0] - 2016-04-29

Added

  • New partialDirs query option allows specifying additional directories to be searched for partials. Thank you @lostthetrail.
  • New preventIndent query option to avoid nested partials adding whitespace to textarea and pre elements.

[1.2.0] - 2016-03-15

Added

  • Helpers can now use ECMAScript 6 export syntax and have their default export used. Thank you @kt0.
  • New exclude query option lets you prevent helpers from being resolved to modules in specified directories (such as node_modules). Thank you @ericmatthys.

See keepachangelog.com for how to update this file.