vite.config.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: [
  32. ElementPlusResolver({
  33. importStyle: 'sass',
  34. }),
  35. ],
  36. }),
  37. Components({
  38. resolvers: [
  39. ElementPlusResolver({
  40. importStyle: 'sass',
  41. }),
  42. ],
  43. }),
  44. ],
  45. resolve: {
  46. alias: {
  47. '@': fileURLToPath(new URL('./src/index', import.meta.url)),
  48. '@hotspot': fileURLToPath(new URL('./src/hotspot', import.meta.url)),
  49. },
  50. extensions: SCENE
  51. ? [
  52. `.${SCENE}.mjs`,
  53. `.${SCENE}.js`,
  54. `.${SCENE}.ts`,
  55. `.${SCENE}.jsx`,
  56. `.${SCENE}.tsx`,
  57. `.${SCENE}.json`,
  58. `.${SCENE}.vue`,
  59. '.mjs',
  60. '.js',
  61. '.ts',
  62. '.jsx',
  63. '.tsx',
  64. '.json',
  65. '.vue',
  66. ]
  67. : ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  68. },
  69. server: {
  70. port: Number(process.env.PORT) || 80,
  71. host: '0.0.0.0',
  72. strictPort: true,
  73. cors: true,
  74. },
  75. build: {
  76. outDir: IS_PRODUCTION && SCENE ? `build/${SCENE}` : 'build',
  77. assetsDir: 'assets',
  78. sourcemap: process.env.SOURCE_MAP === 'true',
  79. rollupOptions: {
  80. input: {
  81. index: path.resolve(__dirname, 'index.html'),
  82. hotspot: path.resolve(__dirname, 'hotspot.html'),
  83. },
  84. output: {
  85. manualChunks: {
  86. vendors: ['vue', 'element-plus', 'swiper'],
  87. },
  88. },
  89. },
  90. },
  91. css: {
  92. preprocessorOptions: {
  93. scss: {
  94. api: 'modern-compiler',
  95. additionalData: `@use "@/assets/el${SCENE ? '.' + SCENE : ''}.scss" as *;`,
  96. },
  97. },
  98. },
  99. };
  100. });