application.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import type { CSSOptions, 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 { NodePackageImporter } from 'sass';
  6. import { defineConfig, loadEnv, mergeConfig } from 'vite';
  7. import { defaultImportmapOptions, getDefaultPwaOptions } from '../options';
  8. import { loadApplicationPlugins } from '../plugins';
  9. import { loadAndConvertEnv } from '../utils/env';
  10. import { getCommonConfig } from './common';
  11. function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
  12. return defineConfig(async (config) => {
  13. const options = await userConfigPromise?.(config);
  14. const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
  15. const { command, mode } = config;
  16. const { application = {}, vite = {} } = options || {};
  17. const root = process.cwd();
  18. const isBuild = command === 'build';
  19. const env = loadEnv(mode, root);
  20. const plugins = await loadApplicationPlugins({
  21. archiver: true,
  22. archiverPluginOptions: {},
  23. compress: false,
  24. compressTypes: ['brotli', 'gzip'],
  25. devtools: true,
  26. env,
  27. extraAppConfig: true,
  28. html: true,
  29. i18n: true,
  30. importmapOptions: defaultImportmapOptions,
  31. injectAppLoading: true,
  32. injectMetadata: true,
  33. isBuild,
  34. license: true,
  35. mode,
  36. nitroMock: !isBuild,
  37. nitroMockOptions: {},
  38. print: !isBuild,
  39. printInfoMap: {
  40. 'Vben Admin Docs': 'https://doc.vben.pro',
  41. },
  42. pwa: true,
  43. pwaOptions: getDefaultPwaOptions(appTitle),
  44. vxeTableLazyImport: true,
  45. ...envConfig,
  46. ...application,
  47. });
  48. const { injectGlobalScss = true } = application;
  49. const applicationConfig: UserConfig = {
  50. base,
  51. build: {
  52. rollupOptions: {
  53. output: {
  54. assetFileNames: '[ext]/[name]-[hash].[ext]',
  55. chunkFileNames: 'js/[name]-[hash].js',
  56. entryFileNames: 'jse/index-[name]-[hash].js',
  57. },
  58. },
  59. target: 'es2015',
  60. },
  61. css: createCssOptions(injectGlobalScss),
  62. esbuild: {
  63. drop: isBuild
  64. ? [
  65. // 'console',
  66. 'debugger',
  67. ]
  68. : [],
  69. legalComments: 'none',
  70. },
  71. plugins,
  72. server: {
  73. host: true,
  74. port,
  75. warmup: {
  76. // 预热文件
  77. clientFiles: [
  78. './index.html',
  79. './src/bootstrap.ts',
  80. './src/{views,layouts,router,store,api,adapter}/*',
  81. ],
  82. },
  83. },
  84. };
  85. const mergedCommonConfig = mergeConfig(
  86. await getCommonConfig(),
  87. applicationConfig,
  88. );
  89. return mergeConfig(mergedCommonConfig, vite);
  90. });
  91. }
  92. function createCssOptions(injectGlobalScss = true): CSSOptions {
  93. const root = findMonorepoRoot();
  94. return {
  95. preprocessorOptions: injectGlobalScss
  96. ? {
  97. scss: {
  98. additionalData: (content: string, filepath: string) => {
  99. const relativePath = relative(root, filepath);
  100. // apps下的包注入全局样式
  101. if (relativePath.startsWith(`apps${path.sep}`)) {
  102. return `@use "@vben/styles/global" as *;\n${content}`;
  103. }
  104. return content;
  105. },
  106. api: 'modern',
  107. importers: [new NodePackageImporter()],
  108. },
  109. }
  110. : {},
  111. };
  112. }
  113. export { defineApplicationConfig };