Skip to content

Commit

Permalink
vite-plugin-compression2 替换 vite-plugin-compression
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Jan 13, 2024
1 parent 738c74d commit 2e34d88
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 7 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 20 additions & 21 deletions vite/plugins/compression.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion vite/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e34d88

Please sign in to comment.