generated from tpl/obsidian-sample-plugin
105 lines
2.3 KiB
JavaScript
105 lines
2.3 KiB
JavaScript
import esbuild from "esbuild";
|
|
import process from "process";
|
|
import builtins from "builtin-modules";
|
|
import esbuildSvelte from "esbuild-svelte";
|
|
import { sveltePreprocess } from "svelte-preprocess";
|
|
import dotenv from "dotenv";
|
|
import dotenvExpand from "dotenv-expand";
|
|
import manifest from "./manifest.json" with { type: "json" };
|
|
import fs from "fs/promises";
|
|
import path from "path";
|
|
|
|
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 context = await esbuild.context({
|
|
banner: {
|
|
js: banner,
|
|
},
|
|
entryPoints: ["src/main.ts"],
|
|
bundle: true,
|
|
plugins: [
|
|
esbuildSvelte({
|
|
preprocess: sveltePreprocess(),
|
|
compilerOptions: { dev: !prod },
|
|
}),
|
|
{
|
|
name: 'copy-plugin',
|
|
setup(build) {
|
|
build.onEnd(async () => {
|
|
try {
|
|
await fs.copyFile(new URL('./manifest.json', import.meta.url), path.resolve(outDir, 'manifest.json'));
|
|
} catch (e) {
|
|
console.error('Failed to rename file:', e);
|
|
}
|
|
});
|
|
},
|
|
},
|
|
{
|
|
name: 'rename-plugin',
|
|
setup(build) {
|
|
build.onEnd(async () => {
|
|
try {
|
|
await fs.rename(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,
|
|
],
|
|
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();
|
|
}
|