forked from Vendicated/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
83 lines (78 loc) · 3.03 KB
/
build.mjs
File metadata and controls
83 lines (78 loc) · 3.03 KB
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
import { context } from "esbuild";
import { readdirSync } from "fs";
import { open, readFile } from "fs/promises";
import { join } from "path";
const isDev = process.argv.includes("--dev");
const plugins = await Promise.all(
readdirSync("./src/plugins").map(p =>
context({
entryPoints: [`./src/plugins/${p}`],
outfile: `Plugins/${p}/${p}.plugin.js`,
minify: false,
bundle: true,
format: "cjs",
target: "esnext",
treeShaking: true,
jsxFactory: "BdApi.React.createElement",
jsxFragment: "BdApi.React.Fragment",
jsx: "transform",
tsconfig: "./tsconfig.esbuild.json",
logLevel: "info",
plugins: [
{
name: "manifest-banner",
async setup(build) {
build.initialOptions.banner = {
js: await readFile(`./src/plugins/${p}/meta.js`, "utf8")
};
}
},
{
name: "auto-deploy",
setup(build) {
build.onEnd(async result => {
if (!isDev) return;
if (result.errors.length) return;
const outFile = `${process.env.APPDATA}/BetterDiscord/plugins/${p}.plugin.js`;
const f = await open(outFile, "w");
try {
await f.write(await readFile(build.initialOptions.outfile));
console.info("Deployed", p);
} finally {
f.close();
}
});
}
},
{
name: "file-include-plugin",
setup: build => {
const filter = /^~fileContent\/.+$/;
build.onResolve({ filter }, args => ({
namespace: "include-file",
path: args.path,
pluginData: {
path: join(args.resolveDir, args.path.slice("include-file/".length))
}
}));
build.onLoad({ filter, namespace: "include-file" }, async ({ pluginData: { path } }) => {
const [name, format] = path.split(";");
let content = await readFile(name, format ?? "utf-8");
content = content.replace(/`/g, "\\`");
return {
contents: `export default \`${content}\``
};
});
}
}
]
})
)
);
for (const p of plugins) {
if (isDev) p.watch();
else {
p.rebuild();
p.dispose();
}
}