import { defineConfig } from 'tsup';
// import glob from 'fast-glob';
// const services = glob.sync('src/services/*.ts');
import fs from 'fs';

const clean = () => {
  const distDir = 'dist';
  if (fs.existsSync(distDir)) {
    fs.rmSync(distDir, { recursive: true, force: true });
  }
};
clean();

const entrys = ['src/index.ts'];
const nodeEntrys = ['src/dev.ts'];

const getCommonConfig = (opts = {}) => {
  return {
    entry: opts.entry,
    outExtension: ({ format }) => ({
      js: format === 'esm' ? '.mjs' : '.js',
    }),
    splitting: false,
    sourcemap: false,
    // clean: true,
    format: 'esm',
    external: ['dotenv'],
    dts: true,
    outDir: 'dist',
    tsconfig: 'tsconfig.json',
    ...opts,
    define: {
      'process.env.IS_BROWSER': JSON.stringify(process.env.BROWSER || false),
      ...opts.define,
    },
  };
};
export default defineConfig([
  // getCommonConfig({ entry: entrys, define: { 'process.env.IS_BROWSER': JSON.stringify(true) } }), // 浏览器
  getCommonConfig({ entry: nodeEntrys, define: { 'process.env.IS_BROWSER': JSON.stringify(false) } }), // node
]);
