You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm trying to use multiple webpack targets and change a helper based on a variable:
webpack.config.js
constHandlebarsPlugin=require("handlebars-webpack-plugin");constconfigForLang=lang=>{return{mode: "development",plugins: [newHandlebarsPlugin({entry: "./index.hbs",output: `./dist/[name]_${lang}.html`,data: {"welcome": {"en": "Welcome to my site!","de": "Willkommen zu meiner Seite!"}},helpers: {translate: obj=>obj[lang]}})]};};module.exports=[configForLang("en"),configForLang("de")];
index.hbs
<h1>{{translate welcome}}</h1>
This is currently not possible, since you are using the same global Handlebars instance for every plugin instance and helpers are just overridden:
HandlebarsPlugin: + helper 'translate'
HandlebarsPlugin: The helper 'translate' is already registered.
Remove duplications to prevent hard to find errors.
A solution to this would be to call this.Handlebars = Handlebars.create() in the constructor of HandlebarsPlugin and then use this local instance in the rest of the methods.
Would you be willing to add an option for this?
The text was updated successfully, but these errors were encountered:
Thank you, works for me! There's a slight problem now if you want to have access to the Handlebars instance from a helper, since you can no longer do this:
helpers: {translate: arg=>{if(typeofarg==="string"){varHB=HandlebarsPlugin.Handlebars;// not the correct instance used by pluginreturnHB.compile(HB.partials[path.join(arg,lang)])();}else{returnarg[lang];}}}
However, I was able to get around this by registering my helper inside onBeforeSetup, where the actual Handlebars instance is made available by the argument:
Hi, I'm trying to use multiple webpack targets and change a helper based on a variable:
webpack.config.js
index.hbs
This is currently not possible, since you are using the same global Handlebars instance for every plugin instance and helpers are just overridden:
A solution to this would be to call
this.Handlebars = Handlebars.create()
in the constructor ofHandlebarsPlugin
and then use this local instance in the rest of the methods.Would you be willing to add an option for this?
The text was updated successfully, but these errors were encountered: