application.ts 3.1 KB

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