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.

45 lines
2.1 KiB

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