pcardune / handlebars-loader

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

Feature request: Optional partialResolver #205

Open Devqon opened 2 years ago

Devqon commented 2 years ago

Hi,

We use the handlebars-loader in combination with Storybook and plain html/scss, which works awesome!

We want to configure handlebars to optionally resolve partials based on prefixed, and if not passed use the default partial resolver. There is now only an option to override the behavior completely, without fallback.

What we want to achieve:

<!-- custom resolving, search in a directory called 'legacy' -->
{{> legacy.my-partial }}
<!-- should still work (fallback to default resolve) -->
{{> my-partial }}
thibaudsowa commented 1 year ago

Hi,

I had the same request and I manage to do this by that ugly thing:

{
    test: /\.(html|handlebars|hbs)$/,
    use: [
        {
            loader: 'handlebars-loader',
            options: {
                partialResolver: function (partial, callback) {
                    callback(undefined, (partial.startsWith('common/') ? path.join(__dirname, 'src', 'partials', partial.replace('common/', '')) : './' + partial) + '.hbs')
                }
            }
        }
    ]
}

In this example, with prefix 'common', partials will be loaded from '/src/partials", without prefix it will follow standard import.