Skip to content

[Feature Request] Support for helpers in handlebars in compile mode #129

@Bessonov

Description

@Bessonov

Feature request

What is motivation or use case for adding/changing the behavior?

Handlebars supports helpers to extend its functionality. For example, there are no comparison or coalesce operators besides the if statement, which expects a boolean value. This limitation leads to boilerplate code that converts every statement like {{#if (eq value "apple")}} to template({isApple: value === "apple"}) and {{#if isApple}}. Using it with HTML controls to set selected in <select><option ... makes the situation even worse.

Additionally, handlebars-loader supports this feature via the helperDirs option. Implementing this would improve feature parity between the two.

Describe the solution you'd like
I would love for helpers to be compiled into the template function, allowing them to be used in imported templates.

Side note: handlebars-loader doesn’t allow inline scripts in the configuration like this plugin does. I think this is a reasonable tradeoff to enable compiling helpers into the template.

Side note 2: It seems like this plugin doesn’t support ESM helpers:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/vagrant/workspace/website/src/handlebars/coalesce.js from /home/vagrant/workspace/website/.cache/pnpm/virtual-store-dir/[email protected][email protected][email protected]/node_modules/html-bundler-webpack-plugin/src/Loader/Preprocessors/Handlebars/index.js not supported.
Instead change the require of coalesce.js in /home/vagrant/workspace/website/.cache/pnpm/virtual-store-dir/[email protected][email protected][email protected]/node_modules/html-bundler-webpack-plugin/src/Loader/Preprocessors/Handlebars/index.js to a dynamic import() which is available in all CommonJS modules.

Describe alternatives you've considered

  • Introducing is* variables.
  • Using handlebars-loader without inheritance.

Appreciation for the useful project

  • [⭐] After the feature is implemented, do not forget to give a star ⭐

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions