callumalpass_tasknotes/esbuild.config.mjs
2026-05-14 21:06:30 +10:00

103 lines
2.6 KiB
JavaScript

import esbuild from "esbuild";
import process from "process";
import { builtinModules } from "node:module";
import { readFileSync, existsSync, copyFileSync, mkdirSync } from "fs";
import { resolve, join } from "path";
import { homedir } from "os";
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");
// Read copy destinations from .copy-files.local (same logic as copy-files.mjs)
function getCopyDestinations() {
const localFile = resolve(process.cwd(), '.copy-files.local');
if (!existsSync(localFile)) return [];
const expandTilde = (p) => p.startsWith('~/') ? join(homedir(), p.slice(2)) : p;
return readFileSync(localFile, 'utf8')
.split('\n')
.map(p => p.trim())
.filter(p => p && !p.startsWith('#'))
.map(expandTilde);
}
// esbuild plugin: copy build outputs to vault after each rebuild
const copyToVaultPlugin = {
name: 'copy-to-vault',
setup(build) {
const destinations = getCopyDestinations();
if (destinations.length === 0) return;
const filesToCopy = ['main.js', 'styles.css', 'manifest.json'];
build.onEnd((result) => {
if (result.errors.length > 0) return;
for (const dest of destinations) {
mkdirSync(dest, { recursive: true });
for (const file of filesToCopy) {
const src = resolve(process.cwd(), file);
if (existsSync(src)) {
copyFileSync(src, join(dest, file));
}
}
console.log(`\x1b[32m✓ Copied to ${dest}\x1b[0m`);
}
});
}
};
// Plugin to import markdown files as strings
const markdownPlugin = {
name: 'markdown',
setup(build) {
build.onLoad({ filter: /\.md$/ }, async (args) => {
const text = readFileSync(args.path, 'utf8');
return {
contents: `export default ${JSON.stringify(text)}`,
loader: 'js',
};
});
}
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
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",
...builtinModules,
],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
minify: prod,
plugins: [markdownPlugin, ...(!prod ? [copyToVaultPlugin] : [])],
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}