forked from twnlink/neptune-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
102 lines (92 loc) · 2.86 KB
/
build.js
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
const esbuild = require("esbuild");
const fs = require("node:fs");
const path = require("node:path");
const crypto = require("node:crypto");
const nativeExternals = ["@neptune", "@plugin", "electron"];
const plugins = fs.readdirSync("./plugins");
for (const plugin of plugins) {
const pluginPath = path.join("./plugins/", plugin);
const pluginManifest = JSON.parse(
fs.readFileSync(path.join(pluginPath, "plugin.json")),
);
const outfile = path.join(pluginPath, "dist/index.js");
esbuild
.build({
entryPoints: [
`./${path.join(pluginPath, pluginManifest.main ?? "index.js")}`,
],
plugins: [
{
name: "neptuneNativeImports",
setup(build) {
build.onLoad(
{ filter: /.*[\/\\].+\.native\.[a-z]+/g },
async (args) => {
const result = await esbuild.build({
entryPoints: [args.path],
bundle: true,
minify: true,
platform: "node",
format: "iife",
globalName: "neptuneExports",
write: false,
external: nativeExternals,
});
const outputCode = result.outputFiles[0].text;
// HATE! WHY WHY WHY WHY WHY (globalName breaks metafile. crying emoji)
const { metafile } = await esbuild.build({
entryPoints: [args.path],
platform: "node",
write: false,
metafile: true,
bundle: true, // I find it annoying that I have to enable bundling.
format: "esm", // This avoids exports not being properly defined, thus you do not need to change log levels.
external: nativeExternals,
});
const builtExports = Object.values(metafile.outputs)[0].exports;
return {
contents: `import {addUnloadable} from "@plugin";const contextId=NeptuneNative.createEvalScope(${JSON.stringify(
outputCode,
)});${builtExports
.map(
(e) =>
`export ${
e === "default" ? "default " : `const ${e} =`
} NeptuneNative.getNativeValue(contextId,${JSON.stringify(
e,
)})`,
)
.join(
";",
)};addUnloadable(() => NeptuneNative.deleteEvalScope(contextId))`,
};
},
);
},
},
],
bundle: true,
minify: true,
format: "esm",
external: ["@neptune", "@plugin"],
platform: "browser",
outfile,
})
.then(() => {
fs.createReadStream(outfile)
// It being md5 does not matter, this is for caching and not security
.pipe(crypto.createHash("md5").setEncoding("hex"))
.on("finish", function () {
fs.writeFileSync(
path.join(pluginPath, "dist/manifest.json"),
JSON.stringify({
name: pluginManifest.name,
description: pluginManifest.description,
author: pluginManifest.author,
hash: this.read(),
}),
);
console.log(`Built ${pluginManifest.name}!`);
});
});
}