admin管理员组文章数量:1379450
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
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
Share Improve this question asked Feb 24, 2022 at 11:12 Maxime RoudierMaxime Roudier 1798 bronze badges1 Answer
Reset to default 10Solution :
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 :
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
generator: {
filename: 'images/[name][ext]'
}
}
Those line will build the same images in /dist/images/
本文标签: javascriptWebpack fileloader outputs empty imagesStack Overflow
版权声明:本文标题:javascript - Webpack file-loader outputs empty images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744419247a2605354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论