import esbuild from "esbuild"; import process from "process"; import builtins from "builtin-modules"; const prod = process.argv[2] === "production"; // Rewrite bare "better-sqlite3" require to resolve via Obsidian's vault path. // __dirname is Electron's internal asar path, not the plugin dir, so we use // the global `app` object to construct the absolute path at runtime. const nativeModulePlugin = { name: "native-module-resolver", setup(build) { build.onResolve({ filter: /^better-sqlite3$/ }, () => ({ path: "better-sqlite3", namespace: "native-module", })); build.onLoad({ filter: /.*/, namespace: "native-module" }, () => ({ contents: ` const path = require("path"); const pluginDir = path.join( app.vault.adapter.basePath, app.vault.configDir, "plugins", "logfire" ); module.exports = require(path.join(pluginDir, "node_modules", "better-sqlite3")); `, loader: "js", })); }, }; const context = await esbuild.context({ entryPoints: ["src/main.ts"], bundle: true, plugins: [nativeModulePlugin], 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: "es2022", logLevel: "info", sourcemap: prod ? false : "inline", treeShaking: true, outfile: "main.js", minify: prod, }); if (prod) { await context.rebuild(); process.exit(0); } else { await context.watch(); }