简体   繁体   English

无法全局配置image-webpack-loader

[英]Can't configure image-webpack-loader globally

I want to configure image-webpack-loader globally the same to it's documentation. 我想全局配置image-webpack-loader,使其与文档相同。

Webpack 2 has another way for setting this. Webpack 2有另一种设置方法。 Now we need to use LoaderOptionsPlugin to configure loaders. 现在,我们需要使用LoaderOptionsPlugin配置加载程序。

This is what i'm trying to do: 这就是我想要做的:

plugins: [
            new webpack.LoaderOptionsPlugin({
                options: {
                    imageWebpackLoader: {
                        mozjpeg: {
                            quality: 65,
                        },
                        pngquant: {
                            quality: '65-90',
                            speed: 4,
                        },
                        svgo: {
                            plugins: [
                                {
                                    removeViewBox: false,
                                },
                                {
                                    removeEmptyAttrs: false,
                                },
                            ],
                        },
                    },
                },
            }),
        ],

As a result i'm starting to get error: 结果,我开始出现错误:

ERROR in ./imgs/imgInStyles.jpg
    Module build failed: TypeError: Cannot read property 'bypassOnDebug' of null
        at Object.module.exports (D:\work\research\deployment\webpack\webpack2-config-sample\node_modules\image-webpack-loader\index.js:30:26)
     @ ./~/css-loader!./~/postcss-loader!./~/sass-loader/lib/loader.js!./app/pages/home/home.scss 6:158-198

It seems that i have configured LoaderOptionsPlugin wrong but can't understand where is the issue. 看来我已将LoaderOptionsPlugin配置为错误,但无法理解问题出在哪里。

The LoaderOptionsPlugin is to make the transition from webpack 1 easier. LoaderOptionsPlugin可以简化从webpack 1的过渡。 In webpack 2 you should define the options directly on the loader. 在webpack 2中,您应该直接在加载程序上定义选项。

Your rule would look like this (using the webpack 2 rule from the README with your options): 您的规则如下所示(使用自述文件中的webpack 2规则和选项):

{
    test: /\.(gif|png|jpe?g|svg)$/i,
    use: [
        'file-loader',
        {
            loader: 'image-webpack-loader',
            options: {
                mozjpeg: {
                    quality: 65,
                },
                pngquant: {
                    quality: '65-90',
                    speed: 4,
                },
                svgo: {
                    plugins: [
                        {
                            removeViewBox: false,
                        },
                        {
                            removeEmptyAttrs: false,
                        },
                    ],
                }
            }
        }
    ]
}

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

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