import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import esbuildSvelte from "esbuild-svelte";
import {sveltePreprocess} from "svelte-preprocess";
import { sassPlugin } from "esbuild-sass-plugin";
import { replace } from "esbuild-plugin-replace";
import fs from "node:fs";

const banner =
  `/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;

const prod = (process.argv[2] === "production");

const context = await esbuild.context({
  banner: {
    js: banner
  },
  outdir: ".",
  entryPoints: ["src/main.ts", "src/styles.scss"],
  bundle: true,
  external: [
    "obsidian",
    "electron",
    "@codemirror/autocomplete",
    "@codemirror/collab",
    "@codemirror/commands",
    "@codemirror/language",
    "@codemirror/lint",
    "@codemirror/search",
    "@codemirror/state",
    "@codemirror/view",
    "@lezer/common",
    "@lezer/highlight",
    "@lezer/lr",
    ...builtins],
  format: "cjs",
  target: "es2018",
  logLevel: "info",
  sourcemap: prod ? false : "inline",
  treeShaking: true,
  conditions: ['svelte'],
  plugins: [
    sassPlugin(),
    esbuildSvelte({
      compilerOptions: { css: "injected", dev: !prod },
      preprocess: sveltePreprocess(),
      include: /\.svelte(\.ts)?$/,
    }),
    replace({
      include: /release-notes-modal\.ts|main\.ts/,
      changelogMd: JSON.stringify(fs.readFileSync("./CHANGELOG.md", "utf-8")),
      currentPluginVersion: JSON.stringify(JSON.parse(fs.readFileSync("./package.json", "utf-8")).version)
    })
  ]
});

if (prod) {
  await context.rebuild();
  process.exit(0);
} else {
  await context.watch();
}
