【问题标题】:Why can't I embed images in webpack with HTML img tags?为什么我不能在带有 HTML img 标签的 webpack 中嵌入图像?
【发布时间】:2021-11-08 08:48:39
【问题描述】:

为什么我不能用 HTML img 标签在 webpack 中嵌入图片?

下面是 webpack.config.js 的内容。

// webpack.config.js
const {
    CleanWebpackPlugin,
} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = [{
        mode: 'development',
        entry: {
            'px.bundle': ['./src/index.js'],
        },
        
        output: {
            path: path.resolve(__dirname,'dist'),
            filename: '[name].js'
        },
        module:{
            rules:[
                {
                    test:/\.css$/,
                    use:[
                        'style-loader',
                        'css-loader'
                    ]
                },
                {
                    test: /\.(png|jpe?g|gif)$/i,
                    use: [
                        {
                            loader: 'file-loader',
                        },
                    ],
                }
            ]
        },
        devServer: {
            static : {
                directory: path.join(__dirname, 'public'),
            },
            compress: true,
            port: 9000
        },
        plugins: [
            new CleanWebpackPlugin({
                cleanOnceBeforeBuildPatterns: ["**/*", "!assets/**/*", "!resources/**/*"],
            }),
            new MiniCssExtractPlugin({
                filename: 'style.css',
            }),
            new HtmlWebpackPlugin({
                title: 'Project Demo',
                minify: {
                    collapseWhitespace: true
                },
                hash: true,
                template: './src/index.html'
            }),

            
        ],
        performance: {
            hints: false,
            maxEntrypointSize: 512000,
            maxAssetSize: 512000,
        },
    }, {
        mode: 'product',
        entry: {
            'px.bundle': ['./src/index.js'],
        },
        output: {
            path: path.resolve(__dirname,'dist'),
            filename: '[name].js',
        },
        module:{
            rules:[
                {
                    test:/\.css$/,
                    use:['style-loader','css-loader']
                }
            ]
        },
        plugins: [
            new CleanWebpackPlugin({
                cleanOnceBeforeBuildPatterns: ["**/*", "!assets/**"],
                cleanOnceBeforeBuildPatterns: ["**/*", "!resources/**"],
                cleanAfterEveryBuildPatterns: ['dist'],
            }),
            new MiniCssExtractPlugin({
                filename: 'style.css',
            }),
            new HtmlWebpackPlugin({
                title: 'Project Demo',
                minify: {
                    collapseWhitespace: true
                },
                hash: true,
                template: './src/index.html'
            }),
        ],
        performance: {
            hints: false,
            maxEntrypointSize: 512000,
            maxAssetSize: 512000,
        },
    },
];

下面是包含 index.html 内容的代码。 <img class="header__logo" src="img/ico_logo.png" alt="company logo" />

如果像这样输入 npm run dev 命令,img/ico_logo.png 的内容将无法正常输出。当然,我检查了文件在路径中的正确位置。

把上面的代码改成下面这样, <img class="header__logo" alt="company logo" />

如果我在我的 css 中包含以下内容

.header__logo{
    content:url('../img/ico_logo.png')
}

如果输入 npm run dev 命令,可以看到运行正常。

如果在 HTML 中将 src 指定为 img 标签,webpack 不能包含图片。 不知道是webpack设置错了还是webpack只能通过css中的url才能正常工作。

我想知道是否由于某种原因 webpack 没有正确嵌入徽标图像,我该如何解决?

【问题讨论】:

    标签: javascript html css webpack frontend


    【解决方案1】:

    我认为原因是 webpack 无法读取 HTML,尽管您使用了 HtmlWebpackPlugin。它只是帮助您将 HTML 提取到文件中,但看起来它并没有读取内容。 我有同样的问题,但我通过添加使用html-loader 解决了它。 我在规则中增加了一个新对象。

    ,{
        test: /\.html$/i,
        loader: 'html-loader'
      }
    

    而且它有效。 此外,在 webpack 5 中,文件加载器已被 Asset Modules 替换。 希望这可以帮助你,真诚的。?

    【讨论】:

      【解决方案2】:

      你可以试试这个

       {
            test: /\.(jpg|jpeg|png|gif|pdf|ico)$/,
            use: [
              {
                loader: "file-loader",
                options: {
                  name: "images/[name].[ext]",
                },
              },
            ],
          },
          {
            test: /\.html$/,
            use: [
              {
                loader: "html-loader",
                options: {
                  minimize: true,
                },
              },
            ],
          },
      

      【讨论】:

        猜你喜欢
        • 2022-12-04
        • 1970-01-01
        • 1970-01-01
        • 2021-11-19
        • 2014-07-16
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 2016-02-19
        相关资源
        最近更新 更多