pcardune / handlebars-loader

A handlebars template loader for webpack
557 stars 169 forks source link

Helpers not detected in webpack #202

Open tosuccess20xx opened 3 years ago

tosuccess20xx commented 3 years ago

Hi,

This is my configuration for handlebars helpers:

                {
                        test: /\.hbs$/,
                        use: [
                        {

                              loader: 'handlebars-loader',
                              options: {
                                    precompileOptions: {
                                          knownHelpersOnly: false,
                                    },
                                    helperDirs: path.resolve(__dirname, './src/component/hbs/helpers'),
                              }
                        }  
                             ]
                }

helper.js

function addYear(s) {
      return s + ' ' + new Date().getFullYear();
    }

    module.exports = addYear;

hbs file

{{ addYear '@' }}

and I keep getting this error whenever I run build:

ERROR in Template execution failed: Error: Missing helper: "addYear"

ERROR in   Error: Missing helper: "addYear"
alexandercerutti commented 3 years ago

It is not very clear but what I was able to understand from this example is that helperDirs is an Array of strings.

Especially, what made me understand that, were the two brackets after helperDirs:

?helperDirs[]=" + __dirname + "/helpers"

In fact, during my first setup I tried with this, and it seems to have worked:

helperDirs: [
    path.resolve(__dirname, "myPathToHelpers"),
],

Hope this helps!