vite.config.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import path from 'path';
  2. import vue from '@vitejs/plugin-vue';
  3. import { BilldHtmlWebpackPlugin, logData } from 'billd-html-webpack-plugin';
  4. import autoImport from 'unplugin-auto-import/vite';
  5. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
  6. import unpluginVueComponents from 'unplugin-vue-components/vite';
  7. import { defineConfig } from 'vite';
  8. import checker from 'vite-plugin-checker';
  9. import eslint from 'vite-plugin-eslint2';
  10. import { createHtmlPlugin } from 'vite-plugin-html';
  11. import pkg from './package.json';
  12. // https://vitejs.dev/config/
  13. export default defineConfig(({ mode }) => {
  14. const isProduction = mode === 'production';
  15. const outputStaticUrl = () => {
  16. if (isProduction) {
  17. return 'https://resource.hsslive.cn/billd-live/client/dist/';
  18. } else {
  19. return './';
  20. }
  21. };
  22. return {
  23. base: outputStaticUrl(),
  24. css: {
  25. preprocessorOptions: {
  26. scss: {
  27. additionalData: `@use 'billd-scss/src/index.scss' as *;@import '@/assets/constant.scss';`,
  28. },
  29. },
  30. },
  31. resolve: {
  32. alias: { '@': path.resolve(__dirname, 'src') },
  33. /**
  34. * 不建议省略.vue后缀
  35. * https://cn.vitejs.dev/config/shared-options.html#resolve-extensions
  36. */
  37. // extensions: ['.js', '.ts', '.jsx', '.tsx', '.vue'],
  38. },
  39. build: {
  40. outDir: 'dist',
  41. },
  42. plugins: [
  43. // legacy(),
  44. // isProduction && legacy(),
  45. vue(),
  46. createHtmlPlugin({
  47. inject: {
  48. data: {
  49. // @ts-ignore
  50. title: '',
  51. },
  52. },
  53. }),
  54. checker({
  55. // typescript: true,
  56. vueTsc: true,
  57. // eslint: {
  58. // lintCommand: 'eslint "./src/**/*.{ts,tsx}"', // for example, lint .ts & .tsx
  59. // },
  60. }),
  61. eslint({}),
  62. autoImport({
  63. imports: [
  64. {
  65. 'naive-ui': ['useMessage', 'useNotification'],
  66. },
  67. ],
  68. }),
  69. unpluginVueComponents({
  70. // eslint-disable-next-line
  71. resolvers: [NaiveUiResolver()],
  72. }),
  73. new BilldHtmlWebpackPlugin({ env: 'vite4' }).config,
  74. ],
  75. define: {
  76. 'process.env': {
  77. BilldHtmlWebpackPlugin: logData(null),
  78. NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'),
  79. PUBLIC_PATH: outputStaticUrl(),
  80. VUE_APP_RELEASE_PROJECT_NAME: JSON.stringify(
  81. process.env.VUE_APP_RELEASE_PROJECT_NAME
  82. ),
  83. VUE_APP_RELEASE_PROJECT_ENV: JSON.stringify(
  84. process.env.VUE_APP_RELEASE_PROJECT_ENV
  85. ),
  86. VUE_APP_RELEASE_PROJECT_VERSION: JSON.stringify(pkg.version),
  87. },
  88. },
  89. server: {
  90. host: '0.0.0.0',
  91. proxy: {
  92. '/api': {
  93. target: 'http://localhost:4300',
  94. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  95. /**
  96. * changeOrigin,是否修改请求地址的源
  97. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  98. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  99. */
  100. changeOrigin: true,
  101. rewrite: (path) => path.replace(/^\/api/, '/'),
  102. },
  103. '/betaapi': {
  104. target: 'http://localhost:4300',
  105. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  106. /**
  107. * changeOrigin,是否修改请求地址的源
  108. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  109. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  110. */
  111. changeOrigin: true,
  112. rewrite: (path) => path.replace(/^\/betaapi/, '/'),
  113. },
  114. '/prodapi': {
  115. target: 'http://localhost:4200',
  116. secure: false, // 默认情况下(secure: true),不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后,后端服务器的HTTPS有无效证书也可运行
  117. /**
  118. * changeOrigin,是否修改请求地址的源
  119. * 默认changeOrigin: false,即发请求即使用devServer的localhost:port发起的,如果后端服务器有校验源,就会有问题
  120. * 设置changeOrigin: true,就会修改发起请求的源,将原本的localhost:port修改为target,这样就可以通过后端服务器对源的校验
  121. */
  122. changeOrigin: true,
  123. rewrite: (path) => path.replace(/^\/prodapi/, '/'),
  124. },
  125. },
  126. },
  127. };
  128. });