Webpack 将错误的图像加载到 dist 目录

6 javascript webpack

我的webpack.config.js档案

const path = require('path');

const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: {
    index: './src/js/index.js',
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true },
          },
        ],
      },
      {
        test: /\.(jpg|png)$/,
        use: {
          loader: 'url-loader',
        },
      },
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'sass-loader',
        ],
      },
    ],
  },

  output: {
    path: path.resolve(__dirname, './dist'),
    filename: './js/[name].bundle.js',
  },

  plugins: [
    new HtmlWebPackPlugin({
      template: path.resolve(__dirname, './src/index.html'),
      filename: 'index.html',
      chunks: ['index'],
    }),

    new CleanWebpackPlugin(),

    new MiniCssExtractPlugin({
      filename: './css/[name].css',
    }),

    new CopyWebpackPlugin({
      patterns: [{ from: './src/assets', to: './assets' }],
    }),
  ],

  devtool: 'source-map',
  mode: 'development',
};
Run Code Online (Sandbox Code Playgroud)

我的文件结构出现问题:

在此输入图像描述

奇怪的是,当我npm run build在 中运行时terminal,所有目录都已正确加载,我的意思是:background-image有效,并且slider加载了带有图像的目录,但还加载了一些具有随机数字名称的附加文件

在此输入图像描述

这三个额外的.png文件被加载到我的index.htmlasimg1.png img2.pngimg3.png在我的网站上不起作用的文件,他们只是不想加载

use*_*480 0

我认为你的图像文件可能是双重处理的,因为你曾经只是将资产文件夹复制到 dist 输出文件夹,然后你可能在某个地方使用带有url("./someImage")或类似的 css 文件中的图像,这些图像由 css 加载器或 css 处理Mini Exctract 插件,它创建神秘的图像资源输出,然后在您的 html 文件中使用,请看这里:webpack.js.org/guides/asset-management/#loading-images.. 我不知道为什么您需要复制您的资产文件夹,但这对我来说看起来多余......

编辑:我不是这方面的专家,但是在对这个奇怪的错误进行了几个小时的调查之后,我意识到图像文件的路径设置正确,但是文件加载器或其他加载器随图像生成的图像输出(像 0f9b8be78d378ad2ef78.jpg) 似乎有所不同,如果我在标准文本/二进制编辑器中的 vscode 编辑器中打开它们,我会得到这一行: ,export default __webpack_public_path__ + "someimage.jpg";使用 url-loader ,它是带有 data-url(..) 的二进制64 转换字符串..所以看来它不是为了在静态 html 文件中使用它的目的。

然后我使用推荐的方法在 webpack.config.js 中执行此操作:

        {   test: /\.(jpg|png)$/, 
            type: 'asset/resource', }
Run Code Online (Sandbox Code Playgroud)

之后我的图像正确显示,我不知道文件加载器等在做什么。

这是我的最终 webpack.config.js:

const path = require('path')

const HtmlWebPackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
    entry: {
        index: './src/js/index.js',
    },
    devServer: {
        port: 5757,
        contentBase: path.resolve(__dirname, 'src'),
        publicPath: '/src',
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader'],
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: 'html-loader',
                        options: { minimize: true },
                    },
                ],
            },
            {
                test: /\.(jpg|png)$/,
                type: 'asset/resource',

            },
            {
                test: /\.(css|scss)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'postcss-loader',
                    'sass-loader',
                ],
            },
        ],
    },

    output: {
        path: path.resolve(__dirname, './build'),
        //publicPath: './assets/images',
    },

    plugins: [
        new HtmlWebPackPlugin({
            template: path.resolve(__dirname, './src/index.html'),
            filename: 'index.html',
            inject: 'body',
        }),

        new MiniCssExtractPlugin({
            filename: './css/[name].css',
        }),
    ],

    devtool: 'source-map',
    mode: 'development',
}
Run Code Online (Sandbox Code Playgroud)