vite.config.ts 2.9 KB

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