perf: 优化env字段

This commit is contained in:
vben 2023-04-05 15:58:03 +08:00
parent 358b77fe96
commit aedb8e53aa
10 changed files with 38 additions and 54 deletions

3
.env
View File

@ -1,5 +1,2 @@
# spa-title
VITE_GLOB_APP_TITLE = Vben Admin
# spa shortname
VITE_GLOB_APP_SHORT_NAME = vue_vben_admin

View File

@ -31,6 +31,7 @@
"stub": "pnpm unbuild --stub"
},
"dependencies": {
"@ant-design/colors": "^7.0.0",
"vite": "^4.3.0-beta.1"
},
"devDependencies": {

View File

@ -74,7 +74,7 @@ function defineApplicationConfig(defineOptions: DefineOptions = {}) {
output: {
manualChunks: {
vue: ['vue', 'pinia', 'vue-router'],
antdv: ['ant-design-vue', '@ant-design/icons-vue'],
antd: ['ant-design-vue', '@ant-design/icons-vue'],
},
},
},

View File

@ -26,7 +26,8 @@ async function createAppConfigPlugin({
return {
name: PLUGIN_NAME,
async configResolved(_config) {
const appTitle = _config?.env?.VITE_GLOB_APP_SHORT_NAME ?? '';
let appTitle = _config?.env?.VITE_GLOB_APP_TITLE ?? '';
appTitle = appTitle.replace(/\s/g, '_');
publicPath = _config.base;
source = await getConfigSource(appTitle);
},

View File

@ -66,13 +66,10 @@
}
},
"dependencies": {
"@ant-design/colors": "^7.0.0",
"@ant-design/icons-vue": "^6.1.0",
"@iconify/iconify": "^3.1.0",
"@logicflow/core": "^1.2.1",
"@logicflow/extension": "^1.2.1",
"@vue/runtime-core": "^3.2.47",
"@vue/shared": "^3.2.47",
"@vueuse/core": "^9.13.0",
"@vueuse/shared": "^9.13.0",
"@zxcvbn-ts/core": "^2.2.1",
@ -111,6 +108,7 @@
},
"devDependencies": {
"@commitlint/cli": "^17.5.1",
"@commitlint/config-conventional": "^17.4.4",
"@iconify/json": "^2.2.45",
"@purge-icons/generated": "^0.9.0",
"@types/codemirror": "^5.60.7",

31
pnpm-lock.yaml generated
View File

@ -4,9 +4,6 @@ importers:
.:
dependencies:
'@ant-design/colors':
specifier: ^7.0.0
version: 7.0.0
'@ant-design/icons-vue':
specifier: ^6.1.0
version: 6.1.0(vue@3.2.47)
@ -19,12 +16,6 @@ importers:
'@logicflow/extension':
specifier: ^1.2.1
version: 1.2.1
'@vue/runtime-core':
specifier: ^3.2.47
version: 3.2.47
'@vue/shared':
specifier: ^3.2.47
version: 3.2.47
'@vueuse/core':
specifier: ^9.13.0
version: 9.13.0(vue@3.2.47)
@ -134,6 +125,9 @@ importers:
'@commitlint/cli':
specifier: ^17.5.1
version: 17.5.1
'@commitlint/config-conventional':
specifier: ^17.4.4
version: 17.4.4
'@iconify/json':
specifier: ^2.2.45
version: 2.2.45
@ -378,6 +372,9 @@ importers:
internal/vite-config:
dependencies:
'@ant-design/colors':
specifier: ^7.0.0
version: 7.0.0
vite:
specifier: ^4.3.0-beta.1
version: 4.3.0-beta.1(@types/node@18.15.11)(less@4.1.3)(sass@1.60.0)
@ -791,6 +788,13 @@ packages:
- '@swc/wasm'
dev: true
/@commitlint/config-conventional@17.4.4:
resolution: {integrity: sha512-u6ztvxqzi6NuhrcEDR7a+z0yrh11elY66nRrQIpqsqW6sZmpxYkDLtpRH8jRML+mmxYQ8s4qqF06Q/IQx5aJeQ==}
engines: {node: '>=v14'}
dependencies:
conventional-changelog-conventionalcommits: 5.0.0
dev: true
/@commitlint/config-validator@17.4.4:
resolution: {integrity: sha512-bi0+TstqMiqoBAQDvdEP4AFh0GaKyLFlPPEObgI29utoKEYoPQTvF0EYqIwYYLEoJYhj5GfMIhPHJkTJhagfeg==}
engines: {node: '>=v14'}
@ -3176,6 +3180,15 @@ packages:
q: 1.5.1
dev: true
/conventional-changelog-conventionalcommits@5.0.0:
resolution: {integrity: sha512-lCDbA+ZqVFQGUj7h9QBKoIpLhl8iihkO0nCTyRNzuXtcd7ubODpYB04IFy31JloiJgG0Uovu8ot8oxRzn7Nwtw==}
engines: {node: '>=10'}
dependencies:
compare-func: 2.0.0
lodash: 4.17.21
q: 1.5.1
dev: true
/conventional-commits-parser@3.2.4:
resolution: {integrity: sha512-nK7sAtfi+QXbxHCYfhpZsfRtaitZLIA6889kFIouLvz6repszQDgxBu7wf2WbU+Dco7sAnNCJYERCwt54WPC2Q==}
engines: {node: '>=10'}

View File

@ -1,28 +1,16 @@
import type { GlobConfig } from '/#/config';
import { warn } from '/@/utils/log';
import { getAppEnvConfig } from '/@/utils/env';
export const useGlobSetting = (): Readonly<GlobConfig> => {
const {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_APP_SHORT_NAME,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
} = getAppEnvConfig();
if (!/[a-zA-Z_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
warn(
`VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`,
);
}
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
getAppEnvConfig();
// Take global configuration
const glob: Readonly<GlobConfig> = {
title: VITE_GLOB_APP_TITLE,
apiUrl: VITE_GLOB_API_URL,
shortName: VITE_GLOB_APP_SHORT_NAME,
shortName: VITE_GLOB_APP_TITLE.replace(/\s/g, '_'),
urlPrefix: VITE_GLOB_API_URL_PREFIX,
uploadUrl: VITE_GLOB_UPLOAD_URL,
};

View File

@ -1,15 +1,16 @@
import type { GlobEnvConfig } from '/#/config';
import { warn } from '/@/utils/log';
import pkg from '../../package.json';
const getVariableName = (title: string) => {
return `__PRODUCTION__${title || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
return `__PRODUCTION__${title.replace(/\s/g, '_') || '__APP'}__CONF__`
.toUpperCase()
.replace(/\s/g, '');
};
export function getCommonStoragePrefix() {
const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
}
// Generate cache key according to version
@ -18,31 +19,19 @@ export function getStorageShortName() {
}
export function getAppEnvConfig() {
const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_SHORT_NAME);
const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE);
const ENV = (import.meta.env.DEV
? // Get the global configuration (the configuration will be extracted independently when packaging)
(import.meta.env as unknown as GlobEnvConfig)
: window[ENV_NAME as any]) as unknown as GlobEnvConfig;
const {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_APP_SHORT_NAME,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
} = ENV;
if (!/^[a-zA-Z_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
warn(
`VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`,
);
}
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
ENV;
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_APP_SHORT_NAME,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
};

2
types/config.d.ts vendored
View File

@ -155,8 +155,6 @@ export interface GlobEnvConfig {
VITE_GLOB_API_URL: string;
// Service interface url prefix
VITE_GLOB_API_URL_PREFIX?: string;
// Project abbreviation
VITE_GLOB_APP_SHORT_NAME: string;
// Upload url
VITE_GLOB_UPLOAD_URL?: string;
}

1
types/global.d.ts vendored
View File

@ -61,7 +61,6 @@ declare global {
VITE_PUBLIC_PATH: string;
VITE_PROXY: [string, string][];
VITE_GLOB_APP_TITLE: string;
VITE_GLOB_APP_SHORT_NAME: string;
VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none';
}