application.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ...envConfig,
  44. ...application,
  45. });
  46. const { injectGlobalScss = true } = application;
  47. const applicationConfig: UserConfig = {
  48. base,
  49. build: {
  50. rollupOptions: {
  51. output: {
  52. assetFileNames: '[ext]/[name]-[hash].[ext]',
  53. chunkFileNames: 'js/[name]-[hash].mjs',
  54. entryFileNames: 'jse/index-[name]-[hash].mjs',
  55. },
  56. },
  57. target: 'es2015',
  58. },
  59. css: createCssOptions(injectGlobalScss),
  60. esbuild: {
  61. drop: isBuild
  62. ? [
  63. // 'console',
  64. 'debugger',
  65. ]
  66. : [],
  67. legalComments: 'none',
  68. },
  69. plugins,
  70. server: {
  71. host: true,
  72. port,
  73. warmup: {
  74. // 预热文件
  75. clientFiles: [
  76. './index.html',
  77. './bootstrap.ts',
  78. './src/{views,layouts,router,store,api}/*',
  79. ],
  80. },
  81. },
  82. };
  83. const mergedCommonConfig = mergeConfig(
  84. await getCommonConfig(),
  85. applicationConfig,
  86. );
  87. return mergeConfig(mergedCommonConfig, vite);
  88. });
  89. }
  90. function createCssOptions(injectGlobalScss = true) {
  91. const root = findMonorepoRoot();
  92. return {
  93. preprocessorOptions: injectGlobalScss
  94. ? {
  95. scss: {
  96. additionalData: (content: string, filepath: string) => {
  97. const relativePath = relative(root, filepath);
  98. // apps下的包注入全局样式
  99. if (relativePath.startsWith(`apps${path.sep}`)) {
  100. return `@import "@vben/styles/global";\n${content}`;
  101. }
  102. return content;
  103. },
  104. },
  105. }
  106. : {},
  107. };
  108. }
  109. export { defineApplicationConfig };