Commit f7499cdb0d5a76a1ee0fbecc9cf89d784b7a494a

Authored by 舒培培
Committed by GitHub
1 parent ee4829c1

chore: optimize prod configuration to control whether to delete origin files whe…

…n using compress plugin (#443)
.env.production
@@ -12,6 +12,9 @@ VITE_DROP_CONSOLE = true @@ -12,6 +12,9 @@ VITE_DROP_CONSOLE = true
12 # If you need multiple forms, you can use `,` to separate 12 # If you need multiple forms, you can use `,` to separate
13 VITE_BUILD_COMPRESS = 'none' 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 # Basic interface address SPA 18 # Basic interface address SPA
16 VITE_GLOB_API_URL=/basic-api 19 VITE_GLOB_API_URL=/basic-api
17 20
build/vite/plugin/compress.ts
@@ -6,7 +6,10 @@ import type { Plugin } from 'vite'; @@ -6,7 +6,10 @@ import type { Plugin } from 'vite';
6 6
7 import compressPlugin from 'vite-plugin-compression'; 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 const compressList = compress.split(','); 13 const compressList = compress.split(',');
11 14
12 const plugins: Plugin[] = []; 15 const plugins: Plugin[] = [];
@@ -15,6 +18,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug @@ -15,6 +18,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug
15 plugins.push( 18 plugins.push(
16 compressPlugin({ 19 compressPlugin({
17 ext: '.gz', 20 ext: '.gz',
  21 + deleteOriginFile,
18 }) 22 })
19 ); 23 );
20 } 24 }
@@ -23,6 +27,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug @@ -23,6 +27,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug
23 compressPlugin({ 27 compressPlugin({
24 ext: '.br', 28 ext: '.br',
25 algorithm: 'brotliCompress', 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,7 +19,13 @@ import { configSvgIconsPlugin } from './svgSprite';
19 import { configHmrPlugin } from './hmr'; 19 import { configHmrPlugin } from './hmr';
20 20
21 export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { 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 const vitePlugins: (Plugin | Plugin[])[] = [ 30 const vitePlugins: (Plugin | Plugin[])[] = [
25 // have to 31 // have to
@@ -64,7 +70,9 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { @@ -64,7 +70,9 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
64 VITE_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin()); 70 VITE_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin());
65 71
66 // rollup-plugin-gzip 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 // vite-plugin-pwa 77 // vite-plugin-pwa
70 vitePlugins.push(configPwaConfig(viteEnv)); 78 vitePlugins.push(configPwaConfig(viteEnv));
types/global.d.ts
@@ -65,6 +65,7 @@ declare global { @@ -65,6 +65,7 @@ declare global {
65 VITE_USE_CDN: boolean; 65 VITE_USE_CDN: boolean;
66 VITE_DROP_CONSOLE: boolean; 66 VITE_DROP_CONSOLE: boolean;
67 VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none'; 67 VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none';
  68 + VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE: boolean;
68 VITE_LEGACY: boolean; 69 VITE_LEGACY: boolean;
69 VITE_USE_IMAGEMIN: boolean; 70 VITE_USE_IMAGEMIN: boolean;
70 VITE_GENERATE_UI: string; 71 VITE_GENERATE_UI: string;