gpt4 book ai didi

javascript - 在 webpack 中使用 html-webpack-plugin 和 string-replace-loader

转载 作者:太空狗 更新时间:2023-10-29 14:54:56 26 4
gpt4 key购买 nike

我正在尝试替换 index.html 中的变量,如下所示:

<meta name='author' content=$variable>

在我使用的配置文件中:

  {
test: /index\.html$/,
loader: 'string-replace',
query: {
search: '$variable',
replace: 'stuff to inject',
},
}

loaders阵列,然后在 plugins :

new HtmlWebpackPlugin({
template: conf.path.src('src/index.html'),
inject: true,
})

但是这个设置会导致:

ERROR in ./~/html-webpack-plugin/lib/loader.js!./src/index.html
Module parse failed (...) Unexpected token (1:0)
You may need an appropriate loader to handle this file type.

您是否知道这可能是由什么引起的,或者我该如何调试?

最佳答案

这是因为 string-replace-plugin 需要一个导出字符串的模块。您应该将 HTML 文件转换为 CommonJS 模块。

例如,这是使用raw-loader的方式:

首先,给html中的content属性加上引号

<meta name='author' content='$variable'>`

{
test: /index\.html$/,
loader: 'string-replace?search=$variable&replace=stuff to inject!raw'
}

关于javascript - 在 webpack 中使用 html-webpack-plugin 和 string-replace-loader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38359842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com