|
| 1 | +import builtins from "builtin-modules"; |
| 2 | +import esbuild from "esbuild"; |
| 3 | +import esbuildSvelte from "esbuild-svelte"; |
| 4 | +import fs from "fs"; |
| 5 | +import path from "path"; |
| 6 | +import process from "process"; |
| 7 | +import { sveltePreprocess } from "svelte-preprocess"; |
| 8 | + |
| 9 | +const banner = `/* |
| 10 | +THIS IS A GENERATED/BUNDLED FILE BY ESBUILD |
| 11 | +if you want to view the source, please visit the github repository of this plugin |
| 12 | +*/ |
| 13 | +`; |
| 14 | + |
| 15 | +const prod = process.argv[2] === "production"; |
| 16 | + |
| 17 | +const rebuildPlugin = { |
| 18 | + name: "rebuild-handler", |
| 19 | + setup(build) { |
| 20 | + build.onEnd(async () => { |
| 21 | + try { |
| 22 | + // await fs.promises.rename( |
| 23 | + // path.join(path.resolve(), "dist", "main.css"), |
| 24 | + // path.join(path.resolve(), "dist", "styles.css") |
| 25 | + // ); |
| 26 | + await fs.promises.copyFile( |
| 27 | + path.join(path.resolve(), "manifest.json"), |
| 28 | + path.join(path.resolve(), "dist", "manifest.json") |
| 29 | + ); |
| 30 | + } catch (err) {} |
| 31 | + }); |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +const context = await esbuild.context({ |
| 36 | + banner: { |
| 37 | + js: banner, |
| 38 | + }, |
| 39 | + entryPoints: ["src/main.ts"], |
| 40 | + bundle: true, |
| 41 | + external: [ |
| 42 | + "obsidian", |
| 43 | + "electron", |
| 44 | + "@codemirror/autocomplete", |
| 45 | + "@codemirror/collab", |
| 46 | + "@codemirror/commands", |
| 47 | + "@codemirror/language", |
| 48 | + "@codemirror/lint", |
| 49 | + "@codemirror/search", |
| 50 | + "@codemirror/state", |
| 51 | + "@codemirror/view", |
| 52 | + "@lezer/common", |
| 53 | + "@lezer/highlight", |
| 54 | + "@lezer/lr", |
| 55 | + ...builtins, |
| 56 | + ], |
| 57 | + format: "cjs", |
| 58 | + target: "es2018", |
| 59 | + logLevel: "info", |
| 60 | + sourcemap: prod ? false : "inline", |
| 61 | + treeShaking: true, |
| 62 | + outfile: "dist/main.js", |
| 63 | + plugins: [ |
| 64 | + rebuildPlugin, |
| 65 | + esbuildSvelte({ |
| 66 | + compilerOptions: { |
| 67 | + css: "injected", |
| 68 | + }, |
| 69 | + preprocess: sveltePreprocess(), |
| 70 | + }), |
| 71 | + ], |
| 72 | +}); |
| 73 | + |
| 74 | +if (prod) { |
| 75 | + await context.rebuild(); |
| 76 | + process.exit(0); |
| 77 | +} else { |
| 78 | + await context.watch(); |
| 79 | +} |
0 commit comments