Skip to content

Commit

Permalink
enhance nodejs dependencies and scripts
Browse files Browse the repository at this point in the history
update npm packages
  • Loading branch information
TimurRin committed Sep 4, 2024
1 parent 38fdcca commit 1ffa30d
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 248 deletions.
4 changes: 2 additions & 2 deletions anca.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"dataVersion": 0,
"version": {
"latest": "0.1.0-dev.2",
"latestNext": "0.1.0-dev.2+next.20240904_035621",
"timestamp": 1725422181
"latestNext": "0.1.0-dev.2+next.20240904_084209",
"timestamp": 1725439329
},
"files": [
{
Expand Down
72 changes: 63 additions & 9 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { build } from "esbuild";
import { build, context } from "esbuild";
import fs from "fs";
import path from "path";
import { spawn } from "child_process";

const getNodeModules = () => {
const nodeModulesPath = path.resolve(import.meta.dirname, "node_modules");
Expand All @@ -13,25 +14,78 @@ const getNodeModules = () => {
});
};

const IS_WATCH_MODE = process.argv[2] === "watch";
const IS_NPM_BUNDLE = process.argv[2] !== "full";
const ARGUMENTS = process.argv[3] || "";

const ESBUILD_NAME = IS_NPM_BUNDLE ? "NPM Bundle" : "Full Bundle";
const OUT_FILE = IS_NPM_BUNDLE ? "dist/index.js" : "build/bundle/index.js";

const nodeModules = IS_NPM_BUNDLE ? getNodeModules() : [];

build({
const buildOptions = {
bundle: true,
entryPoints: ["src/index.ts"],
external: nodeModules,
format: IS_NPM_BUNDLE ? "esm" : "cjs",
outfile: OUT_FILE,
platform: "node",
})
.then((r) => {
console.log(`${ESBUILD_NAME} has been built to ${OUT_FILE}`);
})
.catch((e) => {
console.log(`Error building ${ESBUILD_NAME}: ${e.message}`);
process.exit(1);
};

let childProcess = null;

const runOutputFile = () => {
if (childProcess) {
childProcess.kill();
}
childProcess = spawn("node", [OUT_FILE, ...ARGUMENTS.split(" ")], {
stdio: "inherit",
});
};

const cleanUp = () => {
if (childProcess) {
childProcess.kill();
}
process.exit();
};

const watchOutputFile = () => {
fs.watch(OUT_FILE, (eventType) => {
if (eventType === "change") {
runOutputFile();
}
});
};

process.on("exit", cleanUp);
process.on("SIGINT", cleanUp);
process.on("SIGTERM", cleanUp);
process.on("uncaughtException", cleanUp);

if (IS_WATCH_MODE) {
context(buildOptions)
.then(async (ctx) => {
await ctx.watch();
console.log(
`Watching ${buildOptions.entryPoints[0]}... (out: ${ESBUILD_NAME} '${OUT_FILE}')`,
);
runOutputFile();
watchOutputFile();
})
.catch((e) => {
console.error(
`Error setting up watch mode for ${ESBUILD_NAME}: ${e.message}`,
);
process.exit(1);
});
} else {
build(buildOptions)
.then(() => {
console.log(`${ESBUILD_NAME} has been built to ${OUT_FILE}`);
})
.catch((e) => {
console.error(`Error building ${ESBUILD_NAME}: ${e.message}`);
process.exit(1);
});
}
143 changes: 14 additions & 129 deletions package-lock.json

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

Loading

0 comments on commit 1ffa30d

Please sign in to comment.