vite.config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import path from 'path'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import vueJsx from '@vitejs/plugin-vue-jsx'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  7. import Inspect from 'vite-plugin-inspect'
  8. import mkcert from 'vite-plugin-mkcert'
  9. import glob from 'fast-glob'
  10. import VueMacros from 'unplugin-vue-macros/vite'
  11. import esbuild from 'rollup-plugin-esbuild'
  12. import {
  13. epPackage,
  14. epRoot,
  15. getPackageDependencies,
  16. pkgRoot,
  17. projRoot,
  18. } from '@element-plus/build-utils'
  19. import type { Plugin } from 'vite'
  20. import './vite.init'
  21. const esbuildPlugin = (): Plugin => ({
  22. ...esbuild({
  23. target: 'chrome64',
  24. include: /\.vue$/,
  25. loaders: {
  26. '.vue': 'js',
  27. },
  28. }),
  29. enforce: 'post',
  30. })
  31. export default defineConfig(async ({ mode }) => {
  32. const env = loadEnv(mode, process.cwd(), '')
  33. let { dependencies } = getPackageDependencies(epPackage)
  34. dependencies = dependencies.filter((dep) => !dep.startsWith('@types/')) // exclude dts deps
  35. const optimizeDeps = (
  36. await glob(['dayjs/(locale|plugin)/*.js'], {
  37. cwd: path.resolve(projRoot, 'node_modules'),
  38. })
  39. ).map((dep) => dep.replace(/\.js$/, ''))
  40. return {
  41. resolve: {
  42. alias: [
  43. {
  44. find: /^element-plus(\/(es|lib))?$/,
  45. replacement: path.resolve(epRoot, 'index.ts'),
  46. },
  47. {
  48. find: /^element-plus\/(es|lib)\/(.*)$/,
  49. replacement: `${pkgRoot}/$2`,
  50. },
  51. ],
  52. },
  53. server: {
  54. host: true,
  55. https: !!env.HTTPS,
  56. },
  57. plugins: [
  58. VueMacros({
  59. setupComponent: false,
  60. setupSFC: false,
  61. plugins: {
  62. vue: vue(),
  63. vueJsx: vueJsx(),
  64. },
  65. }),
  66. esbuildPlugin(),
  67. Components({
  68. include: `${__dirname}/**`,
  69. resolvers: ElementPlusResolver({ importStyle: 'sass' }),
  70. dts: false,
  71. }),
  72. mkcert(),
  73. Inspect(),
  74. ],
  75. optimizeDeps: {
  76. include: ['vue', '@vue/shared', ...dependencies, ...optimizeDeps],
  77. },
  78. esbuild: {
  79. target: 'chrome64',
  80. },
  81. }
  82. })