提交 | 用户 | age
|
2f6253
|
1 |
import fs from 'fs'; |
bb3b8f
|
2 |
import path from 'path'; |
2f6253
|
3 |
import dotenv from 'dotenv'; |
bb3b8f
|
4 |
import chalk from 'chalk'; |
N |
5 |
|
2f6253
|
6 |
export const isFunction = (arg: unknown): arg is (...args: any[]) => any => |
陈 |
7 |
typeof arg === 'function'; |
2f1fbf
|
8 |
|
2f6253
|
9 |
export const isRegExp = (arg: unknown): arg is RegExp => |
陈 |
10 |
Object.prototype.toString.call(arg) === '[object RegExp]'; |
ddb88a
|
11 |
|
99ac30
|
12 |
export function isDevFn(mode: string): boolean { |
bd7b53
|
13 |
return mode === 'development'; |
2f6253
|
14 |
} |
陈 |
15 |
|
99ac30
|
16 |
export function isProdFn(mode: string): boolean { |
bd7b53
|
17 |
return mode === 'production'; |
2f6253
|
18 |
} |
陈 |
19 |
|
2f1fbf
|
20 |
/** |
V |
21 |
* Whether to generate package preview |
|
22 |
*/ |
2f6253
|
23 |
export function isReportMode(): boolean { |
陈 |
24 |
return process.env.REPORT === 'true'; |
|
25 |
} |
2f1fbf
|
26 |
|
V |
27 |
/** |
|
28 |
* Whether to generate gzip for packaging |
|
29 |
*/ |
8a1bfd
|
30 |
export function isBuildGzip(): boolean { |
V |
31 |
return process.env.VITE_BUILD_GZIP === 'true'; |
|
32 |
} |
2f1fbf
|
33 |
|
e8aede
|
34 |
export interface ViteEnv { |
B |
35 |
VITE_PORT: number; |
|
36 |
VITE_USE_MOCK: boolean; |
a1b990
|
37 |
VITE_USE_PWA: boolean; |
e8aede
|
38 |
VITE_PUBLIC_PATH: string; |
B |
39 |
VITE_PROXY: [string, string][]; |
bb3b8f
|
40 |
VITE_GLOB_APP_TITLE: string; |
99ac30
|
41 |
VITE_GLOB_APP_SHORT_NAME: string; |
bb3b8f
|
42 |
VITE_USE_CDN: boolean; |
8a1bfd
|
43 |
VITE_DROP_CONSOLE: boolean; |
V |
44 |
VITE_BUILD_GZIP: boolean; |
6f8d75
|
45 |
VITE_DYNAMIC_IMPORT: boolean; |
99ac30
|
46 |
VITE_LEGACY: boolean; |
e8aede
|
47 |
} |
B |
48 |
|
2f1fbf
|
49 |
// Read all environment variable configuration files to process.env |
bd7b53
|
50 |
export function wrapperEnv(envConf: any): ViteEnv { |
2f6253
|
51 |
const ret: any = {}; |
陈 |
52 |
|
bd7b53
|
53 |
for (const envName of Object.keys(envConf)) { |
V |
54 |
let realName = envConf[envName].replace(/\\n/g, '\n'); |
e8aede
|
55 |
realName = realName === 'true' ? true : realName === 'false' ? false : realName; |
B |
56 |
if (envName === 'VITE_PORT') { |
|
57 |
realName = Number(realName); |
|
58 |
} |
|
59 |
if (envName === 'VITE_PROXY') { |
|
60 |
try { |
|
61 |
realName = JSON.parse(realName); |
|
62 |
} catch (error) {} |
|
63 |
} |
2f6253
|
64 |
ret[envName] = realName; |
陈 |
65 |
process.env[envName] = realName; |
|
66 |
} |
|
67 |
return ret; |
|
68 |
} |
bb3b8f
|
69 |
|
2f1fbf
|
70 |
/** |
V |
71 |
* Get the environment variables starting with the specified prefix |
|
72 |
* @param match prefix |
|
73 |
* @param confFiles ext |
|
74 |
*/ |
bb3b8f
|
75 |
export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) { |
N |
76 |
let envConfig = {}; |
|
77 |
confFiles.forEach((item) => { |
|
78 |
try { |
|
79 |
const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item))); |
|
80 |
|
|
81 |
envConfig = { ...envConfig, ...env }; |
|
82 |
} catch (error) {} |
|
83 |
}); |
|
84 |
Object.keys(envConfig).forEach((key) => { |
|
85 |
const reg = new RegExp(`^(${match})`); |
|
86 |
if (!reg.test(key)) { |
|
87 |
Reflect.deleteProperty(envConfig, key); |
|
88 |
} |
|
89 |
}); |
|
90 |
return envConfig; |
|
91 |
} |
|
92 |
|
31e271
|
93 |
function consoleFn(color: string, message: any) { |
bb3b8f
|
94 |
console.log( |
N |
95 |
chalk.blue.bold('**************** ') + |
31e271
|
96 |
(chalk as any)[color].bold(message) + |
bb3b8f
|
97 |
chalk.blue.bold(' ****************') |
N |
98 |
); |
31e271
|
99 |
} |
V |
100 |
|
2f1fbf
|
101 |
/** |
V |
102 |
* warnConsole |
|
103 |
* @param message |
|
104 |
*/ |
31e271
|
105 |
export function successConsole(message: any) { |
V |
106 |
consoleFn('green', '✨ ' + message); |
bb3b8f
|
107 |
} |
N |
108 |
|
2f1fbf
|
109 |
/** |
V |
110 |
* warnConsole |
|
111 |
* @param message |
|
112 |
*/ |
bb3b8f
|
113 |
export function errorConsole(message: any) { |
31e271
|
114 |
consoleFn('red', '✨ ' + message); |
bb3b8f
|
115 |
} |
N |
116 |
|
2f1fbf
|
117 |
/** |
V |
118 |
* warnConsole |
|
119 |
* @param message message |
|
120 |
*/ |
bb3b8f
|
121 |
export function warnConsole(message: any) { |
31e271
|
122 |
consoleFn('yellow', '✨ ' + message); |
bb3b8f
|
123 |
} |
N |
124 |
|
2f1fbf
|
125 |
/** |
V |
126 |
* Get user root directory |
|
127 |
* @param dir file path |
|
128 |
*/ |
bb3b8f
|
129 |
export function getCwdPath(...dir: string[]) { |
N |
130 |
return path.resolve(process.cwd(), ...dir); |
|
131 |
} |