vite.config.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2022 Pnoker All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import legacy from '@vitejs/plugin-legacy'
  17. import vue from '@vitejs/plugin-vue'
  18. import * as dotenv from 'dotenv'
  19. import * as fs from 'fs'
  20. import { resolve } from 'path'
  21. import AutoImport from 'unplugin-auto-import/vite'
  22. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  23. import Components from 'unplugin-vue-components/vite'
  24. import { ConfigEnv, defineConfig } from 'vite'
  25. export default (configEnv: ConfigEnv) => {
  26. process.env.NODE_ENV = configEnv.mode || 'dev'
  27. const envDir = './src/config/env'
  28. const files = [`${envDir}/.env`, `${envDir}/.env.${process.env.NODE_ENV}`]
  29. files.forEach((file) => {
  30. const config = dotenv.parse<any>(fs.readFileSync(file))
  31. Object.keys(config).forEach((key) => {
  32. process.env[key] = config[key]
  33. })
  34. })
  35. const alias: Record<string, string> = {
  36. '@': resolve(__dirname, './src'),
  37. vue$: 'vue/dist/vue.runtime.esm-bundler.js',
  38. }
  39. const apiPrefix = process.env.APP_API_PREFIX as string
  40. const proxy = {
  41. [apiPrefix]: {
  42. ws: false,
  43. changeOrigin: true,
  44. target: `${process.env.APP_API_PATH}:${process.env.APP_API_PORT}`,
  45. rewrite: (path: string) => path.replace(new RegExp(`^${apiPrefix}`), apiPrefix),
  46. },
  47. }
  48. const output = {
  49. entryFileNames: `assets/dc3.[name].[hash].js`,
  50. chunkFileNames: `assets/dc3.[name].[hash].js`,
  51. assetFileNames: `assets/dc3.[name].[hash].[ext]`,
  52. compact: true,
  53. manualChunks: {
  54. vue: ['vue', 'vue-router', 'vuex'],
  55. echarts: ['echarts'],
  56. },
  57. }
  58. return defineConfig({
  59. base: './',
  60. root: './',
  61. envDir,
  62. envPrefix: 'APP_',
  63. resolve: {
  64. alias,
  65. },
  66. server: {
  67. port: Number(process.env.APP_CLI_PORT),
  68. proxy,
  69. },
  70. build: {
  71. outDir: 'dist',
  72. chunkSizeWarningLimit: 1500,
  73. rollupOptions: { output },
  74. },
  75. plugins: [
  76. vue(),
  77. legacy({
  78. targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
  79. }),
  80. AutoImport({
  81. resolvers: [ElementPlusResolver()],
  82. }),
  83. Components({
  84. resolvers: [ElementPlusResolver()],
  85. }),
  86. ],
  87. css: { preprocessorOptions: { css: { charset: false } } },
  88. })
  89. }