vite.config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * @Author: jixiang
  3. * @Date: 2021-07-07 15:19:05
  4. * @Description: 配置中心
  5. * @FilePath: /suqian_pc_new/vite.config.ts
  6. * One World One Drefeel(56580223@qq.com)
  7. */
  8. import { defineConfig } from 'vite';
  9. import vue from '@vitejs/plugin-vue';
  10. import styleImport from 'vite-plugin-style-import';
  11. // import path from 'path';
  12. import path, { resolve } from 'path'; // 主要用于alias文件路径别名
  13. // https://vitejs.dev/config/
  14. const px2rem = require('postcss-px2rem');
  15. const postcss = px2rem({
  16. remUnit: 16, // 基准大小 baseSize,需要和rem.js中相同
  17. });
  18. const setAlias = (alias: [string, string][]) =>
  19. alias.map((v) => {
  20. return { find: v[0], replacement: path.resolve(__dirname, v[1]) };
  21. });
  22. export default defineConfig({
  23. plugins: [
  24. vue(),
  25. styleImport({
  26. libs: [
  27. {
  28. libraryName: 'ant-design-vue',
  29. esModule: true,
  30. resolveStyle: (name) => {
  31. return `ant-design-vue/es/${name}/style/css`;
  32. },
  33. },
  34. ],
  35. }),
  36. ], // 配置需要使用的插件列表,这里将vue添加进去
  37. // 配置文件别名 vite1.0是/@/ 2.0改为/@
  38. // 这里是将src目录配置别名为 /@ 方便在项目中导入src目录下的文件
  39. resolve: {
  40. alias: setAlias([['@', 'src']]),
  41. },
  42. // 强制预构建插件包
  43. optimizeDeps: {
  44. include: ['axios'],
  45. },
  46. // 打包配置
  47. build: {
  48. outDir: 'dist',
  49. assetsDir: 'assets',
  50. manifest: false,
  51. minify: 'terser', // 混淆器,terser构建后文件体积更小
  52. rollupOptions: {
  53. output: {
  54. entryFileNames: `assets/[name].js`,
  55. chunkFileNames: `assets/[name].js`,
  56. assetFileNames: `assets/[name].[ext]`,
  57. },
  58. },
  59. },
  60. publicDir: false,
  61. /**
  62. * 在生产中服务时的基本公共路径。
  63. * @default '/'
  64. */
  65. base: './',
  66. css: {
  67. preprocessorOptions: {
  68. less: {
  69. modifyVars: {
  70. hack: `true; @import (reference) "${path.resolve('src/styles/antd.less')}";`,
  71. },
  72. javascriptEnabled: true,
  73. },
  74. },
  75. postcss: {
  76. plugins: [postcss],
  77. },
  78. },
  79. // 本地运行配置,及反向代理配置
  80. server: {
  81. cors: true, // 默认启用并允许任何源
  82. open: true, // 在服务器启动时自动在浏览器中打开应用程序
  83. //反向代理配置,注意rewrite写法,开始没看文档在这里踩了坑
  84. // proxy: {
  85. // '/api': {
  86. // target: 'http://192.168.99.223:3000', //代理接口
  87. // changeOrigin: true,
  88. // rewrite: (path) => path.replace(/^\/api/, '')
  89. // }
  90. // }
  91. },
  92. });