Webpack 文件加载器输出空图像
我之所以联系您是因为我的 webpack 配置遇到问题。我只想从 /src/images/ 复制 /dist/images/ 文件夹中的图像。
这是我的架构:
dist
|-images
| |-lorem-picsum.png
|-pages
| |--about.html
| |--home.html
|-a069f3e2cf7332c2cd34.png
|-main.js
node_modules
src
|-images
| |-lorem-picsum.png
|-pages
| |--about.html
| |--home.html
|-index.js
package-lock.json
package.json
webpack.config.js
我正在使用文件加载器,这是我的 webpack.config.js 的配置:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
module.exports = {
mode: 'development',
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'main.js',
},
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options : {
minimize : false,
}
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name : '[name].[ext]',
outputPath: 'images'
}
}
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template : path.resolve(__dirname, './src/pages/home.html'),
filename: path.resolve(__dirname, './dist/pages/home.html'),
}),
new HtmlWebpackPlugin({
template : path.resolve(__dirname, './src/pages/about.html'),
filename: path.resolve(__dirname, './dist/pages/about.html'),
})
],
};
当我启动“npm run build”时出现问题,它在 dist 中创建一个空图像(a069f3e2cf7332c2cd34.png)文件夹。该图像没有视觉内容,但包含以下文本:
export default __webpack_public_path__ + "images/lorem-picsum.png";
并且 /dist/home.html 现在包含:
<img src="../a069f3e2cf7332c2cd34.png" alt="">
但图像不显示在前面。
您知道如何避免创建这个“空”文件并简单地将我的图像文件夹复制到 dist 中,并保持与 /src/ 文件夹中写入的内容相同吗?
提前致谢
I'm reaching you because I'm facing an issue in the configuration of my webpack. I just want to copy my images in /dist/images/ folder from /src/images/.
Here is my architecture :
dist
|-images
| |-lorem-picsum.png
|-pages
| |--about.html
| |--home.html
|-a069f3e2cf7332c2cd34.png
|-main.js
node_modules
src
|-images
| |-lorem-picsum.png
|-pages
| |--about.html
| |--home.html
|-index.js
package-lock.json
package.json
webpack.config.js
I'm using file-loader and here is the config of my webpack.config.js:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
module.exports = {
mode: 'development',
entry: path.resolve(__dirname, './src/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'main.js',
},
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options : {
minimize : false,
}
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name : '[name].[ext]',
outputPath: 'images'
}
}
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template : path.resolve(__dirname, './src/pages/home.html'),
filename: path.resolve(__dirname, './dist/pages/home.html'),
}),
new HtmlWebpackPlugin({
template : path.resolve(__dirname, './src/pages/about.html'),
filename: path.resolve(__dirname, './dist/pages/about.html'),
})
],
};
The issue occur when I launch "npm run build", it's creating an empty image (a069f3e2cf7332c2cd34.png) in the dist folder. This image have no visual content but it contain this following text :
export default __webpack_public_path__ + "images/lorem-picsum.png";
And /dist/home.html now contain :
<img src="../a069f3e2cf7332c2cd34.png" alt="">
But the image isn't displayed on the front.
Do you have any idea how I can avoid creating this "empty" file and simply copy my image folder in dist, and keep the same as it's written in /src/ folder ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
问题是版本 5 中的 Webpack 对于图像不能像这样工作。
现在不需要“file-loader”,您可以简单地在 webpack.config.js 中使用以下规则:
这些行将在 /dist/images/ 中构建相同的图像
Solution :
The problem is that Webpack in version 5 isn't working like this for images.
'file-loader' isn't required for now, you can simply use this following rule in webpack.config.js :
Those line will build the same images in /dist/images/