webpack打包html里面img后src为“[object Module]”问题

这篇文章主要介绍了webpack打包html里面img后src为“[object Module]”问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在html中引入img图片

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document1</title> </head> <body> <div > <img src="https://www.jb51.net/article/logo.jpg" alt=""> </div> </html>

使用url-loader/file-loader结合html-loader打包

{ test: /\.(png|jpg|gif|jpeg)$/, use: [{ loader: 'url-loader', loader: 'file-loader', options: { name: '[name].[ext]', limit: 10240 } }] }, { test: /\.(htm|html)$/, loader: 'html-loader' }

发现打包后html里面,img的src为[object Module],

但是如果使用"file-loader": "^4.2.0"或者"file-loader": "^2.0.0"却可以正常打包

后来发现file-loader在新版本中esModule默认为true,因此手动设置为false

{ test: /\.(png|jpg|gif|jpeg)$/, use: [{ loader: 'url-loader', // loader: 'file-loader', options: { esModule: false, // 这里设置为false name: '[name].[ext]', limit: 10240 } }] }

webpack打包html里面img后src为“[object Module]”问题

这样就可以正常打包了

webpack打包html里面img后src为“[object Module]”问题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e9140d0da55b387898fc538a71fde50c.html