2

When Webpack compiles my app which contains the below HTML, it complains the theme variable is undefined.

Here is my index.html file:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
</head>
<body th:class="${theme}">
    <div id="root"></div>
</body>
</html>

And here is the error I get:

Failed to compile.

Error in Template execution failed: ReferenceError: theme is not defined

- loader.js:4 eval
/src/main/resources/templates/index.html?./~/html-webpack-plugin/lib/loader.js:4:10
Ben Bullock
  • 77
  • 1
  • 12

3 Answers3

4

My workaround: replace $ to <%="$"%>

Nick
  • 3,691
  • 18
  • 36
3

Same error here. I've found this issue and you have just to make webpack use 'html-loader' like this:

new HtmlWebpackPlugin({
    template: '!!html-loader!index.html'
})

PS: Don't forget to install it before with npm ;)

2

Put this in :

webpack.config

new HtmlWebpackPlugin({
     template: './index.ejs',
     filename: './index.html',
     minify: { collapseWhitespace: true },
     csrf: '${_csrf.token}',
}),

in your index.ejs file put :

<meta name="_csrf" th:content="<%= htmlWebpackPlugin.options.csrf %>"/>
Aled Evans
  • 126
  • 1
  • 2