You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6 KiB

  1. import legacyPlugin from '@vitejs/plugin-legacy'
  2. // import usePluginImport from 'vite-plugin-importer';
  3. import { viteLogo } from './src/core/config'
  4. import Banner from 'vite-plugin-banner'
  5. import * as path from 'path'
  6. import * as dotenv from 'dotenv'
  7. import * as fs from 'fs'
  8. import vuePlugin from '@vitejs/plugin-vue'
  9. // @see https://cn.vitejs.dev/config/
  10. export default ({
  11. command,
  12. mode
  13. }) => {
  14. const NODE_ENV = process.env.NODE_ENV || 'development'
  15. const envFiles = [
  16. `.env.${NODE_ENV}`
  17. ]
  18. for (const file of envFiles) {
  19. const envConfig = dotenv.parse(fs.readFileSync(file))
  20. for (const k in envConfig) {
  21. process.env[k] = envConfig[k]
  22. }
  23. }
  24. viteLogo(process.env)
  25. const timestamp = Date.parse(new Date())
  26. const rollupOptions = {
  27. output: {
  28. entryFileNames: `gva/gin-vue-admin-[name].${timestamp}.js`,
  29. chunkFileNames: `js/gin-vue-admin-[name].${timestamp}.js`,
  30. assetFileNames: `assets/gin-vue-admin-[name].${timestamp}.[ext]`
  31. }
  32. }
  33. const optimizeDeps = {}
  34. const alias = {
  35. '@': path.resolve(__dirname, './src'),
  36. 'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
  37. }
  38. const esbuild = {}
  39. return {
  40. base: './', // index.html文件所在位置
  41. root: './', // js导入的资源路径,src
  42. resolve: {
  43. alias,
  44. },
  45. define: {
  46. 'process.env': {}
  47. },
  48. server: {
  49. open: true,
  50. port: process.env.VITE_CLI_PORT,
  51. proxy: {
  52. // 把key的路径代理到target位置
  53. // detail: https://cli.vuejs.org/config/#devserver-proxy
  54. [process.env.VITE_BASE_API]: { // 需要代理的路径 例如 '/api'
  55. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  56. changeOrigin: true,
  57. rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
  58. }
  59. },
  60. },
  61. build: {
  62. target: 'es2015',
  63. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  64. manifest: false, // 是否产出maifest.json
  65. sourcemap: false, // 是否产出soucemap.json
  66. outDir: 'dist', // 产出目录
  67. rollupOptions,
  68. },
  69. esbuild,
  70. optimizeDeps,
  71. plugins: [
  72. legacyPlugin({
  73. targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
  74. }), vuePlugin(), [Banner(`\n Build based on gin-vue-admin \n Time : ${timestamp}`)]
  75. ],
  76. css: {
  77. preprocessorOptions: {
  78. less: {
  79. // 支持内联 JavaScript
  80. javascriptEnabled: true,
  81. }
  82. }
  83. },
  84. }
  85. }