Stay Hungry,Stay Foolish!

Web报文压缩方法

编译时压缩

https://www.cnblogs.com/qiuzhimutou/p/7592875.html

这里我列举几个常用的能够用于减少包体大小的插件,我们可以根据项目需求选择性的使用:
compression-webpack-plugin :该插件能够将资源文件压缩为.gz文件,并且根据客户端的需求按需加载。
dedupeplugin :抽取出输出包体中的相同或者近似的文件或者代码,可能对于 Entry Chunk 有所负担,不过能有效地减少包体大小。
uglifyjsplugin :压缩输出块的大小,可以参考官方文档。
ignoreplugin :用于忽略引入模块中并不需要的内容,譬如当我们引入moment.js时,我们并不需要引入该库中所有的区域设置,因此可以利用该插件忽略不必要的代码。
 

https://www.webpackjs.com/plugins/uglifyjs-webpack-plugin/

[
  new UglifyJsPlugin({
    uglifyOptions: {
      ie8: false,
      ecma: 8,
      parse: {...options},
      mangle: {
        ...options,
        properties: {
          // mangle property options
        }
      },
      output: {
        comments: false,
        beautify: false,
        ...options
      },
      compress: {...options},
      warnings: false
    }
  })
]


-----
new webpack.optimize.UglifyJsPlugin({
mangle: true,
compress: {
warnings: false, // Suppress uglification warnings
pure_getters: true,
unsafe: true,
unsafe_comps: true,
screw_ie8: true
},
output: {
comments: false,
},
exclude: [/\.min\.js$/gi] // skip pre-minified libs
}),

https://www.webpackjs.com/plugins/compression-webpack-plugin/

compression-webpack-plugin :该插件能够将资源文件压缩为.gz文件,并且根据客户端的需求按需加载。
var CompressionPlugin = require("compression-webpack-plugin");
module.exports = {
    plugins: [
        new CompressionPlugin({
            asset: "[path].gz[query]",
            algorithm: "gzip",
            test: /\.(js|html)$/,
            threshold: 10240,
            minRatio: 0.8
        })
    ]
}

 

 

请求中压缩

https://www.npmjs.com/package/compression

给express添加压缩中间件

Node.js compression middleware.

The following compression codings are supported:

  • deflate
  • gzip

The middleware will attempt to compress response bodies for all request that traverse through the middleware, based on the given options.

 

When using this module with express or connect, simply app.use the module as high as you like. Requests that pass through the middleware will be compressed.

var compression require('compression')
var express require('express')
 
var app express()
 
// compress all responses
app.use(compression())
 
// add all routes

 

posted @ 2020-02-28 00:21  lightsong  阅读(465)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel