import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import * as fs from 'node:fs'

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin(),
      {
        name: 'preserve-native-modules', closeBundle:() => {
          const libDir=resolve('hardware_monitor_wrapper/build/Release/hardware_monitor.node')
          const sourceDir = resolve('resources/hardware_monitor'); // node-gyp 编译生成的路径
          const destDir = resolve( 'out');
          fs.mkdirSync(destDir, { recursive: true });
          fs.copyFileSync(
            resolve(libDir),
            resolve(destDir, "hardware_monitor.node")
          );
          let files=fs.readdirSync(resolve(sourceDir));
          for(let i=0;i<files.length;i++){
            fs.copyFileSync(
              resolve(sourceDir, files[i]),
              resolve(destDir, files[i])
            );
          }
        }
    }],
    build:{
      rollupOptions: {
        external: ["../hardware_monitor.node"],
      }
    }
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src')
      }
    },
    plugins: [vue()],
    assetsInclude: ["**/*.node", "**.*.lib", "**/*.dll", "**/*.xml"],
  }
})
