最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack file-loader outputs empty images - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 10

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 :

{
    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/

发布评论

评论列表(0)

  1. 暂无评论