Last active
September 8, 2021 15:59
Example of registering custom handlebars helpers (+ handlebars-form-helpers) with webpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import Handlebars runtime lib | |
const Handlebars = require('handlebars/runtime'); | |
const register = require('handlebars-form-helpers').register; | |
const { registerHandlebarHelpers } = require('../../some/other/place'); | |
// Register extra helpers | |
register(Handlebars); | |
registerHandlebarHelpers(Handlebars); | |
/** | |
* Handlebars runtime with custom helpers. | |
* Used by handlebars-loader. | |
*/ | |
module.exports = Handlebars; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't forget to add the rest of the webpack configuration! | |
module.exports = { | |
module: { | |
rules: [ | |
{ | |
test: /\.hbs$/, | |
use: [ | |
{ | |
loader: 'handlebars-loader', | |
options: { | |
// Path to your custom js file, which has Handlebars with custom helpers registered | |
runtime: join(__dirname, 'app/src/handlebars') | |
} | |
} | |
] | |
} | |
] | |
}, | |
resolve: { | |
extensions: ['.hbs'] | |
} | |
}; |
yes a structure diagram would be great here!
I had to add options.precompileOptions.knownHelpersOnly = false to handlebars-loader on webpack config in order to compile successfully.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please elaborate on how to make this work?
I integrated these snippets into my webpack project and I can build my project successfully, as long as I don't use my helper.
When inserting
{{{concat 'a' 'b'}}}
into myindex.hbs
I get errors though.This is my handlebars.js: