import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { VitePWA } from 'vite-plugin-pwa'

export default defineConfig({
  plugins: [
    vue(),
    VitePWA({
      // 自定义 SW：install 时逐个抓取资源并向页面广播进度（驱动首屏进度条）
      strategies: 'injectManifest',
      srcDir: 'src',
      filename: 'sw.ts',
      injectRegister: false, // 注册由 src/pwa.ts 手动处理，避免插件的自动重载
      includeAssets: ['favicon-48x48.png', 'apple-touch-icon-180x180.png'],
      manifest: {
        name: '狼人杀发牌器',
        short_name: '狼人杀',
        description: '离线可用的狼人杀发牌器',
        lang: 'zh-CN',
        theme_color: '#1a1a2e',
        background_color: '#1a1a2e',
        display: 'standalone',
        orientation: 'portrait',
        start_url: '/',
        scope: '/',
        icons: [
          { src: 'pwa-192x192.png', sizes: '192x192', type: 'image/png' },
          { src: 'pwa-512x512.png', sizes: '512x512', type: 'image/png' },
          {
            src: 'maskable-icon-512x512.png',
            sizes: '512x512',
            type: 'image/png',
            purpose: 'maskable',
          },
        ],
      },
      injectManifest: {
        // 预缓存应用外壳 + 全部牌图，保证彻底断网也能发牌
        globPatterns: ['**/*.{js,css,html,ico,png,svg,jpg,JPG,webmanifest}'],
        maximumFileSizeToCacheInBytes: 1024 * 1024,
      },
    }),
  ],
  build: {
    target: 'es2020',
  },
  test: {
    globals: true,
    environment: 'node',
  },
})
