feat: 整合Vite 环境变量和反向代理

This commit is contained in:
ray
2024-09-23 18:23:10 +08:00
parent 737f6a3ee5
commit 272d643744
16 changed files with 5187 additions and 6476 deletions

10
.env.development Normal file
View File

@@ -0,0 +1,10 @@
# 变量必须以 VITE_ 为前缀才能暴露给外部读取
# 项目运行的端口号
VITE_APP_PORT = 5173
# API 基础路径,开发环境下的请求前缀
VITE_APP_BASE_API = '/dev-api'
# API 服务器的 URL
VITE_APP_API_URL = https://api.youlai.tech

View File

@@ -1 +1 @@
#npx --no-install commitlint --edit $1
npx --no-install commitlint --edit $1

View File

@@ -1,2 +1,2 @@
echo "Running pre-commit hook..."
#pnpm run lint:lint-staged
pnpm run lint:lint-staged

View File

@@ -3,10 +3,33 @@ import pluginJs from "@eslint/js"; // JavaScript 的推荐配置
import tseslint from "typescript-eslint"; // TypeScript 配置
import pluginVue from "eslint-plugin-vue"; // Vue 配置
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, resolve } from "path";
// 动态读取 .eslintrc-auto-import.json 文件内容
const autoImportConfig = JSON.parse(
readFileSync(
resolve(dirname(fileURLToPath(import.meta.url)), ".eslintrc-auto-import.json"),
"utf-8",
),
);
export default [
{ extends: ["./.eslintrc-auto-import.json"] },
{ files: ["**/*.{js,mjs,cjs,ts,vue}"] }, // 校验的文件类型
{ languageOptions: { globals: globals.browser } }, // 设置浏览器环境的全局变量
{
// 语言选项配置,定义全局变量
languageOptions: {
globals: {
...globals.browser, // 浏览器变量 (window, document 等)
...globals.node, // Node.js 变量 (process, require 等)
...autoImportConfig.globals, // 自动导入的全局变量
...{
uni: "readonly", // uni-app 全局对象
},
},
},
},
pluginJs.configs.recommended, // JavaScript 推荐配置
...tseslint.configs.recommended, // TypeScript 推荐配置
...pluginVue.configs["flat/essential"], // Vue 推荐配置
@@ -36,10 +59,10 @@ export default [
"no-trailing-spaces": "error", // 不允许行尾有空格
// TypeScript 规则
"@typescript-eslint/ban-types": "off", // 禁用 ban-types 规则
"@typescript-eslint/no-empty-interface": "off", // 禁用 no-empty-interface 规则,允许空接口声明
"@typescript-eslint/no-explicit-any": "off", // 禁用 no-explicit-any 规则,允许使用 any 类型
"@typescript-eslint/explicit-function-return-type": "off", // 不强制要求函数必须明确返回类型
"@typescript-eslint/no-empty-interface": "off", // 禁用 no-empty-interface 规则,允许空接口声明
"@typescript-eslint/no-empty-object-type": "off", // 允许空对象类型
// Vue 规则
"vue/multi-word-component-names": "off", // 关闭多单词组件名称的限制

View File

@@ -41,7 +41,7 @@
"build:quickapp-webview-huawei": "uni build -p quickapp-webview-huawei",
"build:quickapp-webview-union": "uni build -p quickapp-webview-union",
"type-check": "vue-tsc --noEmit",
"lint:eslint": "eslint --fix ./src/App.vue",
"lint:eslint": "eslint --fix ./src",
"lint:stylelint": "stylelint \"**/*.{css,scss,vue,html}\" --fix",
"lint:lint-staged": "lint-staged",
"prepare": "husky",
@@ -130,6 +130,5 @@
"unplugin-auto-import": "^0.18.3",
"vite": "5.2.8",
"vue-tsc": "^1.0.24"
},
"packageManager": "pnpm@9.1.3+sha512.7c2ea089e1a6af306409c4fc8c4f0897bdac32b772016196c469d9428f1fe2d5a21daf8ad6512762654ac645b5d9136bb210ec9a00afa8dbc4677843ba362ecd"
}
}

11547
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

6
src/env.d.ts vendored
View File

@@ -1,7 +1,7 @@
/// <reference types="vite/client" />
declare module "*.vue" {
import { DefineComponent } from "vue"
const component: DefineComponent<{}, {}, any>
export default component
import { DefineComponent } from "vue";
const component: DefineComponent<{}, {}, any>;
export default component;
}

View File

@@ -51,8 +51,8 @@
{
"pagePath": "pages/profile/index",
"text": "我的",
"iconPath": "static/tabbar/my.png",
"selectedIconPath": "static/tabbar/my-active.png"
"iconPath": "static/tabbar/profile.png",
"selectedIconPath": "static/tabbar/profile-active.png"
}
]
}

View File

@@ -20,7 +20,7 @@ const userStore = useUserStore();
const handleLogin = async () => {
await userStore.login(username.value, password.value);
if (!!userStore.token) {
if (userStore.token) {
await userStore.getUserInfo(); // 登录成功后获取用户信息
uni.showToast({ title: "登录成功", icon: "success" });
uni.navigateBack(); // 登录成功后返回上一页

4
src/shime-uni.d.ts vendored
View File

@@ -1,6 +1,6 @@
export {}
export {};
declare module "vue" {
type Hooks = App.AppInstance & Page.PageInstance;
interface ComponentCustomOptions extends Hooks {}
}
}

View File

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

19
src/types/env.d.ts vendored Normal file
View File

@@ -0,0 +1,19 @@
// src/types/env.d.ts
interface ImportMetaEnv {
/**
* 应用端口
*/
VITE_APP_PORT: number;
/**
* API 基础路径
*/
VITE_APP_BASE_API: string;
/**
* API 服务器的 URL
*/
VITE_APP_API_URL: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}

View File

@@ -1,12 +1,10 @@
const API_HOST = "https://api.youlai.tech";
export default function request<T>(options: UniApp.RequestOptions): Promise<T> {
const token = uni.getStorageSync("token"); // 从本地缓存获取 token
return new Promise((resolve, reject) => {
uni.request({
...options,
url: `${API_HOST}${options.url}`,
url: `${import.meta.env.VITE_APP_BASE_API}${options.url}`,
header: {
...options.header,
Authorization: token,

View File

@@ -1,5 +1,12 @@
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"target": "esnext",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"sourceMap": true,
"baseUrl": ".",
"paths": {
@@ -8,5 +15,6 @@
"lib": ["esnext", "dom"],
"types": ["@dcloudio/types"]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"exclude": ["node_modules", "dist"]
}

View File

@@ -1,11 +1,24 @@
import { defineConfig } from "vite";
import { defineConfig, UserConfig, ConfigEnv, loadEnv } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from "unplugin-auto-import/vite";
export default defineConfig(async () => {
export default defineConfig(async ({ mode }: ConfigEnv): Promise<UserConfig> => {
const UnoCss = await import("unocss/vite").then((i) => i.default);
const env = loadEnv(mode, process.cwd());
return {
server: {
host: "0.0.0.0",
port: +env.VITE_APP_PORT,
open: true,
proxy: {
[env.VITE_APP_BASE_API]: {
changeOrigin: true,
target: env.VITE_APP_API_URL,
rewrite: (path) => path.replace(new RegExp("^" + env.VITE_APP_BASE_API), ""),
},
},
},
plugins: [
uni(),