const { defineConfig } = require('@vue/cli-service');
const path = require('path');
const PrerenderSPAPlugin = require('@dreysolano/prerender-spa-plugin');
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const SitemapWebpackPlugin = require('sitemap-webpack-plugin').default;
const CompressionPlugin = require('compression-webpack-plugin');

module.exports = defineConfig({
  transpileDependencies: true,
  
  devServer: {
    hot: true, // 启用HMR
    liveReload: true,   // 启用实时重载
    client: {
      overlay: false // 禁用错误遮罩层
    }
  },

  configureWebpack: config => {
    const baseConfig = {
      cache: {
        type: 'filesystem', // 使用文件系统缓存 加快二次构建速度 减少重复编译
        buildDependencies: {
          config: [__filename]
        },
         // 添加缓存配置
         cacheDirectory: path.resolve(__dirname, 'node_modules/.cache/webpack'),
         name: process.env.NODE_ENV // 区分环境
      },
      optimization: {
          moduleIds: 'deterministic',
        splitChunks: { // 代码分割配置文件系统缓存比内存缓存更持久 可以在多次构建之间复用编译结果
          chunks: 'all', 
          minSize: 30000,   // 最小分块大小 
          maxSize: 300000, // 最大分块大小 避免单个文件过大
          cacheGroups: { // 缓存组配置
            vendors: {  // 第三方库分组
              name: 'chunk-vendors',
              test: /[\\/]node_modules[\\/]/,
              priority: -10,
              chunks: 'initial'
            },
            common: { // 公共模块分组
              name: 'chunk-common',
              minChunks: 2,
              priority: -20,
              chunks: 'initial',
              reuseExistingChunk: true
            }
          },
         
        }
      },
      plugins: [ //压缩静态资源 减少传输体积 提升加载速度
        new CompressionPlugin({
          test: /\.(js|css|html|svg)$/,
          threshold: 10240,
          minRatio: 0.8
        })
      ]
    };

    if (process.env.NODE_ENV === 'production') {
      // 优化预渲染插件配置
      const prerenderPlugin = new PrerenderSPAPlugin({
        staticDir: path.join(__dirname, 'dist'),
        // 只预渲染最重要的路由
        routes: ['/zh', '/en','/en/dataDisplay', '/zh/dataDisplay','/zh/rate', '/en/rate','/zh/ServiceTerms', '/en/ServiceTerms','/zh/apiFile', '/en/apiFile'],
        renderer: new Renderer({
          renderAfterTime: 1000, // 增加等待时间，确保内容加载完成
          maxConcurrentRoutes:2, // 降低并发数，避免资源竞争
          timeout: 10000, // 增加超时时间
          headless: true,
          
         
         
        
        }),
     
      });

     
      const zhSitemapPlugin = new SitemapWebpackPlugin({
        base: 'https://m2pool.com/zh',
        paths: [
          {
            path: '/zh',          
            changefreq: 'daily',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/dataDisplay',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/ServiceTerms',        
            changefreq: 'monthly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/apiFile',            
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/rate',          
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/nexaAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/grsAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/monaAccess',          
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/dgbsAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/dgbqAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/dgboAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/zh/AccessMiningPool/rxdAccess',            
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
        ],
        options: {
          filename: 'sitemap-zh.xml',
          lastmod: true,
          changefreq: 'weekly',
          priority: 0.7
        }
      });

      const enSitemapPlugin = new SitemapWebpackPlugin({
        base: 'https://m2pool.com/en',
        paths: [
          {
            path: '/en',
            priority: 1.0,
            changefreq: 'daily',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/dataDisplay',
            priority: 0.8,
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/ServiceTerms',
            priority: 0.6,
            changefreq: 'monthly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/apiFile',
            priority: 0.7,
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/rate',
            priority: 0.8,
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/nexaAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/grsAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/monaAccess',          
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/dgbsAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/dgbqAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/dgboAccess',           
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
          {
            path: '/en/AccessMiningPool/rxdAccess',            
            changefreq: 'weekly',
            lastmod: new Date().toISOString()
          },
         
        ],
        options: {
          filename: 'sitemap-en.xml',
          lastmod: true,
          changefreq: 'weekly',
          priority: 0.7
        }
      });
      
     
      baseConfig.plugins.push(zhSitemapPlugin);
      baseConfig.plugins.push(enSitemapPlugin);
      baseConfig.plugins.push(prerenderPlugin);
      
    }

    return baseConfig;
  },

  chainWebpack: config => {
    // HTML压缩配置
    config.plugin('html').tap(args => {
      args[0].minify = {
        removeComments: true,  // 删除 HTML 注释减小文件体积
        collapseWhitespace: true,  // 压缩空白字符 删除多余的空格和换行
        removeAttributeQuotes: true, // 删除属性的引号
        collapseBooleanAttributes: true,  // 简化布尔属性
        removeScriptTypeAttributes: true //  删除默认的 script type 属性type="text/javascript"
      };
      // META 标签配置
      args[0].meta = {
         // 移动端视口配置
        viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
        'format-detection': 'telephone=no',// 禁用电话号码自动识别
        'apple-mobile-web-app-capable': 'yes' // 支持添加到 iOS 主屏幕
      };
      return args;
    });

    // 移除 prefetch 插件
    //禁用 Vue CLI 默认的预取功能 prefetch 会在主资源加载完后，
    // 自动下载所有其他路由的资源对于大型应用，可能会预加载过多不必要的资源
    config.plugins.delete('prefetch')
  },
  //生产环境不生成 source map 文件源码映射信息 减小打包体积 提高网站安全性
  productionSourceMap: false, 
  
  css: {
    extract: true, //组件中的 CSS 提取至独立的 CSS 文件中 支持并行加载，减少页面阻塞
    sourceMap: false,// CSS 不启用 source maps 减少构建文件大小
    loaderOptions: {
      css: {}
    }
  }
});