vite.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // 是否离线包
  14. const OFFLINE = Boolean(Number(process.env.VITE_APP_OFFLINE));
  15. const extensions = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'];
  16. if (SCENE != null) {
  17. console.log('当前场景:', SCENE);
  18. extensions.unshift(
  19. `.${SCENE}.mjs`,
  20. `.${SCENE}.js`,
  21. `.${SCENE}.ts`,
  22. `.${SCENE}.jsx`,
  23. `.${SCENE}.tsx`,
  24. `.${SCENE}.json`,
  25. `.${SCENE}.vue`
  26. );
  27. if (OFFLINE) {
  28. extensions.unshift(
  29. `.${SCENE}.offline.mjs`,
  30. `.${SCENE}.offline.js`,
  31. `.${SCENE}.offline.ts`,
  32. `.${SCENE}.offline.jsx`,
  33. `.${SCENE}.offline.tsx`,
  34. `.${SCENE}.offline.json`,
  35. `.${SCENE}.offline.vue`
  36. );
  37. }
  38. }
  39. // https://vite.dev/config/
  40. export default defineConfig(() => {
  41. return {
  42. base: './',
  43. plugins: [
  44. createHtmlPlugin({
  45. inject: {
  46. data: {
  47. title: process.env.TITLE,
  48. },
  49. },
  50. }),
  51. vue(),
  52. vueJsx(),
  53. AutoImport({
  54. resolvers: [
  55. ElementPlusResolver({
  56. importStyle: 'sass',
  57. }),
  58. ],
  59. }),
  60. Components({
  61. resolvers: [
  62. ElementPlusResolver({
  63. importStyle: 'sass',
  64. }),
  65. ],
  66. }),
  67. ],
  68. resolve: {
  69. alias: {
  70. '@': fileURLToPath(new URL('./src/index', import.meta.url)),
  71. '@hotspot': fileURLToPath(new URL('./src/hotspot', import.meta.url)),
  72. },
  73. extensions,
  74. },
  75. server: {
  76. port: Number(process.env.PORT) || 80,
  77. host: '0.0.0.0',
  78. strictPort: true,
  79. cors: true,
  80. },
  81. build: {
  82. outDir: IS_PRODUCTION && SCENE ? `build/${SCENE}` : 'build',
  83. assetsDir: 'assets',
  84. sourcemap: process.env.SOURCE_MAP === 'true',
  85. rollupOptions: {
  86. input: {
  87. index: path.resolve(__dirname, 'index.html'),
  88. hotspot: path.resolve(__dirname, 'hotspot.html'),
  89. },
  90. output: {
  91. manualChunks: {
  92. vendors: ['vue', 'element-plus', 'swiper'],
  93. },
  94. },
  95. },
  96. },
  97. css: {
  98. preprocessorOptions: {
  99. scss: {
  100. api: 'modern-compiler',
  101. additionalData: `@use "@/assets/el${SCENE ? '.' + SCENE : ''}.scss" as *;`,
  102. },
  103. },
  104. },
  105. };
  106. });