import type { BuildOptions } from 'vite';
/**
 * 自定义底层的 rolldown 打包配置
 */
export function rolldownOptions(): BuildOptions['rolldownOptions'] {
  return {
    output: {
      // 拆分代码
      manualChunks: (id: string) => {
        const lastPath = id;
        if (lastPath.includes('node_modules')) {
          if (lastPath.includes('pinia')) {
            return 'store';
          }
          if (lastPath.includes('element-plus')) {
            return 'element-plus';
          }
          if (lastPath.includes('echarts')) {
            return 'echarts';
          }
          if (lastPath.includes('vue')) {
            return 'vue';
          }
        }
        return undefined;
      },
      chunkFileNames: 'js/[name]-[hash].js',
      entryFileNames: 'js/[name]-[hash].js',
      // assetFileNames: '[ext]/[name]-[hash].[ext]',
      assetFileNames: (assetInfo) => {
        const ext: string = assetInfo.name?.split('.').pop() || '';
        let extType: string = ext;
        if (['mp4', 'webm', 'ogg', 'mp3', 'wav', 'flac', 'aac'].includes(ext)) {
          extType = 'media';
        } else if (['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico'].includes(ext)) {
          extType = 'images';
        } else if (['woff', 'woff2', 'ttf', 'eot', 'otf'].includes(ext)) {
          extType = 'fonts';
        } else if (ext === 'css') {
          extType = 'css';
        } else {
          extType = 'assets';
        }
        return `${extType}/[name]-[hash].[ext]`;
      },
    },
  };
}
