devonthesofa_obsidian-note-.../esbuild.config.mjs

115 lines
No EOL
2.7 KiB
JavaScript

import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import fs from "fs";
import path from "path";
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
*/
`;
/**
* Automatically generates the predefinedTemplates.ts file by scanning the templates directory.
*/
const generateTemplatesIndex = () => {
const templatesDir = "./templates";
const outputFile = "./constants/predefinedTemplates.ts";
if (!fs.existsSync(templatesDir)) return;
const files = fs.readdirSync(templatesDir);
const templates = [];
files.forEach((file) => {
if (file.endsWith(".json")) {
const filePath = path.join(templatesDir, file);
const content = JSON.parse(fs.readFileSync(filePath, "utf8"));
templates.push(content);
}
});
const content = `/**
* THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT MANUALLY.
* To add a new template, create a JSON file in the /templates directory.
*/
import { StatusTemplate } from "types/pluginSettings";
export const PREDEFINED_TEMPLATES: StatusTemplate[] = ${JSON.stringify(templates, null, "\t")};
`;
fs.writeFileSync(outputFile, content);
console.log(
`\x1b[36m[Templates]\x1b[0m Automatically registered ${templates.length} templates.`,
);
};
// Generate templates index before build
generateTemplatesIndex();
const prod = process.argv[2] === "production";
// CSS bundling function
const buildStyles = async () => {
await esbuild.build({
entryPoints: ["styles/index.css"],
bundle: true,
outfile: "styles.css",
minify: prod,
logLevel: "info",
});
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.tsx"],
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,
outfile: "main.js",
minify: prod,
jsx: "transform",
jsxFactory: "React.createElement",
jsxFragment: "React.Fragment",
});
// CSS context for watch mode
const cssContext = await esbuild.context({
entryPoints: ["styles/index.css"],
bundle: true,
outfile: "styles.css",
minify: prod,
logLevel: "info",
});
if (prod) {
await context.rebuild();
await buildStyles(); // Build CSS in production mode
process.exit(0);
} else {
await context.watch();
await cssContext.watch(); // Watch CSS files in development mode
}