From 653cfdf8ba6101953bc4363ee41aed00cff43095 Mon Sep 17 00:00:00 2001
From: pany <939630029@qq.com>
Date: Thu, 21 Apr 2022 11:24:17 +0800
Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A4=9A=E7=8E=AF=E5=A2=83=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=20&=20vite=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?=
=?UTF-8?q?=E5=A3=B0=E6=98=8E=20&=20vite=20=E9=85=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.env.development | 5 +++++
.env.production | 5 +++++
.env.staging | 5 +++++
package.json | 6 ++++--
src/App.vue | 10 +++++++++-
src/env.d.ts | 10 ++++++++--
vite.config.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 88 insertions(+), 5 deletions(-)
create mode 100644 .env.development
create mode 100644 .env.production
create mode 100644 .env.staging
diff --git a/.env.development b/.env.development
new file mode 100644
index 0000000..7bbc833
--- /dev/null
+++ b/.env.development
@@ -0,0 +1,5 @@
+# 请勿改动这一项,该项也不可以通过 import.meta.env.NODE_ENV 调用
+NODE_ENV = development
+
+# 自定义的环境变量可以修改(命名必须以 VITE_ 开头)
+VITE_BASE_API = '/mock-api/v1'
diff --git a/.env.production b/.env.production
new file mode 100644
index 0000000..35b23d5
--- /dev/null
+++ b/.env.production
@@ -0,0 +1,5 @@
+# 请勿改动这一项,该项也不可以通过 import.meta.env.NODE_ENV 调用
+NODE_ENV = production
+
+# 自定义的环境变量可以修改(命名必须以 VITE_ 开头)
+VITE_BASE_API = '/mock-api/v1'
diff --git a/.env.staging b/.env.staging
new file mode 100644
index 0000000..35b23d5
--- /dev/null
+++ b/.env.staging
@@ -0,0 +1,5 @@
+# 请勿改动这一项,该项也不可以通过 import.meta.env.NODE_ENV 调用
+NODE_ENV = production
+
+# 自定义的环境变量可以修改(命名必须以 VITE_ 开头)
+VITE_BASE_API = '/mock-api/v1'
diff --git a/package.json b/package.json
index 2c69ea5..736fc54 100644
--- a/package.json
+++ b/package.json
@@ -4,8 +4,10 @@
"version": "0.0.0",
"scripts": {
"dev": "vite",
- "build": "vue-tsc --noEmit && vite build",
- "preview": "vite preview"
+ "build:stage": "vue-tsc --noEmit && vite build --mode staging",
+ "build:prod": "vue-tsc --noEmit && vite build",
+ "preview:stage": "pnpm build:stage && vite preview",
+ "preview:prod": "pnpm build:prod && vite preview"
},
"dependencies": {
"vue": "^3.2.33"
diff --git a/src/App.vue b/src/App.vue
index 1503baf..c4c96e6 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -1,7 +1,15 @@
diff --git a/src/env.d.ts b/src/env.d.ts
index aafef95..c3fab04 100644
--- a/src/env.d.ts
+++ b/src/env.d.ts
@@ -1,8 +1,14 @@
///
-declare module '*.vue' {
- import type { DefineComponent } from 'vue'
+/** 声明自动引入的 vue 组件 */
+declare module "*.vue" {
+ import type { DefineComponent } from "vue"
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
const component: DefineComponent<{}, {}, any>
export default component
}
+
+/** 声明 vite 环境变量的类型(如果未声明则默认是 any) */
+declare interface ImportMetaEnv {
+ readonly VITE_BASE_API: string
+}
diff --git a/vite.config.ts b/vite.config.ts
index 91b3179..509e055 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -11,6 +11,58 @@ export default (env: ConfigEnv): UserConfigExport => {
"@": resolve(__dirname, "./src"),
},
},
+ server: {
+ /** 是否开启 https */
+ https: false,
+ /** host 设置为 true 才可以使用 network 的形式,以 ip 访问项目 */
+ host: true, // host: "0.0.0.0"
+ /** 端口号 */
+ port: 9999,
+ /** 是否自动打开浏览器 */
+ open: false,
+ /** 跨域设置允许 */
+ cors: true,
+ /** 如果端口已占用,直接退出 */
+ strictPort: true,
+ /** 接口代理 */
+ proxy: {
+ "/mock-api": {
+ target: "https://vue-typescript-admin-mock-server-armour.vercel.app/mock-api",
+ ws: true,
+ /** 是否允许跨域 */
+ changeOrigin: true,
+ rewrite: (path) => path.replace("/mock-api", ""),
+ },
+ },
+ },
+ build: {
+ brotliSize: false,
+ /** 消除打包大小超过 500kb 警告 */
+ chunkSizeWarningLimit: 2000,
+ /** vite 2.6.x 以上需要配置 minify: terser,terserOptions 才能生效 */
+ minify: "terser",
+ /** 在 build 代码时移除 console.log、debugger 和 注释 */
+ terserOptions: {
+ compress: {
+ drop_console: false,
+ drop_debugger: true,
+ pure_funcs: ["console.log"],
+ },
+ output: {
+ /** 删除注释 */
+ comments: false,
+ },
+ },
+ assetsDir: "static/assets",
+ /** 静态资源打包到 dist 下的不同目录 */
+ rollupOptions: {
+ output: {
+ chunkFileNames: "static/js/[name]-[hash].js",
+ entryFileNames: "static/js/[name]-[hash].js",
+ assetFileNames: "static/[ext]/[name]-[hash].[ext]",
+ },
+ },
+ },
plugins: [vue()],
}
}