obsidian-book-tracker/esbuild.config.mjs

130 lines
2.8 KiB
JavaScript

import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import esbuildSvelte from "esbuild-svelte";
import { sassPlugin } from "esbuild-sass-plugin";
import { sveltePreprocess } from "svelte-preprocess";
import dotenv from "dotenv";
import dotenvExpand from "dotenv-expand";
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
const manifest = JSON.parse(fs.readFileSync("manifest.json", "utf8"));
const env = dotenv.config();
dotenvExpand.expand(env);
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";
let outDir = "dist";
if (!prod) {
if (!process.env.OBSIDIAN_PLUGIN_DIR) {
console.log(
"Set OBSIDIAN_PLUGIN_DIR in the .env file to plugin directory to copy files to."
);
} else {
outDir = process.env.OBSIDIAN_PLUGIN_DIR + "/" + manifest.id + "-dev";
}
}
if (outDir[0] === "~") {
outDir = path.join(process.env.HOME, outDir.slice(1));
}
const commitHash = execSync("git rev-parse --short HEAD");
const devManifest = {
...manifest,
id: `${manifest.id}-dev`,
name: `${manifest.name} (Dev)`,
version: `dev-${commitHash}`,
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/main.ts"],
bundle: true,
plugins: [
esbuildSvelte({
preprocess: sveltePreprocess(),
compilerOptions: {
dev: !prod,
warningFilter: (warning) =>
!warning.filename?.includes("node_modules"),
},
}),
sassPlugin(),
{
name: "copy-plugin",
setup(build) {
build.onEnd(() => {
try {
console.log("Writing manifest.json");
fs.writeFileSync(
path.resolve(outDir, "manifest.json"),
JSON.stringify(prod ? manifest : devManifest)
);
} catch (e) {
console.error("Failed to rename file:", e);
}
});
},
},
{
name: "rename-plugin",
setup(build) {
build.onEnd(async () => {
try {
console.log("Renaming main.css to styles.css");
fs.renameSync(
path.resolve(outDir, "main.css"),
path.resolve(outDir, "styles.css")
);
} catch (e) {
console.error("Failed to rename file:", e);
}
});
},
},
],
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,
],
conditions: ["svelte"],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outdir: outDir,
minify: prod,
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}