vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import path from 'path'
  2. import Inspect from 'vite-plugin-inspect'
  3. import { defineConfig, loadEnv } from 'vite'
  4. import VueMacros from 'unplugin-vue-macros/vite'
  5. import UnoCSS from 'unocss/vite'
  6. import mkcert from 'vite-plugin-mkcert'
  7. import glob from 'fast-glob'
  8. import vueJsx from '@vitejs/plugin-vue-jsx'
  9. import Components from 'unplugin-vue-components/vite'
  10. import Icons from 'unplugin-icons/vite'
  11. import IconsResolver from 'unplugin-icons/resolver'
  12. import { docPackage, epPackage, getPackageDependencies, projRoot } from '@kankan/build-utils'
  13. import { MarkdownTransform } from './.vitepress/plugins/markdown-transform'
  14. import type { Alias } from 'vite'
  15. const alias: Alias[] = [
  16. {
  17. find: '~/',
  18. replacement: `${path.resolve(__dirname, './.vitepress/vitepress')}/`,
  19. },
  20. ]
  21. // if (process.env.DOC_ENV !== 'production') {
  22. // alias.push(
  23. // {
  24. // find: /^element-plus(\/(es|lib))?$/,
  25. // replacement: path.resolve(projRoot, 'packages/element-plus/index.ts'),
  26. // },
  27. // {
  28. // find: /^element-plus\/(es|lib)\/(.*)$/,
  29. // replacement: `${path.resolve(projRoot, 'packages')}/$2`,
  30. // }
  31. // )
  32. // }
  33. export default defineConfig(async ({ mode }) => {
  34. const env = loadEnv(mode, process.cwd(), '')
  35. const { dependencies: epDeps } = getPackageDependencies(epPackage)
  36. const { dependencies: docsDeps } = getPackageDependencies(docPackage)
  37. const optimizeDeps = [...new Set([...epDeps, ...docsDeps])].filter(dep => !dep.startsWith('@types/') && !['@kankan/metadata', 'kankan'].includes(dep))
  38. optimizeDeps.push(
  39. ...(await glob(['dayjs/plugin/*.js'], {
  40. cwd: path.resolve(projRoot, 'node_modules'),
  41. onlyFiles: true,
  42. }))
  43. )
  44. return {
  45. server: {
  46. host: true,
  47. https: !!env.HTTPS,
  48. fs: {
  49. allow: [projRoot],
  50. },
  51. },
  52. resolve: {
  53. alias,
  54. },
  55. plugins: [
  56. VueMacros({
  57. setupComponent: false,
  58. setupSFC: false,
  59. plugins: {
  60. vueJsx: vueJsx(),
  61. },
  62. }),
  63. // https://github.com/antfu/unplugin-vue-components
  64. Components({
  65. dirs: ['.vitepress/vitepress/components'],
  66. allowOverrides: true,
  67. // custom resolvers
  68. resolvers: [
  69. // auto import icons
  70. // https://github.com/antfu/unplugin-icons
  71. IconsResolver(),
  72. ],
  73. // allow auto import and register components used in markdown
  74. include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
  75. }),
  76. // https://github.com/antfu/unplugin-icons
  77. Icons({
  78. autoInstall: true,
  79. }),
  80. UnoCSS(),
  81. MarkdownTransform(),
  82. Inspect(),
  83. mkcert(),
  84. ],
  85. optimizeDeps: {
  86. include: optimizeDeps,
  87. },
  88. }
  89. })