build.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Vite 打包配置
  3. */
  4. /**
  5. * 获取打包基础配置
  6. */
  7. export function getBuildBase(isProd) {
  8. return {
  9. target: 'es2015',
  10. outDir: 'dist',
  11. emptyOutDir: true,
  12. sourcemap: isProd ? false : 'inline',
  13. chunkSizeWarningLimit: 1500,
  14. cssCodeSplit: true,
  15. cssMinify: isProd,
  16. assetsInlineLimit: 4096,
  17. reportCompressedSize: false
  18. };
  19. }
  20. /**
  21. * 获取压缩配置
  22. */
  23. export function getTerserOptions(isProd) {
  24. if (!isProd) return undefined;
  25. return {
  26. compress: {
  27. drop_console: true,
  28. drop_debugger: true,
  29. pure_funcs: ['console.log', 'console.info', 'console.debug', 'console.trace'],
  30. passes: 2,
  31. },
  32. format: {
  33. comments: false,
  34. }
  35. };
  36. }
  37. /**
  38. * 获取静态资源文件名配置
  39. */
  40. export function getAssetFileNames(assetInfo) {
  41. const info = assetInfo.name.split('.');
  42. let extType = info[info.length - 1];
  43. if (/\.(png|jpe?g|gif|svg|webp|ico)$/i.test(assetInfo.name)) {
  44. extType = 'img';
  45. } else if (/\.(woff2?|eot|ttf|otf)$/i.test(assetInfo.name)) {
  46. extType = 'fonts';
  47. } else if (/\.css$/i.test(assetInfo.name)) {
  48. extType = 'css';
  49. }
  50. return `${extType}/[name]-[hash].[ext]`;
  51. }
  52. /**
  53. * 获取完整的打包配置
  54. */
  55. export function getBuildConfig(isProd) {
  56. const baseConfig = getBuildBase(isProd);
  57. return {
  58. ...baseConfig,
  59. minify: isProd ? 'terser' : false,
  60. terserOptions: getTerserOptions(isProd),
  61. rollupOptions: {
  62. output: {
  63. chunkFileNames: 'js/[name]-[hash].js',
  64. entryFileNames: 'js/[name]-[hash].js',
  65. assetFileNames: getAssetFileNames
  66. },
  67. external: isProd ? [] : [],
  68. },
  69. optimizeDeps: {
  70. include: [
  71. 'vue',
  72. 'vue-router',
  73. 'pinia',
  74. 'dayjs',
  75. 'axios',
  76. 'js-cookie',
  77. 'nprogress'
  78. ],
  79. exclude: [
  80. '@wangeditor/editor',
  81. '@wangeditor/editor-for-vue',
  82. '@form-create/antd-designer',
  83. '@form-create/ant-design-vue',
  84. 'ant-design-vue',
  85. '@ant-design/icons-vue'
  86. ]
  87. }
  88. };
  89. }