Blame view

build/utils.ts 3.08 KB
陈文彬 authored
1
import fs from 'fs';
2
import path from 'path';
陈文彬 authored
3
import dotenv from 'dotenv';
4
5
import chalk from 'chalk';
陈文彬 authored
6
7
export const isFunction = (arg: unknown): arg is (...args: any[]) => any =>
  typeof arg === 'function';
vben authored
8
陈文彬 authored
9
10
11
export const isRegExp = (arg: unknown): arg is RegExp =>
  Object.prototype.toString.call(arg) === '[object RegExp]';
vben authored
12
export function isDevFn(mode: string): boolean {
13
  return mode === 'development';
陈文彬 authored
14
15
}
vben authored
16
export function isProdFn(mode: string): boolean {
17
  return mode === 'production';
陈文彬 authored
18
19
}
vben authored
20
21
22
/**
 * Whether to generate package preview
 */
陈文彬 authored
23
24
25
export function isReportMode(): boolean {
  return process.env.REPORT === 'true';
}
vben authored
26
27
28
29

/**
 * Whether to generate gzip for packaging
 */
vben authored
30
31
32
export function isBuildGzip(): boolean {
  return process.env.VITE_BUILD_GZIP === 'true';
}
vben authored
33
34
35
36
export interface ViteEnv {
  VITE_PORT: number;
  VITE_USE_MOCK: boolean;
vben authored
37
  VITE_USE_PWA: boolean;
38
39
  VITE_PUBLIC_PATH: string;
  VITE_PROXY: [string, string][];
40
  VITE_GLOB_APP_TITLE: string;
vben authored
41
  VITE_GLOB_APP_SHORT_NAME: string;
42
  VITE_USE_CDN: boolean;
vben authored
43
44
  VITE_DROP_CONSOLE: boolean;
  VITE_BUILD_GZIP: boolean;
vben authored
45
  VITE_DYNAMIC_IMPORT: boolean;
vben authored
46
  VITE_LEGACY: boolean;
47
48
}
vben authored
49
// Read all environment variable configuration files to process.env
50
export function wrapperEnv(envConf: any): ViteEnv {
陈文彬 authored
51
52
  const ret: any = {};
53
54
  for (const envName of Object.keys(envConf)) {
    let realName = envConf[envName].replace(/\\n/g, '\n');
55
56
57
58
59
60
61
62
63
    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) {}
    }
陈文彬 authored
64
65
66
67
68
    ret[envName] = realName;
    process.env[envName] = realName;
  }
  return ret;
}
69
vben authored
70
71
72
73
74
/**
 * Get the environment variables starting with the specified prefix
 * @param match prefix
 * @param confFiles ext
 */
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  let envConfig = {};
  confFiles.forEach((item) => {
    try {
      const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));

      envConfig = { ...envConfig, ...env };
    } catch (error) {}
  });
  Object.keys(envConfig).forEach((key) => {
    const reg = new RegExp(`^(${match})`);
    if (!reg.test(key)) {
      Reflect.deleteProperty(envConfig, key);
    }
  });
  return envConfig;
}
vben authored
93
function consoleFn(color: string, message: any) {
94
95
  console.log(
    chalk.blue.bold('****************  ') +
vben authored
96
      (chalk as any)[color].bold(message) +
97
98
99
100
      chalk.blue.bold('  ****************')
  );
}
vben authored
101
102
103
104
/**
 * warnConsole
 * @param message
 */
vben authored
105
106
107
108
export function successConsole(message: any) {
  consoleFn('green', '✨ ' + message);
}
vben authored
109
110
111
112
/**
 * warnConsole
 * @param message
 */
113
export function errorConsole(message: any) {
vben authored
114
  consoleFn('red', '✨ ' + message);
115
116
}
vben authored
117
118
119
120
/**
 * warnConsole
 * @param message message
 */
121
export function warnConsole(message: any) {
vben authored
122
  consoleFn('yellow', '✨ ' + message);
123
124
}
vben authored
125
126
127
128
/**
 * Get user root directory
 * @param dir file path
 */
129
130
131
export function getCwdPath(...dir: string[]) {
  return path.resolve(process.cwd(), ...dir);
}