vite.config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { fileURLToPath, URL } from 'node:url';
  2. import path from 'path';
  3. import { defineConfig } from 'vite';
  4. import vue from '@vitejs/plugin-vue';
  5. import vueJsx from '@vitejs/plugin-vue-jsx';
  6. import { createHtmlPlugin } from 'vite-plugin-html';
  7. import AutoImport from 'unplugin-auto-import/vite';
  8. import Components from 'unplugin-vue-components/vite';
  9. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
  10. const IS_PRODUCTION = process.env.NODE_ENV === 'production';
  11. // 当前场景
  12. const SCENE = process.env.VITE_APP_SCENE;
  13. if (SCENE != null) {
  14. console.log('当前场景:', SCENE);
  15. }
  16. // https://vite.dev/config/
  17. export default defineConfig(() => {
  18. return {
  19. base: './',
  20. plugins: [
  21. createHtmlPlugin({
  22. inject: {
  23. data: {
  24. title: process.env.TITLE,
  25. },
  26. },
  27. }),
  28. vue(),
  29. vueJsx(),
  30. AutoImport({
  31. resolvers: [ElementPlusResolver()],
  32. }),
  33. Components({
  34. resolvers: [ElementPlusResolver()],
  35. }),
  36. ],
  37. resolve: {
  38. alias: {
  39. '@': fileURLToPath(new URL('./src/index', import.meta.url)),
  40. '@hotspot': fileURLToPath(new URL('./src/hotspot', import.meta.url)),
  41. },
  42. extensions: SCENE
  43. ? [
  44. `.${SCENE}.mjs`,
  45. `.${SCENE}.js`,
  46. `.${SCENE}.ts`,
  47. `.${SCENE}.jsx`,
  48. `.${SCENE}.tsx`,
  49. `.${SCENE}.json`,
  50. `.${SCENE}.vue`,
  51. '.mjs',
  52. '.js',
  53. '.ts',
  54. '.jsx',
  55. '.tsx',
  56. '.json',
  57. '.vue',
  58. ]
  59. : ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  60. },
  61. server: {
  62. port: Number(process.env.PORT) || 80,
  63. host: '0.0.0.0',
  64. strictPort: true,
  65. cors: true,
  66. },
  67. build: {
  68. outDir: IS_PRODUCTION && SCENE ? `build/${SCENE}` : 'build',
  69. assetsDir: 'assets',
  70. sourcemap: process.env.SOURCE_MAP === 'true',
  71. rollupOptions: {
  72. input: {
  73. index: path.resolve(__dirname, 'index.html'),
  74. hotspot: path.resolve(__dirname, 'hotspot.html'),
  75. },
  76. output: {
  77. manualChunks: {
  78. vendors: ['vue', 'element-plus', 'swiper'],
  79. },
  80. },
  81. },
  82. },
  83. css: {
  84. preprocessorOptions: {
  85. scss: {
  86. api: 'modern-compiler',
  87. },
  88. },
  89. },
  90. };
  91. });