html-webpack-plugin icon indicating copy to clipboard operation
html-webpack-plugin copied to clipboard

Configuring an *.html module loader beats down template variables

Open enepomnyaschih opened this issue 9 years ago • 5 comments

Registering a default loader for *.html modules in WebPack breaks the variables resolution in template. For example:

{ test: /\.html$/, loader: "raw-loader" }

In result, the variables are left unresolved:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <title><%= htmlWebpackPlugin.options.title %></title>
    </head>
    <body>
        <script type="text/javascript" src="jquery<%= htmlWebpackPlugin.options.suffix %>.js"></script>
    <script type="text/javascript" src="bundle.js"></script></body>
</html>

Changing the test expression to something more detailed fixes the problem:

{ test: /\.jw.html$/, loader: "raw-loader" } // OK!

I've created a minimal repository demonstrating the problem https://github.com/enepomnyaschih/html-plugin-bug

Just run:

git clone [email protected]:enepomnyaschih/html-plugin-bug.git bug
cd bug
npm install
webpack

And check public/index.html file.

Is that expected behaviour?

enepomnyaschih avatar Jan 05 '17 13:01 enepomnyaschih

Yes that's expected behaviour - you can find out more about that in the FAQ :)

jantimon avatar Jan 05 '17 14:01 jantimon

Thank you. Could you please give me a little bit more help? Can I somehow specify the fallback loader in "template" config option explicitly? Something like

new HtmlWebpackPlugin({
    template: "!!underscore!../templates/base.html"
})

Of course, installing https://www.npmjs.com/package/underscore-loader dependency does the trick, but is it possible to use your built-in loader instead? Also, for some reason, this solution refused to work properly until I changed the template path to "./templates/base.html". See diff

https://github.com/enepomnyaschih/html-plugin-bug/commit/40daa718c338f2c0c87f27d32226bb5c30b1f75d

enepomnyaschih avatar Jan 05 '17 15:01 enepomnyaschih

From https://github.com/ampedandwired/html-webpack-plugin/blob/d22c23066a2905f41b6acb999cbd5d2a5841de30/index.js#L605

"!!html-webpack-plugin/lib/loader.js!../templates/base.html"

jantimon avatar Jan 05 '17 15:01 jantimon

This is very helpful, thank you

enepomnyaschih avatar Jan 05 '17 16:01 enepomnyaschih

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar May 31 '18 18:05 lock[bot]