Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Antoine Caron
Antoine Caron

Posted on • Originally published atslashgear.github.io on

     

Why you should use Compression Webpack Plugin?

If you are using webpack as module bundler for your application, you probably are concerned by its own performance and loading time.You may have used code splitting, tree shaking, uglifyfication but you may have forgot a very important step.

Compression is mandatory

In 2019, if you are not using a compression algorithm to deliver your files in a lighter version to make your website load faster, the situation is critical.Every website performance analyser will tell you to do it. Here is a size comparison of a vanillaVue CLI website with and without Gzip.




Vue CLI vanilla size compared to gzip

Here we reduce 64% of the actual size with Gzip.

Compressing static files on the fly is redondant

Every modern web server has the possibility to compress files on demand.Although Gzip is fast, compressing static files on each demand is anti-pattern.Even if you use a cache in front of your static files requests.

Static files compression should be done during the build time.

If you are in a webpack context, adding compression is very easy.

To do so, you can install thecompression-webpack-plugin with:

npminstall--devcompression-webpack-plugin
Enter fullscreen modeExit fullscreen mode

…and add it in your production configuration:

...plugins:[newCompressionWebpackPlugin(),]...
Enter fullscreen modeExit fullscreen mode

In two easy steps, your output static files will have their.gz equivalent.Now if you ship those files on a webserver likeCaddy.It will automatically deliver the pre-gzipped version if they exist.However you need to activate itin Nginx server configuration.

“Should we only generate .gz ?”

If you followed the above steps, you enabled compression on your webserver. That is great!

But in 2019, Gzip is not the only solution to compress your files.

Zopfli

Zopfli is a lossless compression algorithm which has better compression results compared to Gzip with slower compression time.So if you can, you must always use Zopfli compression for static files, instead of basic Gzip.Output files of Zopfli respects the same format as Gzip, browser support is the same.

You can use zopfliwith compression webpack plugin.

Brotli

Brotli is a recent compression algorithm (RFC 7932) witha great browser support.Based on a new context modeling and a new file format, brotli is very performant for compressing static files.There is still some difficult parts with brotli (not available on Node < v11, hard to configure on some webservers like Nginx)

You can use brotliwith compression webpack plugin.




Vue CLI compression results

In conclusion: “Why doing it with Webpack ?”

Compressing static files during the build steps is still a moot topic.Some people argue that it is up to the web server to compress the files.In fact, today web servers do not really optimize the compression of static files.

When you compress during the build :

  • You can easily see the complete size of your web application (which is great when you want to track web app performances)
  • Your app is much more portable (you will easily change your deployment or your web server with pre-compressed assets)
  • As a frontend developper you are familiar with the great value of lossless compression.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Some comments may only be visible to logged-in visitors.Sign in to view all comments.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web trublion making noise at M6Web
  • Location
    Lyon
  • Education
    Polytech Lyon
  • Work
    Frontend developer at Bedrock Streaming
  • Joined

More fromAntoine Caron

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp