learn-webpack4(七)


图片处理

图片处理 和 Base64 编码 (url-loader)
图片压缩 (img-loader)
合成雪碧图 (postcss-loader,postcss-sprites)

base64 编码和图片压缩

// webpack.config.js

const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

let extractTextPlugin = new ExtractTextPlugin({
  filename: "[name].min.css",
  allChunks: false
});

module.exports = {
  entry: {
    app: "./src/app.js"
  },
  output: {
    publicPath: __dirname + "/dist/",
    path: path.resolve(__dirname, "dist"),
    filename: "[name].bundle.js",
    chunkFilename: "[name].chunk.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: {
            loader: "style-loader"
          },
          use: [
            {
              loader: "css-loader"
            }
          ]
        })
      },
      {
        test: /\.(png|jpg|jpeg|gif)$/,
        //图片压缩
        use: [
          {
            loader: "url-loader",
            options: {
              name: "[name]-[hash:5].min.[ext]",
              limit: 1000, // size <= 1KB
              publicPath: "static/",
              outputPath: "static/"
            }
          },
          // img-loader for zip img
          {
            loader: "img-loader",
            options: {
              plugins: [
                require("imagemin-pngquant")({
                  quality: "80" // the quality of zip
                })
              ]
            }
          }
        ]
        //小于20KB的图片转为base64格式
        // use: [
        //   {
        //     loader: "url-loader",
        //     options: {
        //       name: "[name]-[hash:5].min.[ext]",
        //       limit: 20000, // size <= 20KB
        //       publicPath: "static/",
        //       outputPath: "static/"
        //     }
        //   }
        // ]
      }
    ]
  },
  plugins: [extractTextPlugin]
};

雪碧图合成

根据文档加入相应的配置即可

const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

let extractTextPlugin = new ExtractTextPlugin({
  filename: "[name].min.css",
  allChunks: false
});

/*********** sprites config ***************/
let spritesConfig = {
  spritePath: "./dist/static"
};
/******************************************/

module.exports = {
  entry: {
    app: "./src/app.js"
  },
  output: {
    publicPath: __dirname + "/dist/",
    path: path.resolve(__dirname, "dist"),
    filename: "[name].bundle.js",
    chunkFilename: "[name].chunk.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: {
            loader: "style-loader"
          },
          use: [
            {
              loader: "css-loader"
            },
            /*********** loader for sprites ***************/
            {
              loader: "postcss-loader",
              options: {
                ident: "postcss",
                plugins: [require("postcss-sprites")(spritesConfig)]
              }
            }
            /*********************************************/
          ]
        })
      },
      {
        test: /\.(png|jpg|jpeg|gif)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              name: "[name]-[hash:5].min.[ext]",
              limit: 10000, // size <= 20KB
              publicPath: "static/",
              outputPath: "static/"
            }
          },
          {
            loader: "img-loader",
            options: {
              plugins: [
                require("imagemin-pngquant")({
                  quality: "80"
                })
              ]
            }
          }
        ]
      }
    ]
  },
  plugins: [extractTextPlugin]
};

文章作者: 沐雪
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 沐雪 !
评论
 上一篇
learn-webpack4(八) learn-webpack4(八)
字体文件处理安装{ "devDependencies": { "css-loader": "^1.0.0", "extract-text-webpack-plugin": "^4.0.0-beta.0", "fi
2017-10-17
下一篇 
learn-webpack4(六) learn-webpack4(六)
Tree Shaking把没有用到的代码在打包的时候丢弃掉,依赖于 ed6 的模块系统 只需要配置 mode 为”production”,即可显式激活 UglifyjsWebpackPlugin 插件。现在默认就是激活的,不配置 mode
2017-10-16
  目录