application.ts 3.0 KB

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