diff --git a/.env b/.env index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..f5fa246e971c101a4cdd4808163e8464a63470b3 100644 --- a/.env +++ b/.env @@ -0,0 +1,8 @@ +# port +VITE_PORT = 3100 + +# spa-title +VITE_GLOB_APP_TITLE = vben admin 2.0 + +# spa shortname +VITE_GLOB_APP_SHORT_NAME = vue_vben_admin_2x diff --git a/.env.development b/.env.development index 6727fa1aa8a0751bb6e7d7f55631ff84bc76452d..1dfb16a49176da8696f5e537687c0d5ba692b685 100644 --- a/.env.development +++ b/.env.development @@ -1 +1,14 @@ -VITE_USE_MOCK=true +# Whether to open mock +VITE_USE_MOCK = true + +# public path +VITE_PUBLIC_PATH = / + +# Basic interface address SPA +VITE_GLOB_API_URL=/api + +# Interface prefix +VITE_GLOB_API_URL_PREFIX= + +# Cross-domain proxy, you can configure multiple +VITE_PROXY=[["/api","http://localhost:3000"]] diff --git a/.env.production b/.env.production index 6727fa1aa8a0751bb6e7d7f55631ff84bc76452d..57c73d26d46a6c79b25ea76aeed20641fc515c67 100644 --- a/.env.production +++ b/.env.production @@ -1 +1,11 @@ -VITE_USE_MOCK=true +# Whether to open mock +VITE_USE_MOCK = true + +# public path +VITE_PUBLIC_PATH = ./ + +# Basic interface address SPA +VITE_GLOB_API_URL=/api + +# Interface prefix +VITE_GLOB_API_URL_PREFIX= diff --git a/.vscode/extensions.json b/.vscode/extensions.json index b61bb706b2f0e57aa319bf359c8273a311c60246..c67be3d58de3bf4513579384b05249cf3795663a 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,12 +1,16 @@ { "recommendations": [ "octref.vetur", - "dariofuzinato.vue-peek", "dbaeumer.vscode-eslint", "stylelint.vscode-stylelint", "DavidAnson.vscode-markdownlint", "esbenp.prettier-vscode", "mrmlnc.vscode-scss", - "Orta.vscode-jest" + "mrmlnc.vscode-less", + "cpylua.language-postcss", + "Orta.vscode-jest", + "antfu.iconify", + "mikestead.dotenv", + "bradlc.vscode-tailwindcss" ] } diff --git a/README.md b/README.md index 06242d0cc4954ebad84cfc16c74a545563e6d219..21f77e445b2b8bb26ecafef019adff243d8cf4ea 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ git clone https://github.com/anncwb/vue-vben-admin.git vue-vben-admin-2.0 cd vue-vben-admin-2.0 -// 如果使用别的包,可以执行安装 +// 如果使用别的包管理工具,可以自行安装 // 如果未安装yarn,请运行:npm install -g yarn yarn install @@ -174,7 +174,7 @@ yarn clean:lib # 删除node_modules,兼容window系统 - `workflow` 工作流改进 - `ci` 持续集成 - `types` 类型定义文件更改 - - `wip` 删除文件 + - `wip` 开发中 ## 代码贡献 @@ -212,10 +212,8 @@ yarn clean:lib # 删除node_modules,兼容window系统 - [x] 二维码插件 - [x] 国际化插件 - [x] 详情组件 -- [x] 图片裁剪 - [x] 验证码/验证组件 - [x] 树组件 -- [x] 系统性能优化 - [x] 兼容最新`vuex`,`vue-router` - [x] 图片预览组件 - [x] 表格组件 @@ -234,6 +232,7 @@ yarn clean:lib # 删除node_modules,兼容window系统 - [ ] 黑暗主题 - [ ] 打包 Gzip - [ ] 抽取生产环境配置文件 +- [ ] 系统性能优化 更多组件/功能/建议/bug/欢迎提交 pr 或者 issue diff --git a/build/utils.ts b/build/utils.ts index eaa0cd04d403e612c059383d34ce7ea2ac564c64..a210dc04cc2c2870f83e63c76e5d874fa4ad58df 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -1,7 +1,6 @@ import fs from 'fs'; import { networkInterfaces } from 'os'; import dotenv from 'dotenv'; - export const isFunction = (arg: unknown): arg is (...args: any[]) => any => typeof arg === 'function'; export const isRegExp = (arg: unknown): arg is RegExp => @@ -68,7 +67,14 @@ export function isReportMode(): boolean { return process.env.REPORT === 'true'; } -export function loadEnv() { +export interface ViteEnv { + VITE_PORT: number; + VITE_USE_MOCK: boolean; + VITE_PUBLIC_PATH: string; + VITE_PROXY: [string, string][]; +} + +export function loadEnv(): ViteEnv { const env = process.env.NODE_ENV; const ret: any = {}; const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,]; @@ -79,7 +85,16 @@ export function loadEnv() { }); for (const envName of Object.keys(process.env)) { - const realName = (process.env as any)[envName].replace(/\\n/g, '\n'); + let realName = (process.env as any)[envName].replace(/\\n/g, '\n'); + realName = realName === 'true' ? true : realName === 'false' ? false : realName; + if (envName === 'VITE_PORT') { + realName = Number(realName); + } + if (envName === 'VITE_PROXY') { + try { + realName = JSON.parse(realName); + } catch (error) {} + } ret[envName] = realName; process.env[envName] = realName; } diff --git a/getEnvConfig.ts b/getEnvConfig.ts new file mode 100644 index 0000000000000000000000000000000000000000..5da9a57d878c819119f91f34525e8b808a13fa4e --- /dev/null +++ b/getEnvConfig.ts @@ -0,0 +1,6 @@ +import type { GlobEnvConfig } from './src/types/config'; + +export const getGlobEnvConfig = (): GlobEnvConfig => { + const env = import.meta.env; + return (env as unknown) as GlobEnvConfig; +}; diff --git a/package.json b/package.json index c17093cff43f17ae89bd0d547da060f23a49c10f..44b56a8549bd038b1ef3facaae1a9f78342a5352 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "stylelint-config-prettier": "^8.0.2", "stylelint-config-standard": "^20.0.0", "stylelint-order": "^4.1.0", - "tailwindcss": "^1.8.10", + "tailwindcss": "^1.8.13", "tasksfile": "^5.1.1", "ts-node": "^9.0.0", "typescript": "^4.0.3", diff --git a/src/hooks/core/useSetting.ts b/src/hooks/core/useSetting.ts index 3b53e93d00e944bc9e700ce8863c099845322f9c..845e100241b0edf673843309ec2f803e6451b5b9 100644 --- a/src/hooks/core/useSetting.ts +++ b/src/hooks/core/useSetting.ts @@ -2,13 +2,21 @@ import type { ProjectConfig, GlobConfig, SettingWrap } from '/@/types/config'; import getProjectSetting from '/@/settings/projectSetting'; +import { getGlobEnvConfig } from '../../../getEnvConfig'; +const { + VITE_GLOB_API_URL, + VITE_GLOB_APP_SHORT_NAME, + VITE_GLOB_APP_TITLE, + VITE_GLOB_API_URL_PREFIX, +} = getGlobEnvConfig(); + export const useSetting = (): SettingWrap => { // Take global configuration const glob: Readonly = { - title: 'vben admin 2.0', - apiUrl: '/api', - shortName: 'vben_admin_v2', - urlPrefix: '', + title: VITE_GLOB_APP_TITLE, + apiUrl: VITE_GLOB_API_URL, + shortName: VITE_GLOB_APP_SHORT_NAME, + urlPrefix: VITE_GLOB_API_URL_PREFIX, }; const projectSetting: Readonly = getProjectSetting; diff --git a/src/types/config.d.ts b/src/types/config.d.ts index 9f0c33ca5fd55b9255bca904db86ad9315e77ab7..296a9e2e7f415e2aab81136455208722d84e5b7f 100644 --- a/src/types/config.d.ts +++ b/src/types/config.d.ts @@ -117,14 +117,14 @@ export interface GlobConfig { } export interface GlobEnvConfig { // 网站标题 - GLOB_APP_TITLE: string; + VITE_GLOB_APP_TITLE: string; // 项目路径 - GLOB_API_URL: string; + VITE_GLOB_API_URL: string; - GLOB_API_URL_PREFIX?: string; + VITE_GLOB_API_URL_PREFIX?: string; - GLOB_APP_SHORT_NAME: string; + VITE_GLOB_APP_SHORT_NAME: string; } // 修改配置 diff --git a/vite.config.ts b/vite.config.ts index 0966eb1b3d068107a161f7b474f11037caace3ba..f75f86ddf12a40f61a00e6bac2a7ebbf65d571c2 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,6 @@ import { resolve } from 'path'; import type { UserConfig, Plugin as VitePlugin } from 'vite'; -import type { Plugin } from 'rollup'; import visualizer from 'rollup-plugin-visualizer'; import { modifyVars } from './build/config/glob/lessModifyVars'; @@ -12,12 +11,13 @@ import PurgeIcons from 'vite-plugin-purge-icons'; import { isDevFn, isReportMode, isProdFn, loadEnv } from './build/utils'; setupBasicEnv(); -const { VITE_USE_MOCK } = loadEnv(); +const { VITE_USE_MOCK, VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = loadEnv(); function pathResolve(dir: string) { return resolve(__dirname, '.', dir); } -const rollupPlugins: Plugin[] = []; + +const rollupPlugins: any[] = []; const vitePlugins: VitePlugin[] = []; (() => { @@ -27,7 +27,7 @@ const vitePlugins: VitePlugin[] = []; visualizer({ filename: './node_modules/.cache/stats.html', open: true }) as Plugin ); } - if (isDevFn() && VITE_USE_MOCK === 'true') { + if (isDevFn() && VITE_USE_MOCK) { // open mock vitePlugins.push( createMockServer({ @@ -39,17 +39,16 @@ const vitePlugins: VitePlugin[] = []; })(); const viteConfig: UserConfig = { - silent: false, + /** + * 端口号 + * @default '3000' + */ + port: VITE_PORT, /** * 服务地址 * @default 'localhost' */ hostname: 'localhost', - /** - * 端口号 - * @default '3000' - */ - port: 3100, /** * 运行自动打开浏览器· * @default 'false' @@ -62,10 +61,10 @@ const viteConfig: UserConfig = { */ minify: isDevFn() ? false : 'terser', /** - * 在生产中投放时提供基本公共路径 + * 基本公共路径 * @default '/' */ - base: isDevFn() ? '/' : './', + base: VITE_PUBLIC_PATH, /** * 打包输入路径 @@ -91,7 +90,7 @@ const viteConfig: UserConfig = { * @default 'es2019' */ esbuildTarget: 'es2019', - + silent: false, // 别名 alias: { '/@/': pathResolve('src'), @@ -112,7 +111,7 @@ const viteConfig: UserConfig = { include: ['ant-design-vue/es/locale/zh_CN', '@ant-design/icons-vue', 'moment/locale/zh-cn'], }, // 本地跨域代理 - proxy: createProxy([['/api', 'http://localhost:3000']]), + proxy: createProxy(VITE_PROXY), plugins: [PurgeIcons(), ...vitePlugins], rollupOutputOptions: {}, diff --git a/yarn.lock b/yarn.lock index c6d2c6591d5afdcab2cfb69a0ee283c2347ac087..93f0f9f0eb90cee6bd826c435c06c5f241132139 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6478,10 +6478,10 @@ table@^6.0.1: slice-ansi "^4.0.0" string-width "^4.2.0" -tailwindcss@^1.8.10: - version "1.8.10" - resolved "https://registry.npm.taobao.org/tailwindcss/download/tailwindcss-1.8.10.tgz#945ef151c401c04a1c95e6a6bc747387a8d1b9dc" - integrity sha1-lF7xUcQBwEocleamvHRzh6jRudw= +tailwindcss@^1.8.13: + version "1.8.13" + resolved "https://registry.npmjs.org/tailwindcss/-/tailwindcss-1.8.13.tgz#ee57050a516d342bafc92cb74b4de6f92e44c189" + integrity sha512-z3R/6qPqfjauSR4qHhlA8I0OnfSyuotvigXISq666k+V52VSs5HV//KZ0Xe3qrZ4h5Um4OG5g+lcgjXSfURjDw== dependencies: "@fullhuman/postcss-purgecss" "^2.1.2" autoprefixer "^9.4.5"