vite.config.ts 5.2 KB

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