utils.ts 3.1 KB

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