vite.config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { defineConfig, loadEnv } from 'vite';
  2. import {
  3. getRootPath,
  4. getSrcPath,
  5. viteDefine,
  6. setupVitePlugins,
  7. createViteProxy,
  8. } from './build';
  9. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  10. import { getEnvConfig } from './.env-config.js';
  11. // yarn add --dev @esbuild-plugins/node-globals-polyfill
  12. import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
  13. // yarn add --dev @esbuild-plugins/node-modules-polyfill
  14. import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';
  15. // You don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfill
  16. import rollupNodePolyFill from 'rollup-plugin-node-polyfills';
  17. import { resolve } from 'path';
  18. const resolvePath = (...args) => {
  19. return resolve(__dirname, ...args);
  20. };
  21. export default defineConfig((configEnv) => {
  22. const viteEnv = loadEnv(configEnv.mode, process.cwd());
  23. console.log('viteEnv:', viteEnv);
  24. const processEnvValues = {
  25. 'process.env': Object.entries(viteEnv).reduce((prev, [key, val]) => {
  26. return {
  27. ...prev,
  28. [key]: val,
  29. };
  30. }, {}),
  31. };
  32. const rootPath = getRootPath();
  33. const srcPath = getSrcPath();
  34. const isOpenProxy = viteEnv.VITE_HTTP_PROXY === 'Y';
  35. const envConfig = getEnvConfig(viteEnv);
  36. return {
  37. base: viteEnv.VITE_BASE_URL,
  38. resolve: {
  39. alias: {
  40. '~': rootPath,
  41. '@': srcPath,
  42. 'qs': 'rollup-plugin-node-polyfills/polyfills/qs',
  43. },
  44. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  45. },
  46. css: {
  47. preprocessorOptions: {
  48. less: {
  49. javascriptEnabled: true,
  50. additionalData: `@import "${resolve(
  51. __dirname,
  52. 'src/style/color.less'
  53. )}";`,
  54. },
  55. },
  56. },
  57. define: { ...viteDefine, ...processEnvValues },
  58. plugins: [
  59. ...setupVitePlugins(viteEnv),
  60. NodeModulesPolyfillPlugin(),
  61. NodeGlobalsPolyfillPlugin({
  62. process: true,
  63. buffer: true,
  64. }),
  65. createSvgIconsPlugin({
  66. iconDirs: [resolvePath('src/assets/icons')],
  67. symbolId: 'icon-[dir]-[name]',
  68. customDomId: '__svg__icons__dom__',
  69. svgoOptions: {
  70. full: true,
  71. plugins: [
  72. {
  73. name: 'removeAttrs',
  74. params: {
  75. attrs: ['class', 'data-name', 'fill', 'stroke'],
  76. },
  77. },
  78. ],
  79. },
  80. }),
  81. ],
  82. server: {
  83. host: '0.0.0.0',
  84. port: 8899,
  85. open: true,
  86. proxy: createViteProxy(isOpenProxy, envConfig),
  87. },
  88. preview: {
  89. port: 5050,
  90. },
  91. build: {
  92. reportCompressedSize: false,
  93. sourcemap: false,
  94. commonjsOptions: {
  95. ignoreTryCatch: false,
  96. },
  97. // target: ['chrome52'],
  98. // cssTarget: ['chrome52'],
  99. // rollupOptions: {
  100. // plugins: [rollupNodePolyFill()],
  101. // },
  102. },
  103. };
  104. });