HTML Webpack Plugin will not compile as Thymeleaf variables are undefined

巧了我就是萌 提交于 2020-03-23 07:20:15

问题


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

回答1:


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 %>"/>



回答2:


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 ;)




回答3:


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



来源:https://stackoverflow.com/questions/46033326/html-webpack-plugin-will-not-compile-as-thymeleaf-variables-are-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!