Dan*_*sch 8 javascript performance webpack vue.js vuejs2
我需要帮助调试 Webpack 的压缩插件。
问题总结

我尝试过的
config
.plugin('brotliCompress')
.use(CompressionWebpackPlugin, [{
exclude: /.map$/,
cache: true,
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}])
Run Code Online (Sandbox Code Playgroud)
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
}])
Run Code Online (Sandbox Code Playgroud)
config
.plugin('CompressionPlugin')
.use(CompressionWebpackPlugin)
Run Code Online (Sandbox Code Playgroud)
vue invoke compressionvue add compression 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
}
}
},
Run Code Online (Sandbox Code Playgroud)
重要意义
参考链接/文档
代码
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,
}])
},
}
Run Code Online (Sandbox Code Playgroud)
包.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"
}
Run Code Online (Sandbox Code Playgroud)
我感谢所有的意见。谢谢。
似乎是compression-webpack-plugin压缩文件,但它不会自动配置开发服务器来提供压缩文件来代替原始文件。
vue.config.js但是,您可以通过\ 的devServer选项手动设置中间件(传递给webpack-dev-server来执行此操作:
重写所有.js接受br编码的请求以附加.br到原始 URL,该 URL 与filename给定的设置匹配compression-webpack-plugin。这有效地获取了.br由插件压缩的文件。
// vue.config.js\nconst CompressionPlugin = require(\'compression-webpack-plugin\')\n\nmodule.exports = {\n transpileDependencies: true,\n configureWebpack: {\n plugins: [\n new CompressionPlugin({ 1\xef\xb8\x8f\xe2\x83\xa3\n filename: \'[path][base].br\',\n algorithm: \'brotliCompress\',\n test: /\\.js$/,\n })\n ]\n },\n devServer: {\n setupMiddlewares(middlewares, devServer) {\n if (!devServer) {\n throw new Error(\'webpack-dev-server is not defined\')\n }\n\n middlewares.unshift({\n name: \'serve-brotli-js\',\n path: \'*.js\',\n middleware: (req, res, next) => {\n if (req.get(\'Accept-Encoding\')?.includes(\'br\')) {\n 1\xef\xb8\x8f\xe2\x83\xa3\n req.url += \'.br\'\n\n 2\xef\xb8\x8f\xe2\x83\xa3\n res.set(\'Content-Encoding\', \'br\')\n res.set(\'Content-Type\', \'application/javascript; charset=utf-8\')\n }\n next()\n }\n })\n\n return middlewares\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n注意:与 Webpack 5 的唯一区别是 Expressapp直接作为参数传递给devserver.before().
// vue.config.js\n\xe2\x8b\xae\nmodule.exports = {\n \xe2\x8b\xae\n devServer: {\n before(app) {\n // same code as Webpack 5 above\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
7201 次 |
| 最近记录: |