From 13dbcca29c7d58968c8c429e25713b9fdaebb74f Mon Sep 17 00:00:00 2001 From: Mara Date: Sun, 11 Aug 2024 21:36:44 +0200 Subject: [PATCH] chore: upgrade building --- esbuild.config.mjs | 109 ++++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 3c4c2ca9..d3a08a09 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -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(); @@ -13,85 +27,109 @@ program .option("-p, --production", "Production build") .option("-v, --vault", "Use vault path") .option("-o, --output-dir ", "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", @@ -107,7 +145,8 @@ const context = await esbuild.context({ "@lezer/common", "@lezer/highlight", "@lezer/lr", - ...builtins], + ...builtins, + ], format: "cjs", target: "esnext", logLevel: "info", @@ -116,7 +155,7 @@ const context = await esbuild.context({ minifySyntax: prod, minifyWhitespace: prod, outdir, - plugins: [moveStyles, exportToDist, exportToVaultFunc], + plugins, }); if (prod) {