You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
2.0 KiB

4 weeks ago
  1. export function createBuild(viteEnv) {
  2. const { VITE_OUTPUT_DIR } = viteEnv;
  3. return {
  4. sourcemap: false, // 是否启用
  5. outDir: VITE_OUTPUT_DIR,
  6. cssCodeSplit: true, // 禁用 CSS 代码拆分,将整个项目中的所有 CSS 将被提取到一个 CSS 文件中
  7. brotliSize: false, // 关闭打包计算
  8. target: 'esnext',
  9. minify: 'terser', // 混淆器, terser 构建后文件体积更小, esbuild
  10. // 小于此阈值的导入或引用资源将内联为 base64 编码,以避免额外的 http 请求。设置为 0 可以完全禁用此项
  11. assetsInlineLimit: 4096,
  12. chunkSizeWarningLimit: 2000, // chunk 大小警告的限制(以 kbs 为单位)
  13. assetsDir: 'static', // 静态资源目录
  14. // rollup 打包配置
  15. rollupOptions: {
  16. output: {
  17. chunkFileNames: 'static/js/[name]-[hash].js',
  18. entryFileNames: 'static/js/[name]-[hash].js',
  19. assetFileNames: (chunkInfo) => {
  20. if (chunkInfo.name) {
  21. const info = chunkInfo.name.split('.');
  22. let extType = info[info.length - 1];
  23. if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(chunkInfo.name)) {
  24. extType = 'media';
  25. } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(chunkInfo.name)) {
  26. extType = 'images';
  27. } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(chunkInfo.name)) {
  28. extType = 'fonts';
  29. }
  30. return `static/${extType}/[name]-[hash][extname]`;
  31. }
  32. return 'static/[ext]/[name]-[hash].[ext]';
  33. }
  34. }
  35. },
  36. // 压缩配置
  37. terserOptions: {
  38. compress: {
  39. drop_console: false, // 生产环境移除console
  40. drop_debugger: true // 生产环境移除debugger
  41. }
  42. }
  43. };
  44. }