vite.config.js 2.2 KB

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