From fee3663b1852585a2790f9e3ef8cd3c713e29873 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Thu, 21 Aug 2025 13:34:13 +0100 Subject: [PATCH] Bundle Actions using esbuild --- build.mjs | 38 ++++++++++++++++++++++++++++++++++++++ eslint.config.mjs | 1 + package.json | 5 ++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 build.mjs diff --git a/build.mjs b/build.mjs new file mode 100644 index 000000000..f5205f535 --- /dev/null +++ b/build.mjs @@ -0,0 +1,38 @@ +import { rm } from "node:fs/promises"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +import * as esbuild from "esbuild"; +import { globSync } from "glob"; +import { typecheckPlugin } from "@jgoz/esbuild-plugin-typecheck"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const OUT_DIR = "lib"; + +await rm(join(__dirname, OUT_DIR), { recursive: true, force: true }); + +// This will just log when a build ends +/** @type {esbuild.Plugin} */ +const onEndPlugin = { + name: "on-end", + setup(build) { + build.onEnd((result) => { + // eslint-disable-next-line no-console + console.log(`Build ended with ${result.errors.length} errors`); + }); + }, +}; + +const context = await esbuild.context({ + entryPoints: globSync(["src/*-action.ts", "src/*-action-post.ts"]), + bundle: true, + format: "cjs", + outdir: OUT_DIR, + platform: "node", + plugins: [typecheckPlugin(), onEndPlugin], +}); + +await context.rebuild(); +await context.dispose(); diff --git a/eslint.config.mjs b/eslint.config.mjs index 0c06089d6..7b9f2e6f5 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -29,6 +29,7 @@ export default [ "lib/**/*", "src/testdata/**/*", "tests/**/*", + "build.mjs", "eslint.config.mjs", ".github/**/*", ], diff --git a/package.json b/package.json index 81648b9ce..9c454bc49 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "description": "CodeQL action", "scripts": { - "build": "tsc --build", + "build": "node build.mjs", "test": "ava src/**.test.ts --serial --verbose", "test-debug": "ava src/**.test.ts --serial --verbose --timeout=20m", "lint": "eslint --report-unused-disable-directives --max-warnings=0 .", @@ -61,6 +61,7 @@ "@eslint/compat": "^1.3.2", "@eslint/eslintrc": "^3.3.1", "@eslint/js": "^9.33.0", + "@jgoz/esbuild-plugin-typecheck": "^4.0.3", "@microsoft/eslint-formatter-sarif": "^3.1.0", "@types/archiver": "^6.0.3", "@types/console-log-level": "^1.4.5", @@ -73,12 +74,14 @@ "@typescript-eslint/eslint-plugin": "^8.40.0", "@typescript-eslint/parser": "^8.40.0", "ava": "^6.4.1", + "esbuild": "^0.25.9", "eslint": "^8.57.1", "eslint-import-resolver-typescript": "^3.8.7", "eslint-plugin-filenames": "^1.3.2", "eslint-plugin-github": "^5.1.8", "eslint-plugin-import": "2.29.1", "eslint-plugin-no-async-foreach": "^0.1.1", + "glob": "^11.0.3", "nock": "^14.0.10", "removeNPMAbsolutePaths": "3.0.1", "sinon": "^21.0.0",