vite.config.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. port: process.env.VITE_CLI_PORT,
  41. proxy:{
  42. // 把key的路径代理到target位置
  43. // detail: https://cli.vuejs.org/config/#devserver-proxy
  44. [process.env.VITE_BASE_API]: { // 需要代理的路径 例如 '/api'
  45. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  46. changeOrigin: true,
  47. rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
  48. }
  49. },
  50. },
  51. build: {
  52. target: 'es2015',
  53. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  54. manifest: false, // 是否产出maifest.json
  55. sourcemap: false, // 是否产出soucemap.json
  56. outDir: 'build', // 产出目录
  57. rollupOptions,
  58. },
  59. esbuild,
  60. optimizeDeps,
  61. plugins: [
  62. legacyPlugin({
  63. targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
  64. }), vuePlugin(),
  65. ],
  66. css: {
  67. preprocessorOptions: {
  68. less: {
  69. // 支持内联 JavaScript
  70. javascriptEnabled: true,
  71. }
  72. }
  73. },
  74. }
  75. }