简体   繁体   English

为什么我不能在带有 HTML img 标签的 webpack 中嵌入图像?

[英]Why can't I embed images in webpack with HTML img tags?

Why can't I embed images in webpack with HTML img tags?为什么我不能在带有 HTML img 标签的 webpack 中嵌入图像?

Below is the content of webpack.config.js.下面是 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,
        },
    },
];

And below is the code including the contents of index.html.下面是包含 index.html 内容的代码。 <img class="header__logo" src="img/ico_logo.png" alt="company logo" />

If enter the npm run dev command like this, the contents of img/ico_logo.png are not output properly.如果像这样输入npm run dev命令,img/ico_logo.png的内容就不是output了。 Of course, I checked that the file is properly located in the path.当然,我检查了文件是否正确位于路径中。

Change the above code to the following, <img class="header__logo" alt="company logo" />将上面的代码改成如下, <img class="header__logo" alt="company logo" />

If I include the following in my css如果我在我的 css 中包含以下内容

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

If enter the npm run dev command, I can see that it works properly.如果输入 npm run dev 命令,我可以看到它工作正常。

If specify src as an img tag in HTML, webpack cannot include the image.如果在HTML中指定src为img标签,webpack不能包含图片。 I don't know if the webpack setting is wrong or if webpack works normally only through the url in css.不知道是webpack设置错误还是webpack只能通过css中的url才能正常工作。

I'm wondering if for some reason webpack is not embedding the logo image properly, and how can I fix it?我想知道是否由于某种原因 webpack 没有正确嵌入徽标图像,我该如何解决?

I think the reason is that webpack can't read HTML, although you had used HtmlWebpackPlugin.我认为原因是 webpack 无法读取 HTML,尽管您使用了 HtmlWebpackPlugin。 It just helps you to extract the HTML to a file, but it looks like it doesn't read the content.它只是帮助您将 HTML 提取到文件中,但看起来它没有读取内容。 I had the same problem, but I solved it by adding use html-loader .我有同样的问题,但我通过添加 use html-loader解决了它。 I increase a new object into rules.我在规则中增加了一个新的 object。

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

And it works.它有效。 And also, the file-loader has been replaced by Asset Modules in webpack 5. Hope this could help you, sincerely.此外,文件加载器已被 webpack 中的资产模块取代 5. 真诚地希望这对您有所帮助。

you can try with this你可以试试这个

 {
      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,
          },
        },
      ],
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM