Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

productionGzip in vue cli 3.0 #978

Closed
lzp4ever opened this issue Mar 13, 2018 · 11 comments
Closed

productionGzip in vue cli 3.0 #978

lzp4ever opened this issue Mar 13, 2018 · 11 comments

Comments

@lzp4ever
Copy link

lzp4ever commented Mar 13, 2018

What problem does this feature solve?

no productionGzip config after cli 3.0, would it be add?

What does the proposed API look like?

productionGzip: on

@yyx990803
Copy link
Member

Probably no, as most static servers have gzip capabilities. You can easily add this yourself using vue.config.js -> configureWebpack + compression-webpack-plugin.

@realeve
Copy link

realeve commented Aug 15, 2018

const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']

configureWebpack: {
        plugins: [
            new CompressionWebpackPlugin({
                asset: '[path].gz[query]',
                algorithm: 'gzip',
                test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                threshold: 10240,
                minRatio: 0.8
            })
        ]
    }

@nearzeno
Copy link

@realeve 我试了,但是没有将.gz文件注入进index.html,不知道有什么好的方法吗?

@realeve
Copy link

realeve commented Aug 15, 2018

@aloneweb .gzip文件不是注入index.html的。

在服务端没有开启gzip的情况下,读取一个js/css文件,如果存在其.gzip版本则服务器优先返回压缩的文件,前台拿到后在浏览器层面解压。
但就像 evan you说的那样,目前大多数服务器都开启了gzip功能,文件会自动压缩,这个你在前台的网络面板里面能够看到。

我这里开gzip纯粹是为了看一下文件实际传输大小是多大,尽可能优化体积,同时服务端也是把gzip默认华人打开了的。

@nearzeno
Copy link

@realeve 懂了,非常感谢大佬的解答 👍

@RehanSaeed
Copy link

To do this right and optimally, there is more code to this than I would care to maintain. Would appreciate if this was built into Vue CLI 3. Ideally, you'd support Zopfli for better GZIP compression and Brotli too which requires three dependencies alltogether.

const BrotliPlugin = require("brotli-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const zopfli = require("@gfx/zopfli");

let plugins = [];
if (process.env.NODE_ENV === "production") {
  const compressionTest = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
  plugins = [
    new CompressionPlugin({
      algorithm(input, compressionOptions, callback) {
        return zopfli.gzip(input, compressionOptions, callback);
      },
      compressionOptions: {
        numiterations: 15
      },
      minRatio: 0.99,
      test: compressionTest
    }),
    new BrotliPlugin({
      test: compressionTest,
      minRatio: 0.99
    })
  ];
}

module.exports = {
  configureWebpack: {
    plugins
  },
  // ...omitted
}

@ayizhi
Copy link

ayizhi commented Sep 10, 2018

Not success at begining,found that compression-webpack-plugin option “asset” has been changed to “filename” at last version 2.0.0,For avoiding confusion

@lifedever
Copy link

@ayizhi Thank you very much !

@magicdawn
Copy link

magicdawn commented Mar 19, 2019

For any one using this #978 (comment)
when u met unknown option error, use this

$ npm i -D compression-webpack-plugin@1

@AccessViolator
Copy link

With node 11.7.0+ compression-webpack-plugin has native support for brotli
https://github.com/webpack-contrib/compression-webpack-plugin#using-brotli

@Anima-No
Copy link

brotli

This package has been deprecated

https://www.npmjs.com/package/iltorb?activeTab=explore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests