【问题标题】:Webpack doesn't bundle images referenced only in HTMLWebpack 不捆绑仅在 HTML 中引用的图像
【发布时间】:2017-08-15 17:23:47
【问题描述】:

我遇到了一个我自己无法解决的扼杀问题。当仅在 HTML 中引用它们时,我的 webpack 配置不会将源目录中的 imagenes 编译到 build 目录,而它确实编译在 css 中引用的 imagenes 没有任何问题。这是我的 webpack.config.file:

            var webpack = require('webpack');
            var path = require('path');


            module.exports = {
                entry: {
                    bundle: './js/app.js',
                    vendor: 'jquery'
                },
                output: {
                    path: path.resolve('./build'),
                    filename: "[name].js",
                    publicPath: './build/'
                },
                plugins: [
                    new webpack.optimize.UglifyJsPlugin(),
                    new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.js', minChunks: Infinity })],
                watch: true,
                module: {
                    loaders: [{
                        test: /\.js$/,
                        exclude: /node_modules/,
                        loader: 'babel-loader',
                        query: {
                            presets: ['es2015']
                        }
                    },
                    {
                        test: /\.scss$/,
                        loaders: ['style-loader', 'css-loader', 'sass-loader']
                    },
                    {
                        test: /\.(png|jpg|gif|svg)$/,
                        loaders: [{
                            loader: 'file-loader',
                            options: {
                                name: 'img/[name].[ext]',
                                context: '',
                            }
                        },
                        {
                            loader: 'image-webpack-loader',
                            query: {

                                mozjpeg: {
                                    progressive: true,
                                },
                                gifsicle: {
                                    interlaced: false,
                                },
                                optipng: {
                                    optimizationLevel: 5,
                                },
                                pngquant: {
                                    quality: '75-90',
                                    speed: 5,
                                },
                            }
                        }
                        ]
                    },

                    {
                        test: /\.(eot|ttf|woff|woff2)$/,
                        loader: 'url-loader'
                    },


                    {
                        test: /\.(html)$/,
                        use: {
                            loader: 'html-loader',
                            options: {
                                attrs: [':img-src']
                            }
                        }
                    }

                    ]
                }
            };

【问题讨论】:

    标签: html webpack config task-runner-explorer


    【解决方案1】:

    html-loader 只会处理attrs 选项中以<tag>:<attribute> 形式指定的属性。您正在使用:img-src,这意味着匹配任何标签(因为:之前的值为空)并处理属性img-src

    你要做的是处理img标签的src属性:

    {
        loader: 'html-loader',
        options: {
            attrs: ['img:src']
        }
    }
    

    这已经是html-loader 的默认值(参见README - Usage),这意味着您不需要配置该选项。

    【讨论】:

    • 谢谢,但它仍然不起作用,即使我删除了附加选项。我能看到的唯一解决方法是将所有图像移动到 css 文件
    • 这并不能解决任何问题。您仍然需要使用 CopyWebpackPlugin 将图像复制到 dist。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2018-05-03
    • 2018-08-23
    • 2019-11-26
    • 2020-01-10
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    相关资源
    最近更新 更多