application.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. injectMetadata: true,
  23. isBuild,
  24. license: true,
  25. mock: true,
  26. mode,
  27. pwa: true,
  28. turboConsole: false,
  29. ...(typeof application === 'function'
  30. ? application(config)
  31. : application),
  32. });
  33. const applicationConfig: UserConfig = {
  34. build: {
  35. rollupOptions: {
  36. output: {
  37. assetFileNames: '[ext]/[name]-[hash].[ext]',
  38. chunkFileNames: 'js/[name]-[hash].mjs',
  39. entryFileNames: 'jse/index-[name]-[hash].mjs',
  40. },
  41. },
  42. target: 'es2015',
  43. },
  44. esbuild: {
  45. drop: isBuild
  46. ? [
  47. // 'console',
  48. 'debugger',
  49. ]
  50. : [],
  51. legalComments: 'none',
  52. },
  53. plugins,
  54. server: {
  55. host: true,
  56. warmup: {
  57. // 预热文件
  58. clientFiles: ['./index.html', './src/{views,layouts}/*'],
  59. },
  60. },
  61. };
  62. const mergedConfig = mergeConfig(
  63. await getCommonConfig(),
  64. applicationConfig,
  65. );
  66. return mergeConfig(
  67. mergedConfig,
  68. typeof vite === 'function' ? vite(config) : vite,
  69. );
  70. });
  71. }
  72. export { defineApplicationConfig };