Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Option to) Create new Handlebars instance per plugin instance #62

Closed
alexschlueter opened this issue Jan 27, 2020 · 3 comments
Closed

Comments

@alexschlueter
Copy link

Hi, I'm trying to use multiple webpack targets and change a helper based on a variable:

webpack.config.js
const HandlebarsPlugin = require("handlebars-webpack-plugin");

const configForLang = lang => {
  return {
    mode: "development",
    plugins: [

      new HandlebarsPlugin({
        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?

@sagold
Copy link
Owner

sagold commented Jan 27, 2020

Yes indeed. This should actually be the default. It will be a breaking change, but fast to implement, since Handelbars is only required once.

@sagold
Copy link
Owner

sagold commented Jan 27, 2020

If you like, checkout the following branch and test your setup: https://github.com/sagold/handlebars-webpack-plugin/tree/issue-62-handlebars-instances

@alexschlueter
Copy link
Author

alexschlueter commented Jan 27, 2020

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 (typeof arg === "string") {
      var HB = HandlebarsPlugin.Handlebars; // not the correct instance used by plugin
      return HB.compile(HB.partials[path.join(arg, lang)])();
    } else {
      return arg[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:

onBeforeSetup: HB => {
  HB.registerHelper("translate", arg => {
    if (typeof arg === "string") {
      return HB.compile(HB.partials[path.join(arg, lang)])();
    } else {
      return arg[lang];
    }
  });
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants