【发布时间】:2021-11-16 04:49:35
【问题描述】:
我需要帮助调试 Webpack 的压缩插件。
问题摘要
- 目标是启用资产压缩并减少我的应用程序包的大小。使用 Brotli 算法作为默认算法,并将 gzip 作为不支持的浏览器的后备。
- 我希望资产的响应标头中有一个 content-encoding 字段。相反,它们是在没有字段的情况下加载的。我使用 Chrome 开发工具的网络选项卡来确认这一点。有关上下文,请参阅以下 sn-p:
- 在本地运行时,我的浏览器或 IDE 中没有显示错误。
我尝试了什么
- 对压缩插件使用不同的实现。请参阅下面的方法列表:
- (使用 Webpack 链 API)
config
.plugin('brotliCompress')
.use(CompressionWebpackPlugin, [{
exclude: /.map$/,
cache: true,
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}])
- (使用 Webpack 链 API)
config
.plugin('gzip')
.use(CompressionWebpackPlugin, [{
algorithm: 'gzip',
test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
threshold: 8192, // Assets larger than 8192 bytes are not processed
minRatio: 0.8, // Assets compressing worse that this ratio are not processed
}])
- (使用 Webpack 链 API)
config
.plugin('CompressionPlugin')
.use(CompressionWebpackPlugin)
- (使用 vue-cli-plugin:压缩)当我使用
vue invoke compression响应 IDE 控制台消息后,由于 Missing generator 错误而失败运行vue add compression作为使用 Webpack Chain API 进行压缩配置的替代方案。
pluginOptions: {
compression: {
brotli: {
filename: '[file].br[query]',
algorithm: 'brotliCompress',
include: /\.(js|css|html|svg|json)(\?.*)?$/i,
minRatio: 0.8,
},
gzip: {
filename: '[file].gz[query]',
algorithm: 'gzip',
include: /\.(js|css|html|svg|json)(\?.*)?$/i,
minRatio: 0.8
}
}
},
- 最后,我尝试将阈值字段设置为 0 并将其提高到大于 10k 字节。
意义点
- 上述尝试未达到我在第一个摘要项目符号中所述的目标,并被用来代替之前测试的方法。
- 我优先考虑使用 Webpack Chain API,因为它在重新构建和运行应用程序时不会导致任何错误。
参考链接/文档
- https://webpack.js.org/plugins/compression-webpack-plugin/
- https://github.com/neutrinojs/webpack-chain/tree/main
- https://neutrinojs.org/webpack-chain/#config-plugins-adding
- https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/500(与另一个插件类似的生成器问题)
- https://webpack.js.org/plugins/compression-webpack-plugin/
- Use webpack-chain to do webpack configuration in vue.config.js, so how to use speed-measure-webpack-plugin plugin?(不是一个有效的答案,但仍然引用了语法)
- https://github.com/vuejs/vue-cli/issues/6091#issuecomment-738536334
- Webpack prerender-spa-plugin with compression-webpack-plugin. index.html not compressed
代码
vue.config.js
const path = require('path')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
function resolve (dir) {
return path.join(__dirname, dir)
}
module.exports = {
/* ....shortened for brevity */
// Compress option VI (with vue cli plugin, generator bug when invoked)
// pluginOptions: {
// compression: {
// brotli: {
// filename: '[file].br[query]',
// algorithm: 'brotliCompress',
// include: /\.(js|css|html|svg|json)(\?.*)?$/i,
// minRatio: 0.8,
// },
// gzip: {
// filename: '[file].gz[query]',
// algorithm: 'gzip',
// include: /\.(js|css|html|svg|json)(\?.*)?$/i,
// minRatio: 0.8
// }
// }
// },
chainWebpack: config => {
config
.resolve.alias
.set('@', resolve('src'))
config
.plugins.delete('prefetch')
config
.optimization.splitChunks()
config
.output
.chunkFilename('[id].js')
// The below configurations are recommeneded only in prod.
// config.when(process.env.NODE_ENV === 'production', config => { config... })
// Compress option VII
// config
// .plugin('gzip')
// .use(CompressionWebpackPlugin, [{
// algorithm: 'gzip',
// test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
// threshold: 8192, // Assets larger than 8192 bytes are not processed
// minRatio: 0.8, // Assets compressing worse that this ratio are not processed
// }])
// Compress option VIII
// config
// .plugin('CompressionPlugin')
// .use(CompressionWebpackPlugin)
config
.plugin('brotliCompress')
.use(CompressionWebpackPlugin, [{
exclude: /.map$/,
// deleteOriginalAssets: true,
cache: true,
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}])
},
}
package.json
"dependencies": {
"@auth0/auth0-spa-js": "^1.15.0",
"audio-recorder-polyfill": "^0.4.1",
"compression-webpack-plugin": "^6.0.0",
"core-js": "^3.6.5",
"dotenv": "^8.2.0",
"dotenv-expand": "^5.1.0",
"moment": "^2.29.1",
"register-service-worker": "^1.7.1",
"uuid": "^3.4.0",
"vue": "^2.6.11",
"vue-loader": "^15.9.8",
"vue-router": "^3.5.1",
"vuex": "^3.6.2"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-pwa": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-cli-plugin-compression": "~1.1.5",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.46.0"
}
感谢所有的意见。谢谢。
【问题讨论】:
标签: javascript performance vue.js webpack vuejs2