Commit a4e70b9efe78ccf15c4e2d93ac194452f9a7a3e3
Committed by
GitHub
1 parent
4e3e7216
通过把名称编码成hex,使项目能够在VITE_GLOB_APP_TITLE包含特殊字符时正常打包 (#2910)
* Update env.ts 使getVariableName能处理含有特殊字符的标题 * Update appConfig.ts 是getVariableName能处理包含特殊字符的标题的情况
Showing
2 changed files
with
20 additions
and
4 deletions
internal/vite-config/src/plugins/appConfig.ts
... | ... | @@ -74,7 +74,16 @@ async function createAppConfigPlugin({ |
74 | 74 | * @param env |
75 | 75 | */ |
76 | 76 | const getVariableName = (title: string) => { |
77 | - return `__PRODUCTION__${title || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, ''); | |
77 | + function strToHex(str: string) { | |
78 | + const result: string[] = []; | |
79 | + for (let i = 0; i < str.length; ++i) { | |
80 | + const hex = str.charCodeAt(i).toString(16); | |
81 | + result.push(('000' + hex).slice(-4)); | |
82 | + } | |
83 | + return result.join('').toUpperCase(); | |
84 | + } | |
85 | + | |
86 | + return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, ''); | |
78 | 87 | }; |
79 | 88 | |
80 | 89 | async function getConfigSource(appTitle: string) { | ... | ... |
src/utils/env.ts
... | ... | @@ -2,9 +2,16 @@ import type { GlobEnvConfig } from '/#/config'; |
2 | 2 | import pkg from '../../package.json'; |
3 | 3 | |
4 | 4 | const getVariableName = (title: string) => { |
5 | - return `__PRODUCTION__${title.replace(/\s/g, '_').replace(/-/g, '_') || '__APP'}__CONF__` | |
6 | - .toUpperCase() | |
7 | - .replace(/\s/g, ''); | |
5 | + function strToHex(str: string) { | |
6 | + const result: string[] = []; | |
7 | + for (let i = 0; i < str.length; ++i) { | |
8 | + const hex = str.charCodeAt(i).toString(16); | |
9 | + result.push(('000' + hex).slice(-4)); | |
10 | + } | |
11 | + return result.join('').toUpperCase(); | |
12 | + } | |
13 | + | |
14 | + return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, ''); | |
8 | 15 | }; |
9 | 16 | |
10 | 17 | export function getCommonStoragePrefix() { | ... | ... |