From 2e34d88d94925048fe148a0a80529e3bddaa5fe5 Mon Sep 17 00:00:00 2001 From: Hooray Hu <304327508@qq.com> Date: Sun, 14 Jan 2024 00:08:44 +0800 Subject: [PATCH] =?UTF-8?q?vite-plugin-compression2=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=20vite-plugin-compression?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- pnpm-lock.yaml | 19 +++++++---------- vite/plugins/compression.ts | 41 ++++++++++++++++++------------------- vite/plugins/index.ts | 2 +- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index f85cc2e..a0f8850 100755 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "unplugin-vue-components": "^0.26.0", "vite": "^5.0.11", "vite-plugin-banner": "^0.7.1", - "vite-plugin-compression": "^0.5.1", + "vite-plugin-compression2": "^0.11.0", "vite-plugin-fake-server": "^2.1.0", "vite-plugin-pages": "^0.32.0", "vite-plugin-svg-icons": "^2.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f7c40eb..a67f9e4 100755 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -256,9 +256,9 @@ devDependencies: vite-plugin-banner: specifier: ^0.7.1 version: 0.7.1 - vite-plugin-compression: - specifier: ^0.5.1 - version: 0.5.1(vite@5.0.11) + vite-plugin-compression2: + specifier: ^0.11.0 + version: 0.11.0 vite-plugin-fake-server: specifier: ^2.1.0 version: 2.1.0 @@ -11691,17 +11691,12 @@ packages: resolution: {integrity: sha512-Bww2Xd5tOGsZ1yZ9rQiGneryvsL1u86znPrqeQjCsXPsG72pnSdV5lcQA+cy8UNDguMqyTJiCevlNUbLnT85UA==} dev: true - /vite-plugin-compression@0.5.1(vite@5.0.11): - resolution: {integrity: sha512-5QJKBDc+gNYVqL/skgFAP81Yuzo9R+EAf19d+EtsMF/i8kFUpNi3J/H01QD3Oo8zBQn+NzoCIFkpPLynoOzaJg==} - peerDependencies: - vite: '>=2.0.0' + /vite-plugin-compression2@0.11.0: + resolution: {integrity: sha512-U6oEyRXZD26BynOgD/tStNTbQOLPt96aQNj/gdJTicKVYCQCdlV7QdmSF7VEhSyjiS59pQRhiMBu/uajprxWLA==} dependencies: - chalk: 4.1.2 - debug: 4.3.4 - fs-extra: 10.1.0 - vite: 5.0.11(sass@1.69.7)(terser@5.26.0) + '@rollup/pluginutils': 5.1.0 transitivePeerDependencies: - - supports-color + - rollup dev: true /vite-plugin-fake-server@2.1.0: diff --git a/vite/plugins/compression.ts b/vite/plugins/compression.ts index 317582d..76b3c26 100755 --- a/vite/plugins/compression.ts +++ b/vite/plugins/compression.ts @@ -1,25 +1,24 @@ -import compression from 'vite-plugin-compression' +import { compression } from 'vite-plugin-compression2' +import type { PluginOption } from 'vite' -export default function createCompression(env) { - const { VITE_BUILD_COMPRESS } = env - const compressList = VITE_BUILD_COMPRESS.split(',') - const plugin: any[] = [] - if (compressList.includes('gzip')) { - plugin.push( - compression({ - ext: '.gz', - deleteOriginFile: false, - }), - ) - } - if (compressList.includes('brotli')) { - plugin.push( - compression({ - ext: '.br', - algorithm: 'brotliCompress', - deleteOriginFile: false, - }), - ) +export default function createCompression(env, isBuild) { + const plugin: (PluginOption | PluginOption[])[] = [] + if (isBuild) { + const { VITE_BUILD_COMPRESS } = env + const compressList = VITE_BUILD_COMPRESS.split(',') + if (compressList.includes('gzip')) { + plugin.push( + compression(), + ) + } + if (compressList.includes('brotli')) { + plugin.push( + compression({ + exclude: [/\.(br)$/, /\.(gz)$/], + algorithm: 'brotliCompress', + }), + ) + } } return plugin } diff --git a/vite/plugins/index.ts b/vite/plugins/index.ts index 601f3ec..4960f4f 100755 --- a/vite/plugins/index.ts +++ b/vite/plugins/index.ts @@ -37,7 +37,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) { vitePlugins.push(createMock(viteEnv, isBuild)) vitePlugins.push(createLayouts()) vitePlugins.push(createPages()) - isBuild && vitePlugins.push(...createCompression(viteEnv)) + vitePlugins.push(...createCompression(viteEnv, isBuild)) vitePlugins.push(createConsole()) vitePlugins.push(createBanner()) return vitePlugins