utils.ts 3.1 KB

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