Skip to content

Commit 9752919

Browse files
committed
feat: 添加useGlobSetting
1 parent a1a2d79 commit 9752919

File tree

7 files changed

+63
-15
lines changed

7 files changed

+63
-15
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ VITE_GLOB_APP_TITLE = VueToimcAdmin-基于Vue3+ElementPlus的中后台管理系
88
VITE_GLOB_APP_SHORT_NAME = VueToimcAdmin
99

1010
# config file
11-
VITE_GLOB_CONFIG_FILE_NAME = "_app.config.js"
11+
VITE_APP_CONFIG_FILE_NAME = "_app.config.js"
1212

1313
# dist path
1414
VITE_APP_OUTPUT_DIR=dist

.env.production

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
VITE_USE_MOCK = true
33

44
# public path
5-
VITE_PUBLIC_PATH =/vue3-toimc-admin
5+
VITE_PUBLIC_PATH =/vue3-toimc-admin/
66

77
# Delete console
88
VITE_DROP_CONSOLE = true

build/script/buildConf.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ interface CreateConfigParams {
1313
configName: string
1414
config: any
1515
configFileName?: string
16+
outputDir?: string
1617
}
1718

1819
function createConfig(params: CreateConfigParams) {
19-
const { configName, config, configFileName } = params
20+
const { configName, config, configFileName, outputDir = 'dist' } = params
2021
try {
21-
const { VITE_APP_OUTPUT_DIR } = config as ViteEnv
2222
const windowConf = `window.${configName}`
2323
// Ensure that the variable will not be modified
2424
const configStr = `${windowConf}=${JSON.stringify(config)};
@@ -28,22 +28,24 @@ function createConfig(params: CreateConfigParams) {
2828
writable: false,
2929
});
3030
`.replace(/\s/g, '')
31-
fs.mkdirp(getRootPath(VITE_APP_OUTPUT_DIR))
32-
writeFileSync(getRootPath(`${VITE_APP_OUTPUT_DIR}/${configFileName}`), configStr)
31+
fs.mkdirp(getRootPath(outputDir))
32+
writeFileSync(getRootPath(`${outputDir}/${configFileName}`), configStr)
3333

3434
console.log(colors.cyan(`✨ [${pkg.name}]`) + ' - configuration file is build successfully:')
35-
console.log(colors.gray(VITE_APP_OUTPUT_DIR + '/' + colors.green(configFileName)) + '\n')
35+
console.log(colors.gray(outputDir + '/' + colors.green(configFileName)) + '\n')
3636
} catch (error) {
3737
console.log(colors.red('configuration file configuration file failed to package:\n' + error))
3838
}
3939
}
4040

4141
export function runBuildConfig() {
42-
const config = getEnvConfig() as ViteEnv
42+
const config = getEnvConfig()
43+
const appConfig = getEnvConfig('VITE_APP_') as ViteEnv
4344
const configFileName = getConfigFileName(config)
4445
createConfig({
4546
config,
4647
configName: configFileName,
47-
configFileName: config.VITE_GLOB_CONFIG_FILE_NAME
48+
configFileName: appConfig.VITE_APP_CONFIG_FILE_NAME,
49+
outputDir: appConfig.VITE_APP_OUTPUT_DIR
4850
})
4951
}

src/hooks/useGlobSetting.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { GlobConfig } from 'types/config'
2+
3+
import { warn } from '@/utils/log'
4+
import { getAppEnvConfig } from '@/utils/env'
5+
6+
// 获取由esno形成的全局的配置文件_app.config.js中的配置
7+
export const useGlobSetting = (): Readonly<GlobConfig> => {
8+
const {
9+
VITE_GLOB_APP_TITLE,
10+
VITE_GLOB_API_URL,
11+
VITE_GLOB_APP_SHORT_NAME,
12+
VITE_GLOB_API_URL_PREFIX,
13+
VITE_GLOB_UPLOAD_URL
14+
} = getAppEnvConfig()
15+
16+
if (!/[a-zA-Z\_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
17+
warn(
18+
'VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.'
19+
)
20+
}
21+
22+
// Take global configuration
23+
const glob: Readonly<GlobConfig> = {
24+
title: VITE_GLOB_APP_TITLE,
25+
apiUrl: VITE_GLOB_API_URL,
26+
shortName: VITE_GLOB_APP_SHORT_NAME,
27+
urlPrefix: VITE_GLOB_API_URL_PREFIX,
28+
uploadUrl: VITE_GLOB_UPLOAD_URL
29+
}
30+
return glob as Readonly<GlobConfig>
31+
}

types/config.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
export type LocaleType = 'zh-CN' | 'en'
22

3+
export interface GlobConfig {
4+
// Site title
5+
title: string
6+
// Service interface url
7+
apiUrl: string
8+
// Upload url
9+
uploadUrl?: string
10+
// Service interface url prefix
11+
urlPrefix?: string
12+
// Project abbreviation
13+
shortName: string
14+
}
15+
316
export interface LocaleSetting {
417
locale: LocaleType
518
// default language

types/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ declare global {
4848
VITE_GLOB_APP_TITLE: string
4949
VITE_GLOB_APP_SHORT_NAME: string
5050
VITE_GLOB_CONFIG_FILE_NAME: string
51+
VITE_GLOB_API_URL: string
5152
VITE_USE_CDN: boolean
5253
VITE_DROP_CONSOLE: boolean
5354
VITE_HTTPS: boolean
5455
VITE_BUILD_COMPRESS: 'gzip' | 'brotli' | 'none'
5556
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE: boolean
5657
VITE_USE_IMAGEMIN: boolean
5758
VITE_GENERATE_UI: string
59+
VITE_APP_CONFIG_FILE_NAME: string
5860
VITE_APP_OUTPUT_DIR: string
5961
}
6062

vite.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default ({ mode, command }: ConfigEnv): UserConfig => {
5353
VITE_HTTPS,
5454
VITE_USE_MOCK,
5555
VITE_GLOB_APP_TITLE,
56-
VITE_GLOB_CONFIG_FILE_NAME
56+
VITE_APP_CONFIG_FILE_NAME
5757
} = viteEnv
5858

5959
const isBuild = command === 'build'
@@ -62,7 +62,7 @@ export default ({ mode, command }: ConfigEnv): UserConfig => {
6262
const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`
6363

6464
const getAppConfigSrc = () => {
65-
return `${path || '/'}${VITE_GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`
65+
return `${path || '/'}${VITE_APP_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`
6666
}
6767

6868
return {
@@ -107,9 +107,9 @@ export default ({ mode, command }: ConfigEnv): UserConfig => {
107107
prodEnabled: VITE_USE_MOCK,
108108
// 相当于在src/main.ts中inject下面的代码,所以注意文件的路径问题
109109
injectCode: `
110-
import { setupProdMockServer } from '../mock/_createProductionServer';
111-
setupProdMockServer();
112-
`
110+
import { setupProdMockServer } from '../mock/_createProductionServer';
111+
setupProdMockServer();
112+
`
113113
}),
114114
createSvgIconsPlugin({
115115
// 指定需要缓存的图标文件夹
@@ -133,7 +133,7 @@ export default ({ mode, command }: ConfigEnv): UserConfig => {
133133
data: {
134134
title: VITE_GLOB_APP_TITLE
135135
},
136-
// Embed the generated app.config.js file
136+
// Embed the generated _app.config.js file, 使用esno编译,npm run build:post
137137
tags: isBuild
138138
? [
139139
{

0 commit comments

Comments
 (0)