From 2f1b747243631696e0837308d407c382ac38721d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E9=BE=99?= Date: Sat, 27 Aug 2022 11:04:07 +0800 Subject: [PATCH] feat: add compress plugin --- .env.production | 8 +++++++- build/plugin/index.js | 6 ++++++ package.json | 1 + pnpm-lock.yaml | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.env.production b/.env.production index 92abf61..9987509 100644 --- a/.env.production +++ b/.env.production @@ -8,4 +8,10 @@ VITE_APP_USE_MOCK = true VITE_APP_BASE_API = '/api' # test base api -VITE_APP_BASE_API_TEST = '/api-test' \ No newline at end of file +VITE_APP_BASE_API_TEST = '/api-test' + +# 是否启用压缩 +VITE_USE_COMPRESS = true + +# 压缩类型 +VITE_COMPRESS_TYPE = gzip \ No newline at end of file diff --git a/build/plugin/index.js b/build/plugin/index.js index 6f2a7f4..1afd6d4 100644 --- a/build/plugin/index.js +++ b/build/plugin/index.js @@ -14,6 +14,8 @@ import Unocss from 'unocss/vite' // rollup打包分析插件 import visualizer from 'rollup-plugin-visualizer' +// 压缩 +import viteCompression from 'vite-plugin-compression' import { configHtmlPlugin } from './html' import { configMockPlugin } from './mock' @@ -26,6 +28,10 @@ export function createVitePlugins(viteEnv, isBuild) { plugins.push(configMockPlugin(isBuild)) } + if (viteEnv.VITE_USE_COMPRESS) { + plugins.push(viteCompression({ algorithm: viteEnv.VITE_COMPRESS_TYPE || 'gzip' })) + } + if (isBuild) { plugins.push( visualizer({ diff --git a/package.json b/package.json index 5d9671e..44c4698 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "unplugin-icons": "^0.14.1", "unplugin-vue-components": "^0.17.21", "vite": "^2.9.9", + "vite-plugin-compression": "^0.5.1", "vite-plugin-html": "^3.2.0", "vite-plugin-mock": "^2.9.6", "vite-plugin-vue-setup-extend-plus": "^0.1.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8fdd8c7..5958904 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,7 @@ specifiers: unplugin-icons: ^0.14.1 unplugin-vue-components: ^0.17.21 vite: ^2.9.9 + vite-plugin-compression: ^0.5.1 vite-plugin-html: ^3.2.0 vite-plugin-mock: ^2.9.6 vite-plugin-vue-setup-extend-plus: ^0.1.0 @@ -80,6 +81,7 @@ devDependencies: unplugin-icons: 0.14.1_jyihscimozjyoh3ugo77i7phoi unplugin-vue-components: 0.17.21_vite@2.9.9+vue@3.2.31 vite: 2.9.9_sass@1.49.10 + vite-plugin-compression: 0.5.1_vite@2.9.9 vite-plugin-html: 3.2.0_vite@2.9.9 vite-plugin-mock: 2.9.6_mockjs@1.1.0+vite@2.9.9 vite-plugin-vue-setup-extend-plus: 0.1.0 @@ -4283,6 +4285,19 @@ packages: vue: 3.2.31 dev: true + /vite-plugin-compression/0.5.1_vite@2.9.9: + resolution: {integrity: sha512-5QJKBDc+gNYVqL/skgFAP81Yuzo9R+EAf19d+EtsMF/i8kFUpNi3J/H01QD3Oo8zBQn+NzoCIFkpPLynoOzaJg==} + peerDependencies: + vite: '>=2.0.0' + dependencies: + chalk: 4.1.2 + debug: 4.3.4 + fs-extra: 10.0.1 + vite: 2.9.9_sass@1.49.10 + transitivePeerDependencies: + - supports-color + dev: true + /vite-plugin-html/3.2.0_vite@2.9.9: resolution: {integrity: sha512-2VLCeDiHmV/BqqNn5h2V+4280KRgQzCFN47cst3WiNK848klESPQnzuC3okH5XHtgwHH/6s1Ho/YV6yIO0pgoQ==} peerDependencies: