application.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { type UserConfig, defineConfig, mergeConfig, loadEnv } from 'vite';
  2. import { resolve } from 'node:path';
  3. import { readPackageJSON } from 'pkg-types';
  4. import { generateModifyVars } from '../utils/modifyVars';
  5. import { commonConfig } from './common';
  6. import { createPlugins } from '../plugins';
  7. import dayjs from 'dayjs';
  8. interface DefineOptions {
  9. overrides?: UserConfig;
  10. options?: {};
  11. }
  12. function defineApplicationConfig(defineOptions: DefineOptions = {}) {
  13. const { overrides = {} } = defineOptions;
  14. return defineConfig(async ({ command, mode }) => {
  15. const root = process.cwd();
  16. const isBuild = command === 'build';
  17. const { VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_ENABLE_ANALYZE } = loadEnv(mode, root);
  18. const defineData = await createDefineData(root);
  19. const plugins = await createPlugins({
  20. isBuild,
  21. root,
  22. enableAnalyze: VITE_ENABLE_ANALYZE === 'true',
  23. enableMock: VITE_USE_MOCK === 'true',
  24. compress: VITE_BUILD_COMPRESS,
  25. });
  26. const pathResolve = (pathname: string) => resolve(root, '.', pathname);
  27. const applicationConfig: UserConfig = {
  28. optimizeDeps: {
  29. include: [
  30. '@iconify/iconify',
  31. 'ant-design-vue/es/locale/zh_CN',
  32. 'ant-design-vue/es/locale/en_US',
  33. ],
  34. },
  35. resolve: {
  36. alias: [
  37. {
  38. find: 'vue-i18n',
  39. replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
  40. },
  41. // /@/xxxx => src/xxxx
  42. {
  43. find: /\/@\//,
  44. replacement: pathResolve('src') + '/',
  45. },
  46. // /#/xxxx => types/xxxx
  47. {
  48. find: /\/#\//,
  49. replacement: pathResolve('types') + '/',
  50. },
  51. // @/xxxx => src/xxxx
  52. {
  53. find: /@\//,
  54. replacement: pathResolve('src') + '/',
  55. },
  56. // #/xxxx => types/xxxx
  57. {
  58. find: /#\//,
  59. replacement: pathResolve('types') + '/',
  60. },
  61. ],
  62. },
  63. define: defineData,
  64. build: {
  65. target: 'es2015',
  66. cssTarget: 'chrome80',
  67. rollupOptions: {
  68. output: {
  69. manualChunks: {
  70. vue: ['vue', 'pinia', 'vue-router'],
  71. antdv: ['ant-design-vue', '@ant-design/icons-vue'],
  72. },
  73. },
  74. },
  75. },
  76. css: {
  77. preprocessorOptions: {
  78. less: {
  79. modifyVars: generateModifyVars(),
  80. javascriptEnabled: true,
  81. },
  82. },
  83. },
  84. plugins,
  85. };
  86. const mergedConfig = mergeConfig(commonConfig, applicationConfig);
  87. return mergeConfig(mergedConfig, overrides);
  88. });
  89. }
  90. async function createDefineData(root: string) {
  91. try {
  92. const pkgJson = await readPackageJSON(root);
  93. const { dependencies, devDependencies, name, version } = pkgJson;
  94. const __APP_INFO__ = {
  95. pkg: { dependencies, devDependencies, name, version },
  96. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  97. };
  98. return {
  99. __APP_INFO__: JSON.stringify(__APP_INFO__),
  100. };
  101. } catch (error) {
  102. return {};
  103. }
  104. }
  105. export { defineApplicationConfig };