diff --git a/.esbuild.mjs b/.esbuild.mjs new file mode 100644 index 0000000..44b4713 --- /dev/null +++ b/.esbuild.mjs @@ -0,0 +1,42 @@ +import { appendFileSync } from "node:fs"; +import builtins from "builtin-modules"; +import esbuild from "esbuild"; + +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 production = process.argv[2] === "production"; +const analyze = process.argv[2] === "analyze"; + +//────────────────────────────────────────────────────────────────────────────── + +const result = await esbuild + .build({ + entryPoints: ["src/main.ts"], + banner: { js: banner + "\n" }, + outfile: "main.js", + bundle: true, + // biome-ignore format: no need to inspect this regularly + 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: "es2022", + sourcemap: production || analyze ? false : "inline", + minify: production || analyze, + drop: ["debugger"], + treeShaking: true, + logLevel: analyze ? "silent" : "info", + metafile: analyze, + }) + .catch(() => process.exit(1)); + +//────────────────────────────────────────────────────────────────────────────── + +// DOCS https://esbuild.github.io/api/index#metafile +if (result.metafile) { + const sizes = await esbuild.analyzeMetafile(result.metafile, { verbose: false }); + console.info(sizes); +} + +// FIX prevent Obsidian from removing the source map when using dev build +// https://forum.obsidian.md/t/source-map-trimming-in-dev-builds/87612 +if (!production) appendFileSync(import.meta.dirname + "/main.js", "\n/* nosourcemap */"); diff --git a/esbuild.config.mjs b/esbuild.config.mjs deleted file mode 100644 index dafd09e..0000000 --- a/esbuild.config.mjs +++ /dev/null @@ -1,29 +0,0 @@ -import esbuild from "esbuild"; -import process from "process"; -import builtins from 'builtin-modules' - -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'); - -esbuild.build({ - banner: { - js: banner, - }, - entryPoints: ['src/main.ts'], - bundle: true, - external: ['obsidian', 'electron', ...builtins], - format: 'cjs', - watch: !prod, - target: 'es2021', - logLevel: "info", - sourcemap: prod ? false : 'inline', - treeShaking: true, - minify: prod, - outfile: 'main.js', -}).catch(() => process.exit(1));