vite.config.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import path from 'path';
  2. import vue from '@vitejs/plugin-vue';
  3. import { BilldHtmlWebpackPlugin, logData } from 'billd-html-webpack-plugin';
  4. import externalGlobals from 'rollup-plugin-external-globals';
  5. import autoImport from 'unplugin-auto-import/vite';
  6. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
  7. import unpluginVueComponents from 'unplugin-vue-components/vite';
  8. import { defineConfig } from 'vite';
  9. import prefetchPlugin from 'vite-plugin-bundle-prefetch';
  10. import checker from 'vite-plugin-checker';
  11. import { chunkSplitPlugin } from 'vite-plugin-chunk-split';
  12. import eslint from 'vite-plugin-eslint2';
  13. import { createHtmlPlugin } from 'vite-plugin-html';
  14. import pkg from './package.json';
  15. const globals = externalGlobals({
  16. // 'runtime-core': 'runtime-core',
  17. // vue: 'Vue',
  18. // 'vue-demi': 'VueDemi',
  19. // 'vue-router': 'VueRouter',
  20. 'video.js': 'videojs',
  21. 'mpegts.js': 'mpegts',
  22. fabric: 'fabric',
  23. 'cos-js-sdk-v5': 'COS',
  24. });
  25. // https://vitejs.dev/config/
  26. export default defineConfig(({ mode }) => {
  27. const isProduction = mode === 'production';
  28. const outputStaticUrl = () => {
  29. if (isProduction) {
  30. return 'https://live.hsslive.cn/dist/';
  31. } else {
  32. return './';
  33. }
  34. // return './';
  35. };
  36. return {
  37. base: outputStaticUrl(),
  38. css: {
  39. preprocessorOptions: {
  40. scss: {
  41. additionalData: `@use 'billd-scss/src/index.scss' as *;@import '@/assets/constant.scss';`,
  42. },
  43. },
  44. },
  45. resolve: {
  46. alias: {
  47. '@': path.resolve(__dirname, 'src'),
  48. // 'runtime-core':
  49. // 'https://res.hsslive.cn/npm/@vue/runtime-core@3.5.13/+esm.js',
  50. // vue: 'https://res.hsslive.cn/npm/vue@3.5.13/+esm.js',
  51. // 'vue-router': 'https://res.hsslive.cn/npm/vue-router@4.2.4/+esm.js',
  52. // 'video.js': 'https://res.hsslive.cn/npm/video.js@8.21.0/+esm.js',
  53. // 'mpegts.js': 'https://res.hsslive.cn/npm/mpegts.js@1.7.3/+esm.js',
  54. // fabric: 'https://res.hsslive.cn/npm/fabric@5.4.2/+esm.js',
  55. // 'cos-js-sdk-v5':
  56. // 'https://res.hsslive.cn/npm/cos-js-sdk-v5@1.8.6/+esm.js',
  57. },
  58. /**
  59. * 不建议省略.vue后缀
  60. * https://cn.vitejs.dev/config/shared-options.html#resolve-extensions
  61. */
  62. // extensions: ['.js', '.ts', '.jsx', '.tsx', '.vue'],
  63. },
  64. build: {
  65. outDir: 'dist',
  66. rollupOptions: {
  67. external: [
  68. // 'vue',
  69. // 'vue-demi',
  70. // 'vue-router',
  71. // 'runtime-core',
  72. 'video.js',
  73. 'mpegts.js',
  74. 'fabric',
  75. 'cos-js-sdk-v5',
  76. ],
  77. plugins: [globals],
  78. },
  79. },
  80. plugins: [
  81. // visualizer({
  82. // gzipSize: true,
  83. // brotliSize: true,
  84. // emitFile: false,
  85. // filename: 'visualizer.html', // 分析图生成的文件名
  86. // open: true, // 如果存在本地服务端口,将在打包后自动展示
  87. // }),
  88. chunkSplitPlugin({
  89. // 指定拆包策略
  90. // customSplitting: {
  91. // // `vue` and `vue-router` 会被打包到一个名为`vue-vendor`的 chunk 里面(包括它们的一些依赖,如 object-assign)
  92. // 'vue-vendor': [/vue/],
  93. // 'vue-router-vendor': [/vue-router/],
  94. // 'av-cliper-vendor': [/@webav\/av-cliper/],
  95. // // 源码中 utils 目录的代码都会打包进 `utils` 这个 chunk 中
  96. // // utils: [/src\/utils/],
  97. // views: [/src\/views/],
  98. // compoents: [/src\/compoents/],
  99. // },
  100. }),
  101. prefetchPlugin(),
  102. // isProduction && legacy(),
  103. vue(),
  104. createHtmlPlugin({
  105. inject: {
  106. data: {
  107. // @ts-ignore
  108. title: '',
  109. },
  110. },
  111. }),
  112. checker({
  113. // typescript: true,
  114. vueTsc: true,
  115. // eslint: {
  116. // lintCommand: 'eslint "./src/**/*.{ts,tsx}"', // for example, lint .ts & .tsx
  117. // },
  118. }),
  119. eslint({}),
  120. autoImport({
  121. imports: [
  122. {
  123. 'naive-ui': ['useMessage', 'useNotification'],
  124. },
  125. ],
  126. }),
  127. unpluginVueComponents({
  128. // eslint-disable-next-line
  129. resolvers: [NaiveUiResolver()],
  130. }),
  131. new BilldHtmlWebpackPlugin({ env: 'vite4' }).config,
  132. ],
  133. define: {
  134. 'process.env': {
  135. BilldHtmlWebpackPlugin: logData(null),
  136. NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'),
  137. PUBLIC_PATH: outputStaticUrl(),
  138. VUE_APP_RELEASE_PROJECT_NAME: JSON.stringify(
  139. process.env.VUE_APP_RELEASE_PROJECT_NAME
  140. ),
  141. VUE_APP_RELEASE_PROJECT_ENV: JSON.stringify(
  142. process.env.VUE_APP_RELEASE_PROJECT_ENV
  143. ),
  144. VUE_APP_RELEASE_PROJECT_VERSION: JSON.stringify(pkg.version),
  145. },
  146. },
  147. server: {
  148. host: '0.0.0.0',
  149. proxy: {
  150. '/api': {
  151. target: 'http://localhost:4300',
  152. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  153. /**
  154. * changeOrigin,是否修改请求地址的源
  155. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  156. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  157. */
  158. changeOrigin: true,
  159. rewrite: (path) => path.replace(/^\/api/, '/'),
  160. },
  161. '/betaapi': {
  162. target: 'http://localhost:4300',
  163. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  164. /**
  165. * changeOrigin,是否修改请求地址的源
  166. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  167. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  168. */
  169. changeOrigin: true,
  170. rewrite: (path) => path.replace(/^\/betaapi/, '/'),
  171. },
  172. '/prodapi': {
  173. target: 'http://localhost:4200',
  174. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  175. /**
  176. * changeOrigin,是否修改请求地址的源
  177. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  178. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  179. */
  180. changeOrigin: true,
  181. rewrite: (path) => path.replace(/^\/prodapi/, '/'),
  182. },
  183. },
  184. },
  185. };
  186. });