import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import { builtinModules } from "node:module";
import { readFileSync } from "node:fs";
import path from 'node:path';

// Load dependencies
const pkgJson = JSON.parse(
  readFileSync(path.resolve(process.cwd(), "package.json"), "utf8")
);
const deps = Object.keys(pkgJson.dependencies || {});


export default {
  input: "src/index.ts",

  external: id =>
      id.startsWith(".") ? false :
      id.startsWith("node:") ||
      builtinModules.includes(id) ||
      deps.includes(id),


  output: [
    {
      file: "dist/index.js",
      format: "esm",
      sourcemap: true
    },
  ],

  plugins: [
    typescript({
      tsconfig: "./tsconfig.json",
      importHelpers: true,
      noEmitHelpers: true
    }),

    resolve({
      extensions: [".ts", ".js"],
      mainFields: ["module", "main"],
      preferBuiltins: false
    }),

    commonjs({
      include: [/node_modules/, /\/usr\/share\/nodejs\//],
      requireReturnsDefault: "auto",
      defaultIsModuleExports: true
    }),
  ]
};
