From d2a22a3ee5d59affa7f3ca0a85491b4f6e87fc44 Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Wed, 22 Mar 2023 19:42:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20PWA=20=E9=BB=98=E8=AE=A4=E4=B8=8D?= =?UTF-8?q?=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 +++ src/typings/env.d.ts | 3 ++- vite.config.ts | 35 +++++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.env b/.env index 53fd4d0..54f675a 100644 --- a/.env +++ b/.env @@ -5,3 +5,6 @@ VITE_APP_API_BASE_URL=http://localhost:3002/ # Whether long replies are supported, which may result in higher API fees VITE_GLOB_OPEN_LONG_REPLY=false + +# When you want to use PWA +VITE_GLOB_APP_PWA=false diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index 54408d7..04d5999 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -2,6 +2,7 @@ interface ImportMetaEnv { readonly VITE_GLOB_API_URL: string; - readonly VITE_GLOB_API_TIMEOUT: string; readonly VITE_APP_API_BASE_URL: string; + readonly VITE_GLOB_OPEN_LONG_REPLY: string; + readonly VITE_GLOB_APP_PWA: string; } diff --git a/vite.config.ts b/vite.config.ts index 100abb8..5ce7ac4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,8 +1,28 @@ import path from 'path' +import type { PluginOption } from 'vite' import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import { VitePWA } from 'vite-plugin-pwa' +function setupPlugins(env: ImportMetaEnv): PluginOption[] { + const plugins = [vue()] + + if (env.VITE_GLOB_APP_PWA === 'true') { + VitePWA({ + injectRegister: 'auto', + manifest: { + name: 'chatGPT', + short_name: 'chatGPT', + icons: [ + { src: 'pwa-192x192.png', sizes: '192x192', type: 'image/png' }, + { src: 'pwa-512x512.png', sizes: '512x512', type: 'image/png' }, + ], + }, + }) + } + return plugins +} + export default defineConfig((env) => { const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv @@ -12,20 +32,7 @@ export default defineConfig((env) => { '@': path.resolve(process.cwd(), 'src'), }, }, - plugins: [ - vue(), - VitePWA({ - injectRegister: 'auto', - manifest: { - name: 'chatGPT', - short_name: 'chatGPT', - icons: [ - { src: 'pwa-192x192.png', sizes: '192x192', type: 'image/png' }, - { src: 'pwa-512x512.png', sizes: '512x512', type: 'image/png' }, - ], - }, - }), - ], + plugins: setupPlugins(viteEnv), server: { host: '0.0.0.0', port: 1002,