vite.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import postCssPxToRem from 'postcss-pxtorem'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import path from 'path'
  4. import createVitePlugins from './vite/plugins'
  5. // https://vitejs.dev/config/
  6. export default defineConfig(({ mode, command }) => {
  7. // 加载.env文件,默认不会自动加载
  8. const env = loadEnv(mode, process.cwd())
  9. const { VITE_APP_ENV } = env
  10. return {
  11. // 部署生产环境和开发环境下的URL。
  12. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  13. base: VITE_APP_ENV === 'production' ? '/' : './',
  14. plugins: createVitePlugins(env, command === 'build'),
  15. resolve: {
  16. // https://cn.vitejs.dev/config/#resolve-alias
  17. alias: {
  18. // 设置路径
  19. '~': path.resolve(__dirname, '/'),
  20. // 设置别名
  21. '@': path.resolve(__dirname, 'src')
  22. },
  23. // https://cn.vitejs.dev/config/#resolve-extensions
  24. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  25. },
  26. // vite 相关配置
  27. server: {
  28. port: 9053,
  29. host: '0.0.0.0',
  30. proxy: {
  31. // https://cn.vitejs.dev/config/#server-proxy
  32. '/dev-api': {
  33. target: 'https://www.baidu.com', // 本地开发环境公用网关
  34. // target: 'http://0.0.0.0:8080',
  35. changeOrigin: true,
  36. rewrite: (p) => p.replace(/^\/dev-api/, '')
  37. }
  38. },
  39. hmr: true,
  40. usePolling: true
  41. },
  42. css: {
  43. devSourcemap: true,
  44. postcss: {
  45. plugins: [
  46. // postCssPxToRem({ // 自适应,px>rem转换
  47. // rootValue: 192, // 1rem的大小
  48. // propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
  49. // exclude: /node_modules/i
  50. // }),
  51. // 前缀追加
  52. require('autoprefixer')({
  53. overrideBrowserslist: [
  54. 'Android 4.1',
  55. 'iOS 7.1',
  56. 'Chrome > 31',
  57. 'ff > 31',
  58. 'ie >= 8',
  59. '> 1%'
  60. ],
  61. grid: true
  62. }),
  63. require('postcss-flexbugs-fixes'),
  64. {
  65. postcssPlugin: 'internal:charset-removal',
  66. AtRule: {
  67. charset: (atRule) => {
  68. if (atRule.name === 'charset') {
  69. atRule.remove();
  70. }
  71. }
  72. }
  73. }
  74. ]
  75. },
  76. preprocessorOptions: {
  77. scss: {
  78. /**如果引入多个文件,可以使用
  79. * '@import "@/assets/scss/globalVariable1.scss";
  80. * @import"@/assets/scss/globalVariable2.scss";'
  81. **/
  82. additionalData: '@import "@/assets/style/global.scss";',
  83. }
  84. }
  85. },
  86. build: {
  87. assetsDir: 'static',
  88. rollupOptions: {
  89. external: [],
  90. output: {
  91. chunkFileNames: 'static/js/[name]-[hash].js',
  92. entryFileNames: 'static/js/[name]-[hash].js',
  93. assetFileNames: 'static/[ext]/name-[hash].[ext]',
  94. manualChunks(id) {
  95. // 将pinia的全局库实例打包进vendor,避免和页面一起打包造成资源重复引入
  96. if (id.includes(path.resolve(__dirname, '/src/store/index.js'))) {
  97. return 'vendor';
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. })