
Compress Files
Compress Files 관련
Compressing files can reduce file download time, providing a better user experience.
Thanks to the development of Webpack and Node, file compression is now very convenient.
In Webpack, you can use the following plugins for compression:
- JavaScript: UglifyPlugin
- CSS: MiniCssExtractPlugin
- HTML: HtmlWebpackPlugin
In fact, we can do even better by using gzip compression. This can be enabled by adding the gzip identifier to the Accept-Encoding header in the HTTP request header. Of course, the server must also support this feature.
Gzip is currently the most popular and effective compression method. For example, the app.js file generated after building a project I developed with Vue has a size of 1.4MB, but after gzip compression, it's only 573KB, reducing the volume by nearly 60%.
Here are the methods for configuring gzip in webpack and node.
Download plugins
npm install compression-webpack-plugin --save-dev
npm install compression
Webpack configuration
const CompressionPlugin = require('compression-webpack-plugin');
module.exports = {
plugins: [new CompressionPlugin()],
}
Node configuration
const compression = require('compression')
// Use before other middleware
app.use(compression())