Skip to content

Commit

Permalink
chore: upgrade building
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Aug 11, 2024
1 parent 425f859 commit 13dbcca
Showing 1 changed file with 74 additions and 35 deletions.
109 changes: 74 additions & 35 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import esbuild from "esbuild";
import * as fs from "fs";
import * as path from "path";
import process from "process";
import packageJson from "./package.json" with { type: "json" };
import manifest from "./manifest.json" with { type: "json" };

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: ${packageJson.repository}
*/
`;

function cleanOutdir(outdir) {
if (fs.existsSync(outdir)) {
fs.rm(outdir, { recursive: true });
}
}

dotenv.config();

Expand All @@ -13,85 +27,109 @@ program
.option("-p, --production", "Production build")
.option("-v, --vault", "Use vault path")
.option("-o, --output-dir <path>", "Output path")
.option("-b, --beta", "Pre-release version")
.parse();

program.parse();

/** OPTIONS */
const opt = program.opts();
const prod = opt.production ?? false;
const exportToVault = opt.vault ?? !!opt.outputDir ?? false;

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 exportToVault = opt.vault ?? false;

const manifest = JSON.parse(fs.readFileSync("./manifest.json", "utf-8"));
/** VARIABLES **/
const isStyled = fs.existsSync("src/styles.css");
const pluginID = manifest.id;
const vaultPath = opt.outputDir ?? process.env.VAULT;
const folderPlugin = vaultPath ? path.join(vaultPath, ".obsidian", "plugins", pluginID) : undefined;

/** FOLDER PATHS **/
const vaultPath = process.env.VAULT;
const folderPlugin = vaultPath
? path.join(vaultPath, ".obsidian", "plugins", pluginID)
: undefined;

if (vaultPath && exportToVault && !fs.existsSync(folderPlugin)) {
fs.mkdirSync(folderPlugin, { recursive: true });
}
if (opt.beta && !fs.existsSync("manifest-beta.json")) {
fs.copyFileSync("manifest.json", "manifest-beta.json");
}

let outdir = "./";
if (opt.outputDir && prod) {
if (opt.outputDir) {
outdir = opt.outputDir;
cleanOutdir(outdir);
} else if (exportToVault) {
outdir = folderPlugin;
if (!prod) fs.writeFileSync(path.join(folderPlugin, ".hotreload"), "");
} else if (prod) {
outdir = "./dist";
//clean dist if
cleanOutdir(outdir);
}



/**
* Move styles.css to output directory
*/
const moveStyles = {
name: "move-styles",
setup(build) {
build.onEnd(() => {
if (fs.existsSync("src/styles.css")) fs.copyFileSync("src/styles.css", "./styles.css");
fs.copyFileSync("src/styles.css", "./styles.css");
});
}
},
};

/**
* Export to vault if set in environment variable
*/
const exportToVaultFunc = {
name: "export-to-vault",
setup(build) {
build.onEnd(() => {
if (!(prod && exportToVault)) {
return;
}
if (!folderPlugin) {
console.error("VAULT environment variable not set, skipping export to vault");
return;
}
if (!folderPlugin)
throw new Error("VAULT environment variable not set, skipping export to vault");

fs.copyFileSync(`${outdir}/main.js`, path.join(folderPlugin, "main.js"));
if (fs.existsSync(`${outdir}/styles.css`)) fs.copyFileSync("./styles.css", path.join(folderPlugin, "styles.css"));
fs.copyFileSync("./manifest.json", path.join(folderPlugin, "manifest.json"));
if (fs.existsSync(`${outdir}/styles.css`))
fs.copyFileSync("./styles.css", path.join(folderPlugin, "styles.css"));
if (opt.beta) fs.copyFileSync("manifest-beta.json", path.join(folderPlugin, "manifest.json"));
else fs.copyFileSync("./manifest.json", path.join(folderPlugin, "manifest.json"));
});
}
},
};

/**
* Export to production folder
*/
const exportToDist = {
name: "export-to-dist",
setup(build) {
build.onEnd(() => {
if (!prod) {
return;
}
fs.copyFileSync("manifest.json", path.join(outdir, "manifest.json"));
if (opt.beta) fs.copyFileSync("manifest-beta.json", path.join(outdir, "manifest.json"));
else fs.copyFileSync("manifest.json", path.join(outdir, "manifest.json"));
});
}
},
};

/**
* ENTRIES *
*/
const entryPoints = ["src/main.ts"];
if (isStyled) entryPoints.push("src/styles.css");

/** PLUGINS **/
const plugins = [];
if (isStyled) plugins.push(moveStyles);
if (prod) plugins.push(exportToDist);
if (prod && exportToVault) plugins.push(exportToVaultFunc);

/**
* BUILD
*/
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/main.ts", "src/styles.css"],
entryPoints,
bundle: true,
external: [
"obsidian",
Expand All @@ -107,7 +145,8 @@ const context = await esbuild.context({
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins],
...builtins,
],
format: "cjs",
target: "esnext",
logLevel: "info",
Expand All @@ -116,7 +155,7 @@ const context = await esbuild.context({
minifySyntax: prod,
minifyWhitespace: prod,
outdir,
plugins: [moveStyles, exportToDist, exportToVaultFunc],
plugins,
});

if (prod) {
Expand Down

0 comments on commit 13dbcca

Please sign in to comment.