application.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import type { UserConfig } from 'vite';
  2. import type { DefineApplicationOptions } from '../typing';
  3. import path, { relative } from 'node:path';
  4. import { findMonorepoRoot } from '@vben/node-utils';
  5. import { defineConfig, loadEnv, mergeConfig } from 'vite';
  6. import { defaultImportmapOptions, getDefaultPwaOptions } from '../options';
  7. import { loadApplicationPlugins } from '../plugins';
  8. import { loadAndConvertEnv } from '../utils/env';
  9. import { getCommonConfig } from './common';
  10. function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
  11. return defineConfig(async (config) => {
  12. const options = await userConfigPromise?.(config);
  13. const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
  14. const { command, mode } = config;
  15. const { application = {}, vite = {} } = options || {};
  16. const root = process.cwd();
  17. const isBuild = command === 'build';
  18. const env = loadEnv(mode, root);
  19. const plugins = await loadApplicationPlugins({
  20. archiver: true,
  21. archiverPluginOptions: {},
  22. compress: false,
  23. compressTypes: ['brotli', 'gzip'],
  24. devtools: true,
  25. env,
  26. extraAppConfig: true,
  27. html: true,
  28. i18n: true,
  29. importmapOptions: defaultImportmapOptions,
  30. injectAppLoading: true,
  31. injectMetadata: true,
  32. isBuild,
  33. license: true,
  34. mode,
  35. nitroMock: !isBuild,
  36. nitroMockOptions: {},
  37. print: !isBuild,
  38. printInfoMap: {
  39. 'Vben Admin Docs': 'https://doc.vben.pro',
  40. },
  41. pwa: true,
  42. pwaOptions: getDefaultPwaOptions(appTitle),
  43. vxeTableLazyImport: true,
  44. ...envConfig,
  45. ...application,
  46. });
  47. const { injectGlobalScss = true } = application;
  48. const applicationConfig: UserConfig = {
  49. base,
  50. build: {
  51. rollupOptions: {
  52. output: {
  53. assetFileNames: '[ext]/[name]-[hash].[ext]',
  54. chunkFileNames: 'js/[name]-[hash].js',
  55. entryFileNames: 'jse/index-[name]-[hash].js',
  56. },
  57. },
  58. target: 'es2015',
  59. },
  60. css: createCssOptions(injectGlobalScss),
  61. esbuild: {
  62. drop: isBuild
  63. ? [
  64. // 'console',
  65. 'debugger',
  66. ]
  67. : [],
  68. legalComments: 'none',
  69. },
  70. plugins,
  71. server: {
  72. host: true,
  73. port,
  74. warmup: {
  75. // 预热文件
  76. clientFiles: [
  77. './index.html',
  78. './src/bootstrap.ts',
  79. './src/{views,layouts,router,store,api}/*',
  80. ],
  81. },
  82. },
  83. };
  84. const mergedCommonConfig = mergeConfig(
  85. await getCommonConfig(),
  86. applicationConfig,
  87. );
  88. return mergeConfig(mergedCommonConfig, vite);
  89. });
  90. }
  91. function createCssOptions(injectGlobalScss = true) {
  92. const root = findMonorepoRoot();
  93. return {
  94. preprocessorOptions: injectGlobalScss
  95. ? {
  96. scss: {
  97. additionalData: (content: string, filepath: string) => {
  98. const relativePath = relative(root, filepath);
  99. // apps下的包注入全局样式
  100. if (relativePath.startsWith(`apps${path.sep}`)) {
  101. return `@import "@vben/styles/global";\n${content}`;
  102. }
  103. return content;
  104. },
  105. api: 'modern-compiler',
  106. },
  107. }
  108. : {},
  109. };
  110. }
  111. export { defineApplicationConfig };