Browse Source
chore: optimize prod configuration to control whether to delete origin files when using compress plugin (#443)
pull/453/head
舒培培
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
20 additions and
3 deletions
-
.env.production
-
build/vite/plugin/compress.ts
-
build/vite/plugin/index.ts
-
types/global.d.ts
|
|
|
@ -12,6 +12,9 @@ VITE_DROP_CONSOLE = true |
|
|
|
# If you need multiple forms, you can use `,` to separate |
|
|
|
VITE_BUILD_COMPRESS = 'none' |
|
|
|
|
|
|
|
# Whether to delete origin files when using compress, default false |
|
|
|
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = true |
|
|
|
|
|
|
|
# Basic interface address SPA |
|
|
|
VITE_GLOB_API_URL=/basic-api |
|
|
|
|
|
|
|
|
|
|
|
@ -6,7 +6,10 @@ import type { Plugin } from 'vite'; |
|
|
|
|
|
|
|
import compressPlugin from 'vite-plugin-compression'; |
|
|
|
|
|
|
|
export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plugin | Plugin[] { |
|
|
|
export function configCompressPlugin( |
|
|
|
compress: 'gzip' | 'brotli' | 'none', |
|
|
|
deleteOriginFile: boolean = false |
|
|
|
): Plugin | Plugin[] { |
|
|
|
const compressList = compress.split(','); |
|
|
|
|
|
|
|
const plugins: Plugin[] = []; |
|
|
|
@ -15,6 +18,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug |
|
|
|
plugins.push( |
|
|
|
compressPlugin({ |
|
|
|
ext: '.gz', |
|
|
|
deleteOriginFile, |
|
|
|
}) |
|
|
|
); |
|
|
|
} |
|
|
|
@ -23,6 +27,7 @@ export function configCompressPlugin(compress: 'gzip' | 'brotli' | 'none'): Plug |
|
|
|
compressPlugin({ |
|
|
|
ext: '.br', |
|
|
|
algorithm: 'brotliCompress', |
|
|
|
deleteOriginFile, |
|
|
|
}) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
@ -19,7 +19,13 @@ import { configSvgIconsPlugin } from './svgSprite'; |
|
|
|
import { configHmrPlugin } from './hmr'; |
|
|
|
|
|
|
|
export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { |
|
|
|
const { VITE_USE_IMAGEMIN, VITE_USE_MOCK, VITE_LEGACY, VITE_BUILD_COMPRESS } = viteEnv; |
|
|
|
const { |
|
|
|
VITE_USE_IMAGEMIN, |
|
|
|
VITE_USE_MOCK, |
|
|
|
VITE_LEGACY, |
|
|
|
VITE_BUILD_COMPRESS, |
|
|
|
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE, |
|
|
|
} = viteEnv; |
|
|
|
|
|
|
|
const vitePlugins: (Plugin | Plugin[])[] = [ |
|
|
|
// have to
|
|
|
|
@ -64,7 +70,9 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) { |
|
|
|
VITE_USE_IMAGEMIN && vitePlugins.push(configImageminPlugin()); |
|
|
|
|
|
|
|
// rollup-plugin-gzip
|
|
|
|
vitePlugins.push(configCompressPlugin(VITE_BUILD_COMPRESS)); |
|
|
|
vitePlugins.push( |
|
|
|
configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE) |
|
|
|
); |
|
|
|
|
|
|
|
// vite-plugin-pwa
|
|
|
|
vitePlugins.push(configPwaConfig(viteEnv)); |
|
|
|
|
|
|
|
@ -65,6 +65,7 @@ declare global { |
|
|
|
VITE_USE_CDN: boolean; |
|
|
|
VITE_DROP_CONSOLE: boolean; |
|
|
|
VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none'; |
|
|
|
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE: boolean; |
|
|
|
VITE_LEGACY: boolean; |
|
|
|
VITE_USE_IMAGEMIN: boolean; |
|
|
|
VITE_GENERATE_UI: string; |
|
|
|
|