Skip to content

Commit

Permalink
Use TypeScript for build script
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ckoates committed Aug 25, 2024
1 parent 28dd413 commit d7fb38d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 107 deletions.
106 changes: 0 additions & 106 deletions build.js

This file was deleted.

101 changes: 101 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import esbuild from "esbuild";
import fs from "fs";
import path from "path";
import crypto from "crypto";

const nativeExternals = ["@neptune", "@plugin", "electron"];

const minify = true;

const neptuneNativeImports: esbuild.Plugin = {
name: "neptuneNativeImports",
setup(build) {
build.onLoad({ filter: /.*[\/\\].+\.native\.[a-z]+/g }, async (args) => {
const result = await esbuild.build({
entryPoints: [args.path],
bundle: true,
minify,
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,
minify,
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))`,
};
});
},
};

const plugins = fs.readdirSync("./plugins");
for (const plugin of plugins) {
if (plugin.startsWith("_")) continue;
const pluginPath = path.join("./plugins/", plugin);
const pluginPackage = JSON.parse(
fs.readFileSync(path.join(pluginPath, "package.json"), "utf8")
);
const outfile = path.join(pluginPath, "dist/index.js");

esbuild
.build({
entryPoints: [
"./" + path.join(pluginPath, pluginPackage.main ?? "index.js"),
],
plugins: [neptuneNativeImports],
bundle: true,
minify,
format: "esm",
external: ["@neptune", "@plugin"],
platform: "browser",
outfile,
logOverride: {
"import-is-undefined": "silent",
},
})
.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 (this: crypto.Hash) {
fs.writeFileSync(
path.join(pluginPath, "dist/manifest.json"),
JSON.stringify({
name: pluginPackage.displayName,
description: pluginPackage.description,
author: pluginPackage.author,
hash: this.read(),
})
);

console.log("Built " + pluginPackage.displayName + "!");
});
});
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"scripts": {
"watch": "concurrently nodemon npm:serve",
"build": "node ./build.js",
"build": "tsx build.ts",
"serve": "serve -C ./plugins"
},
"devDependencies": {
Expand All @@ -15,6 +15,7 @@
"neptune-types": "^1.0.1",
"nodemon": "^3.1.4",
"serve": "^14.2.3",
"tsx": "^4.18.0",
"typescript": "^5.5.4"
},
"dependencies": {
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d7fb38d

Please sign in to comment.