import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'
import Uni from '@dcloudio/vite-plugin-uni'
import UniKuRoot from '@uni-ku/root'
import { resolve } from 'node:path'
import UnoCSS from 'unocss/vite'

// 获取当前时间戳
const timestamp = new Date().getTime()

export default defineConfig({
  envDir: './env', // 自定义env目录
  resolve: {
    alias: [
      {
        find: '@',
        replacement: resolve(process.cwd(), 'src'),
      },
      {
        find: '@/static',
        replacement: resolve(__dirname, 'src/static'),
      },
    ],
  },
  server: {
    host: '0.0.0.0', // 监听所有网络接口
    port: 2367,
    // 选项写法
    proxy: {
      '/pag': {
        target: 'https://cdn.tmui.design',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
    },
  },
  plugins: [
    Uni(),
    UniKuRoot(),
    UnoCSS(),
    AutoImport({
      imports: [
        'vue',
        {
          'naive-ui': [
            'useDialog',
            'useMessage',
            'useNotification',
            'useLoadingBar',
          ],
        },
      ],
    }),
    Components({
      resolvers: [NaiveUiResolver()],
    }),
  ],
  build: {
    rollupOptions: {
      output: {
        entryFileNames(chunkInfo) {
          return `static/js/${chunkInfo.name}.${timestamp}.js`
        },
        //需要注释掉 @dcloudio/uni-h5-vite/dist/plugin/config.js 中的同名方法，不然不会生效
        chunkFileNames: (chunkInfo) => {
          const facadeModuleId = chunkInfo.facadeModuleId
            ? chunkInfo.facadeModuleId.split('/')
            : []
          //console.log(facadeModuleId);
          if (chunkInfo.facadeModuleId?.length) {
            const fileName = `pages-${
              facadeModuleId[facadeModuleId.length - 2]
            }-${facadeModuleId[facadeModuleId.length - 1]}`
            console.log('正在打包' + fileName)
            return `static/js/${fileName}.${timestamp}.js`
          } else {
            console.log('正在打包', `static/js/[name].${timestamp}.js`)
            return `static/js/[name].${timestamp}.js`
          }
        },
        // 资源文件像 字体，图片等
        assetFileNames: (assetInfo) => {
          if (
            assetInfo.name &&
            assetInfo.type === 'asset' &&
            /\.(jpe?g|png|gif|svg)$/i.test(assetInfo.name)
          ) {
            return 'static/images/[name].[hash].[ext]'
          }
          if (
            assetInfo.name &&
            assetInfo.type === 'asset' &&
            /\.(ttf|woff|woff2|eot)$/i.test(assetInfo.name)
          ) {
            return 'static/fonts/[name].[hash].[ext]'
          }
          return 'static/[ext]/[name]-[hash].[ext]'
        },
      },
    },
  },
  css: {
    extract: {
      // css重构 打包编译后的css文件名称,添加时间戳
      filename: `css/[name].${timestamp}.css`,
      chunkFilename: `css/[name].${timestamp}.css`,
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@import "@/static/css/color.scss";`,
      },
    },
  },
})
