Commit f7499cdb0d5a76a1ee0fbecc9cf89d784b7a494a
Committed by
GitHub
1 parent
ee4829c1
chore: optimize prod configuration to control whether to delete origin files whe…
…n using compress plugin (#443)
Showing
4 changed files
with
20 additions
and
3 deletions
.env.production
... | ... | @@ -12,6 +12,9 @@ VITE_DROP_CONSOLE = true |
12 | 12 | # If you need multiple forms, you can use `,` to separate |
13 | 13 | VITE_BUILD_COMPRESS = 'none' |
14 | 14 | |
15 | +# Whether to delete origin files when using compress, default false | |
16 | +VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = true | |
17 | + | |
15 | 18 | # Basic interface address SPA |
16 | 19 | VITE_GLOB_API_URL=/basic-api |
17 | 20 | ... | ... |
build/vite/plugin/compress.ts
... | ... | @@ -6,7 +6,10 @@ import type { Plugin } from 'vite'; |
6 | 6 | |
7 | 7 | import compressPlugin from 'vite-plugin-compression'; |
8 | 8 | |
9 | -export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plugin | Plugin[] { | |
9 | +export function configCompressPlugin( | |
10 | + compress: 'gzip' | 'brotli' | 'none', | |
11 | + deleteOriginFile: boolean = false | |
12 | +): Plugin | Plugin[] { | |
10 | 13 | const compressList = compress.split(','); |
11 | 14 | |
12 | 15 | const plugins: Plugin[] = []; |
... | ... | @@ -15,6 +18,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug |
15 | 18 | plugins.push( |
16 | 19 | compressPlugin({ |
17 | 20 | ext: '.gz', |
21 | + deleteOriginFile, | |
18 | 22 | }) |
19 | 23 | ); |
20 | 24 | } |
... | ... | @@ -23,6 +27,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug |
23 | 27 | compressPlugin({ |
24 | 28 | ext: '.br', |
25 | 29 | algorithm: 'brotliCompress', |
30 | + deleteOriginFile, | |
26 | 31 | }) |
27 | 32 | ); |
28 | 33 | } | ... | ... |
build/vite/plugin/index.ts
... | ... | @@ -19,7 +19,13 @@ import { configSvgIconsPlugin } from './svgSprite'; |
19 | 19 | import { configHmrPlugin } from './hmr'; |
20 | 20 | |
21 | 21 | export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { |
22 | - const { VITE_USE_IMAGEMIN, VITE_USE_MOCK, VITE_LEGACY, VITE_BUILD_COMPRESS } = viteEnv; | |
22 | + const { | |
23 | + VITE_USE_IMAGEMIN, | |
24 | + VITE_USE_MOCK, | |
25 | + VITE_LEGACY, | |
26 | + VITE_BUILD_COMPRESS, | |
27 | + VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE, | |
28 | + } = viteEnv; | |
23 | 29 | |
24 | 30 | const vitePlugins: (Plugin | Plugin[])[] = [ |
25 | 31 | // have to |
... | ... | @@ -64,7 +70,9 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { |
64 | 70 | VITE_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin()); |
65 | 71 | |
66 | 72 | // rollup-plugin-gzip |
67 | - vitePlugins.push(configCompressPlugin(VITE_BUILD_COMPRESS)); | |
73 | + vitePlugins.push( | |
74 | + configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE) | |
75 | + ); | |
68 | 76 | |
69 | 77 | // vite-plugin-pwa |
70 | 78 | vitePlugins.push(configPwaConfig(viteEnv)); | ... | ... |
types/global.d.ts
... | ... | @@ -65,6 +65,7 @@ declare global { |
65 | 65 | VITE_USE_CDN: boolean; |
66 | 66 | VITE_DROP_CONSOLE: boolean; |
67 | 67 | VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none'; |
68 | + VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE: boolean; | |
68 | 69 | VITE_LEGACY: boolean; |
69 | 70 | VITE_USE_IMAGEMIN: boolean; |
70 | 71 | VITE_GENERATE_UI: string; | ... | ... |