webpack - 如何修复 webpack 输出的图像 src 中的 [Object Module]?
问题描述
我正在尝试使用 Webpack 设置一个现代的香草网络启动器。
webpack-html-loader
添加and时我卡住了html-loader
。这就是发生的事情......
- 当我使用条目文件中的
img
标签时,具有这样的属性 ( ),文件夹中的属性被替换为. 但是当我删除( to )之前的点时,文件夹中的输出与. 这不引用我想要包含的图像。index.html
src
./imgs/image.png
src
dist
[object Module]
uri
./imgs/image.png
/imgs/image.png
src
dist
src
- 由于
src
webpack 输出的值与源完全相同,所以我尽量粗略地使用uri
输出文件夹中的图像,如下所示/dist/img/image.png
。这行得通,但这不是一个很好的体验。我很想uri
在文件夹中使用我的图像,并在构建时src
让 webpack 替换它。dist
这可能吗?
这是我的文件结构之后的样子npm run build
- dist
- imgs
- image.png
- index.html
- main.js
- node_modules
- src
- index.html
- imgs
- image.png
- package.json
- package-lock.json
- webpack.config.js
这是我的webpack.config.js
const HTMLWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");
module.exports = {
module: {
rules: [
{
test: /\.(html)$/,
use: [
{
loader: "html-loader",
options: {
minimize: false
}
}
]
},
{
test: /\.(png|jpe?g)$/,
use: [
{
loader: "file-loader",
options: {
outputPath: "imgs",
publicPath: "imgs",
name: "[name].[ext]"
}
}
]
}
]
},
plugins: [
new HTMLWebpackPlugin({
template: path.resolve(__dirname, "src/index.html")
}),
new CleanWebpackPlugin()
]
};
我在这里做了一个回购
非常感谢您的时间
更新(02-01-2020)
评论中有人指出了问题的解决方案。(当我在 webpack 中使用 file-loader 和 html-loader 时。图像的 src 属性会像 '[object Module]')
解决方案是像这样在规则中设置esModules
对象false
file-loader
{
test: /\.(png|jpe?g)$/,
use: [
{
loader: "file-loader",
options: {
// Here!!!
esModule: false,
outputPath: "images",
publicPath: "images",
name: "[name].[ext]"
}
}
]
}
解决方案
推荐阅读
- swiftui - SwiftUI Nested ForEach 导致致命错误:每个布局项可能只出现一次
- c - main 不考虑 for 循环
- excel - Excel从url加载图像
- azure - 如何设计和构建 Elastic 堆栈(使用 Beats、Logstash、Elastic Search、Kibana)?(最佳实践)
- java - Spring boot 启动失败(无法异步初始化原生EntityManagerFactory:java.util.NoSuchElementException)
- java - 在命令提示符上运行 jps
- python-3.x - 如何填充 numpy.array 以便它可以是 np.vsplit()?
- c - 如何从 C 中的成员访问包含结构?
- reactjs - 创建具有唯一 url 的反应页面
- c# - 正确保存“ç”字符