vite.config.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: any = 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: 'billd直播',
  109. // injectScript: `<script src="${outputStaticUrl()}worker.js"></script>`,
  110. },
  111. tags: isProduction
  112. ? [
  113. {
  114. injectTo: 'head',
  115. tag: 'script',
  116. attrs: {
  117. src: 'https://res.hsslive.cn/npm/video.js@8.21.0/video.min.js',
  118. },
  119. },
  120. {
  121. injectTo: 'head',
  122. tag: 'script',
  123. attrs: {
  124. src: 'https://res.hsslive.cn/npm/mpegts.js@1.7.3/mpegts.min.js',
  125. },
  126. },
  127. {
  128. injectTo: 'head',
  129. tag: 'script',
  130. attrs: {
  131. src: 'https://res.hsslive.cn/npm/cos-js-sdk-v5@1.8.6/cos-js-sdk-v5.min.js',
  132. },
  133. },
  134. // {
  135. // injectTo: 'head',
  136. // tag: 'script',
  137. // attrs: {
  138. // src: 'https://res.hsslive.cn/npm/vue@3.5.13/vue.global.min.js',
  139. // },
  140. // },
  141. // {
  142. // injectTo: 'head',
  143. // tag: 'script',
  144. // attrs: {
  145. // src: 'https://res.hsslive.cn/npm/vue-demi@0.14.6/index.iife.min.js',
  146. // },
  147. // },
  148. // {
  149. // injectTo: 'head',
  150. // tag: 'script',
  151. // attrs: {
  152. // src: 'https://res.hsslive.cn/npm/vue-router@4.2.4/vue-router.global.min.js',
  153. // },
  154. // },
  155. ]
  156. : [],
  157. },
  158. }),
  159. checker({
  160. // typescript: true,
  161. vueTsc: true,
  162. // eslint: {
  163. // lintCommand: 'eslint "./src/**/*.{ts,tsx}"', // for example, lint .ts & .tsx
  164. // },
  165. }),
  166. eslint({}),
  167. autoImport({
  168. imports: [
  169. {
  170. 'naive-ui': ['useMessage', 'useNotification'],
  171. },
  172. ],
  173. }),
  174. unpluginVueComponents({
  175. // eslint-disable-next-line
  176. resolvers: [NaiveUiResolver()],
  177. }),
  178. new BilldHtmlWebpackPlugin({ env: 'vite4' }).config,
  179. ],
  180. define: {
  181. 'process.env': {
  182. BilldHtmlWebpackPlugin: logData(null),
  183. NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'),
  184. PUBLIC_PATH: outputStaticUrl(),
  185. VUE_APP_RELEASE_PROJECT_NAME: JSON.stringify(
  186. process.env.VUE_APP_RELEASE_PROJECT_NAME
  187. ),
  188. VUE_APP_RELEASE_PROJECT_ENV: JSON.stringify(
  189. process.env.VUE_APP_RELEASE_PROJECT_ENV
  190. ),
  191. VUE_APP_RELEASE_PROJECT_VERSION: JSON.stringify(pkg.version),
  192. },
  193. },
  194. server: {
  195. host: '0.0.0.0',
  196. proxy: {
  197. '/api': {
  198. target: 'http://localhost:4300',
  199. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  200. /**
  201. * changeOrigin,是否修改请求地址的源
  202. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  203. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  204. */
  205. changeOrigin: true,
  206. rewrite: (path) => path.replace(/^\/api/, '/'),
  207. },
  208. '/betaapi': {
  209. target: 'http://localhost:4300',
  210. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  211. /**
  212. * changeOrigin,是否修改请求地址的源
  213. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  214. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  215. */
  216. changeOrigin: true,
  217. rewrite: (path) => path.replace(/^\/betaapi/, '/'),
  218. },
  219. '/prodapi': {
  220. target: 'http://localhost:4200',
  221. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  222. /**
  223. * changeOrigin,是否修改请求地址的源
  224. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  225. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  226. */
  227. changeOrigin: true,
  228. rewrite: (path) => path.replace(/^\/prodapi/, '/'),
  229. },
  230. },
  231. },
  232. };
  233. });