-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathesbuild.config.mjs
145 lines (130 loc) · 3.59 KB
/
esbuild.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import esbuild from "esbuild";
import process from "process";
import esbuildSvelte from "esbuild-svelte";
import sveltePreprocess from "svelte-preprocess";
import builtins from "builtin-modules";
import { execSync } from "child_process";
import chokidar from "chokidar";
import path from "path";
import fs, { mkdirSync } from "fs";
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 gitTag = execSync("git describe --tags --always", {
encoding: "utf8",
}).trim();
const staging = process.argv[2] === "staging";
const watch = process.argv[2] === "watch" || staging;
const debug = process.argv[2] === "debug" || watch || staging
const out = process.argv[3] || ".";
const tld = staging ? "dev": "md"
const apiUrl = `https://api.system3.${tld}`;
const authUrl = `https://auth.system3.${tld}`;
const healthUrl = `${apiUrl}/health?version=${gitTag}`;
console.log("git tag:", gitTag);
console.log("health URL", healthUrl);
const NotifyPlugin = {
name: "on-end",
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) execSync(`notify-send "Build Failed"`);
});
},
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["src/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",
plugins: [
esbuildSvelte({
compilerOptions: { css: true },
preprocess: sveltePreprocess(),
}),
NotifyPlugin,
],
target: "es2018",
minify: !debug,
logLevel: "info",
sourcemap: debug ? "inline" : false,
define: {
BUILD_TYPE: debug ? '"debug"' : '"prod"',
GIT_TAG: `"${gitTag}"`,
HEALTH_URL: `"${healthUrl}"`,
API_URL: `"${apiUrl}"`,
AUTH_URL: `"${authUrl}"`,
},
treeShaking: true,
outfile: out + "/main.js",
});
const copyFile = (src, dest) => {
if (src === dest) {
return;
}
fs.copyFileSync(src, dest);
console.log(`Copied ${src} to ${dest}`);
};
const watchAndMove = (fnames, mapping) => {
// only usable on top level directory
const watcher = chokidar.watch(fnames, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
});
watcher.on("change", (filePath) => {
const destName = mapping[filePath] || filePath;
const destPath = path.join(out, path.basename(destName));
copyFile(filePath, destPath);
});
};
const mapping = debug ? { "manifest-beta.json": "manifest.json" } : {};
const manifest = debug ? "manifest-beta.json" : "manifest.json";
const files = ["styles.css", manifest];
const updateManifest = (manifest) => {
const manifestPath = path.join(out, path.basename('manifest.json'));
const raw_manifest = fs.readFileSync(manifestPath);
const parsed = JSON.parse(raw_manifest);
parsed.version = gitTag;
const new_manifest = JSON.stringify(parsed, null, 2);
console.log(`Set ${manifest} version to ${gitTag}`)
fs.writeFileSync(manifestPath, new_manifest);
};
const move = (fnames, mapping) => {
// only usable on top level directory
mkdirSync(out, { recursive: true });
for (const fname of fnames) {
const destName = mapping[fname] || fname;
const destPath = path.join(out, path.basename(destName));
copyFile(fname, destPath);
}
};
if (watch) {
await context.watch();
move(files, mapping);
updateManifest();
watchAndMove(files, mapping);
} else {
await context.rebuild();
move(files, mapping);
updateManifest();
process.exit(0);
}