html-webpack-plugin 模板变量替换怎么做双重替换?

如下例子,期望替换 html 中 type 的内容,该如何设置呢?

如下test.html内容:

<head>
    <title></title>
</head>
<body>
    <script>
        window.type = '<%="<"%>%type%<%=">"%>';
        window.buildTime = <%= buildTime %>;
    </script>
</body>
</html>

webpack相关的配置如下:

new HtmlWebpackPlugin({
    template: 'test.html',
    templateParameters: {
        buildTimestamp: Number(new Date())
    }
})
阅读 2.7k
1 个回答

templateContent 代替 templatetemplateParameters。先用正则替换完变量再通过 templateContent 传给插件。

new HtmlWebpackPlugin({
    templateContent: fs.readFileSync('test.html', 'utf-8')
    // templateContent: () => fs.readFileSync('test.html', 'utf-8')
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题