【问题标题】:How to compress images via webpack 4如何通过 webpack 4 压缩图像
【发布时间】:2018-05-31 17:01:21
【问题描述】:

我需要像 TinyPNG 一样压缩图像并将压缩图像保存在 dist 文件夹中。 我使用 webpack 4 并找到了 imagemin-webpack。但我不明白有什么用:插件或加载器? 请帮忙做这个任务的配置。

let path = require('path');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
let {imageminLoader} = require("imagemin-webpack");
let imageminGifsicle = require("imagemin-gifsicle");

let conf = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, './dist/'),
        filename: 'main.js',
        publicPath: 'dist/'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        {
                            loader: "css-loader",
                            options: {
                                minimize: true,
                                sourceMap: true
                            }
                        }
                    ]

                })
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                use: [
                    {
                        loader: "file-loader"
                    },
                    {
                        loader: imageminLoader,
                        options: {
                            cache: true,
                            bail: false,
                            imageminOptions: {
                                plugins: [imageminGifsicle()]
                            },
                            name: "[hash]-compressed.[ext]"
                        }
                    }
                ]
            }

        ]
    },
    plugins: [
        new ExtractTextPlugin("styles.css"),
    ]
};

这是我的配置文件。运行 build 命令后,图像没有任何反应。

【问题讨论】:

    标签: webpack webpack-4


    【解决方案1】:

    为了在 Webpack 4 中压缩图像,我使用“img-loader”。

                {
                    test: /\.(jpe?g|png|gif|svg)$/i,
                    loaders: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '' + imgPath + '[name].[ext]'
                            }
                        },
                        {
                            loader: 'img-loader',
                            options: {
                                plugins: [
                                    imageminGifsicle({
                                        interlaced: false
                                    }),
                                    imageminMozjpeg({
                                        progressive: true,
                                        arithmetic: false
                                    }),
                                    imageminPngquant({
                                        floyd: 0.5,
                                        speed: 2
                                    }),
                                    imageminSvgo({
                                        plugins: [
                                            { removeTitle: true },
                                            { convertPathData: false }
                                        ]
                                    })
                                ]
                            }
                        }
                    ]
                }
    

    当然,这些都是必需的变量:

    const imgPath = './assets/img/';
    const imageminGifsicle = require("imagemin-gifsicle");
    const imageminPngquant = require("imagemin-pngquant");
    const imageminSvgo = require("imagemin-svgo");
    const imageminMozjpeg = require('imagemin-mozjpeg');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-30
      • 2020-09-11
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 2013-01-18
      相关资源
      最近更新 更多