104 lines
2.3 KiB
JavaScript
104 lines
2.3 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
|
|
*/
|
|
`;
|
|
|
|
const prod = process.argv[2] === "production";
|
|
|
|
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",
|
|
...builtins,
|
|
],
|
|
format: "cjs",
|
|
target: "es2018",
|
|
logLevel: "info",
|
|
sourcemap: prod ? false : "inline",
|
|
treeShaking: true,
|
|
outfile: "build/main.js",
|
|
minify: prod,
|
|
plugins: [
|
|
{
|
|
name: "post-compile",
|
|
setup(build) {
|
|
build.onEnd((result) => {
|
|
if (result.errors.length === 0) {
|
|
copyFiles(
|
|
["manifest.json", "versions.json", ".hotreload"],
|
|
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
|
);
|
|
|
|
copyFiles(
|
|
"build",
|
|
"/mnt/c/Users/Andras/Desktop/test/test/.obsidian/plugins/my-plugin"
|
|
);
|
|
}
|
|
});
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
if (prod) {
|
|
await context.rebuild();
|
|
process.exit(0);
|
|
} else {
|
|
await context.watch();
|
|
}
|
|
|
|
async function copyFiles(sourceDir, destinationDir) {
|
|
try {
|
|
await fs.promises.mkdir(destinationDir, { recursive: true });
|
|
|
|
const paths = Array.isArray(sourceDir)
|
|
? sourceDir
|
|
: (await fs.promises.readdir(sourceDir)).map((file) =>
|
|
path.join(sourceDir, file)
|
|
);
|
|
|
|
await Promise.all(
|
|
paths.map(async (sourcePath) => {
|
|
const stat = await fs.promises.stat(sourcePath);
|
|
|
|
if (stat.isFile()) {
|
|
const destinationFile = path.join(
|
|
destinationDir,
|
|
path.basename(sourcePath)
|
|
);
|
|
await fs.promises.copyFile(sourcePath, destinationFile);
|
|
console.debug(`Copied ${sourcePath} to ${destinationFile}`);
|
|
} else {
|
|
console.info(`Skipping directory ${sourcePath}`);
|
|
}
|
|
})
|
|
);
|
|
|
|
console.info("All files copied successfully.");
|
|
} catch (err) {
|
|
console.error("Error copying files:", err);
|
|
}
|
|
}
|