application.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import type { UserConfig } from 'vite';
  2. import type { DefineApplicationOptions } from '../typing';
  3. import { defineConfig, loadEnv, mergeConfig } from 'vite';
  4. import { getApplicationConditionPlugins } from '../plugins';
  5. import { getCommonConfig } from './common';
  6. function defineApplicationConfig(options: DefineApplicationOptions = {}) {
  7. return defineConfig(async (config) => {
  8. const { command, mode } = config;
  9. const { application = {}, vite = {} } = options;
  10. const root = process.cwd();
  11. const isBuild = command === 'build';
  12. const env = loadEnv(mode, root);
  13. const plugins = await getApplicationConditionPlugins({
  14. compress: false,
  15. compressTypes: ['brotli', 'gzip'],
  16. devtools: true,
  17. env,
  18. extraAppConfig: true,
  19. html: true,
  20. i18n: true,
  21. injectAppLoading: true,
  22. isBuild,
  23. license: true,
  24. mock: true,
  25. mode,
  26. pwa: true,
  27. turboConsole: false,
  28. ...(typeof application === 'function'
  29. ? application(config)
  30. : application),
  31. });
  32. const applicationConfig: UserConfig = {
  33. build: {
  34. rollupOptions: {
  35. output: {
  36. assetFileNames: '[ext]/[name]-[hash].[ext]',
  37. chunkFileNames: 'js/[name]-[hash].mjs',
  38. entryFileNames: 'jse/index-[name]-[hash].mjs',
  39. },
  40. },
  41. target: 'es2015',
  42. },
  43. esbuild: {
  44. drop: isBuild
  45. ? [
  46. // 'console',
  47. 'debugger',
  48. ]
  49. : [],
  50. legalComments: 'none',
  51. },
  52. plugins,
  53. server: {
  54. host: true,
  55. warmup: {
  56. // 预热文件
  57. clientFiles: ['./index.html', './src/{views,layouts}/*'],
  58. },
  59. },
  60. };
  61. const mergedConfig = mergeConfig(
  62. await getCommonConfig(),
  63. applicationConfig,
  64. );
  65. return mergeConfig(
  66. mergedConfig,
  67. typeof vite === 'function' ? vite(config) : vite,
  68. );
  69. });
  70. }
  71. export { defineApplicationConfig };