vite.config.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { defineConfig, loadEnv } from 'vite';
  2. import {
  3. getRootPath,
  4. getSrcPath,
  5. viteDefine,
  6. setupVitePlugins,
  7. createViteProxy,
  8. } from './build';
  9. import { getEnvConfig } from './.env-config.js';
  10. // yarn add --dev @esbuild-plugins/node-globals-polyfill
  11. import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
  12. // yarn add --dev @esbuild-plugins/node-modules-polyfill
  13. import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';
  14. // You don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfill
  15. import rollupNodePolyFill from 'rollup-plugin-node-polyfills';
  16. export default defineConfig((configEnv) => {
  17. const viteEnv = loadEnv(configEnv.mode, process.cwd());
  18. console.log('viteEnv:', viteEnv);
  19. const processEnvValues = {
  20. 'process.env': Object.entries(viteEnv).reduce((prev, [key, val]) => {
  21. return {
  22. ...prev,
  23. [key]: val,
  24. };
  25. }, {}),
  26. };
  27. const rootPath = getRootPath();
  28. const srcPath = getSrcPath();
  29. const isOpenProxy = viteEnv.VITE_HTTP_PROXY === 'Y';
  30. const envConfig = getEnvConfig(viteEnv);
  31. return {
  32. base: viteEnv.VITE_BASE_URL,
  33. resolve: {
  34. alias: {
  35. '~': rootPath,
  36. '@': srcPath,
  37. 'qs': 'rollup-plugin-node-polyfills/polyfills/qs',
  38. },
  39. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  40. },
  41. define: { ...viteDefine, ...processEnvValues },
  42. plugins: [
  43. ...setupVitePlugins(viteEnv),
  44. NodeModulesPolyfillPlugin(),
  45. NodeGlobalsPolyfillPlugin({
  46. process: true,
  47. buffer: true,
  48. }),
  49. ],
  50. server: {
  51. host: '0.0.0.0',
  52. port: 8888,
  53. open: true,
  54. proxy: createViteProxy(isOpenProxy, envConfig),
  55. },
  56. preview: {
  57. port: 5050,
  58. },
  59. build: {
  60. reportCompressedSize: false,
  61. sourcemap: false,
  62. commonjsOptions: {
  63. ignoreTryCatch: false,
  64. },
  65. // target: ['chrome52'],
  66. // cssTarget: ['chrome52'],
  67. // rollupOptions: {
  68. // plugins: [rollupNodePolyFill()],
  69. // },
  70. },
  71. };
  72. });