|
| 1 | +import { build, context } from "esbuild"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import { spawn } from "child_process"; |
| 5 | + |
| 6 | +const getNodeModules = () => { |
| 7 | + const nodeModulesPath = path.resolve(import.meta.dirname, "node_modules"); |
| 8 | + if (!fs.existsSync(nodeModulesPath)) { |
| 9 | + return []; |
| 10 | + } |
| 11 | + |
| 12 | + return fs.readdirSync(nodeModulesPath).filter((module) => { |
| 13 | + return !module.startsWith("."); |
| 14 | + }); |
| 15 | +}; |
| 16 | + |
| 17 | +const markBuild = () => { |
| 18 | + if (process.env.NODE_ENV === "production") { |
| 19 | + return `""`; |
| 20 | + } |
| 21 | + |
| 22 | + const datetime = new Date(Date.now()) |
| 23 | + .toISOString() |
| 24 | + .replaceAll("-", "") |
| 25 | + .replaceAll(":", "") |
| 26 | + .replace("T", ".") |
| 27 | + .replace("Z", "") |
| 28 | + .split("."); |
| 29 | + |
| 30 | + const build = datetime[0] + "_" + datetime[1]; |
| 31 | + |
| 32 | + return `"+next.${build}"`; |
| 33 | +} |
| 34 | + |
| 35 | +const IS_WATCH_MODE = process.argv[2] === "watch"; |
| 36 | +const IS_NPM_BUNDLE = process.argv[2] !== "full"; |
| 37 | +const ARGUMENTS = process.argv[3] || ""; |
| 38 | + |
| 39 | +const ESBUILD_NAME = IS_NPM_BUNDLE ? "NPM Bundle" : "Full Bundle"; |
| 40 | +const OUT_FILE = IS_NPM_BUNDLE ? "dist/index.js" : "build/bundle/index.js"; |
| 41 | + |
| 42 | +const nodeModules = IS_NPM_BUNDLE ? getNodeModules() : []; |
| 43 | + |
| 44 | +const myVars = { |
| 45 | + 'process.env.ANCA_BUILD_PREFIX': markBuild(), |
| 46 | +} |
| 47 | + |
| 48 | +const buildOptions = { |
| 49 | + bundle: true, |
| 50 | + define: myVars, |
| 51 | + entryPoints: ["src/index.ts"], |
| 52 | + external: nodeModules, |
| 53 | + format: IS_NPM_BUNDLE ? "esm" : "cjs", |
| 54 | + outfile: OUT_FILE, |
| 55 | + platform: "node", |
| 56 | +}; |
| 57 | + |
| 58 | +let childProcess = null; |
| 59 | + |
| 60 | +const runOutputFile = () => { |
| 61 | + if (childProcess) { |
| 62 | + childProcess.kill(); |
| 63 | + } |
| 64 | + childProcess = spawn("node", [OUT_FILE, ...ARGUMENTS.split(" ")], { |
| 65 | + stdio: "inherit", |
| 66 | + }); |
| 67 | +}; |
| 68 | + |
| 69 | +const cleanUp = () => { |
| 70 | + if (childProcess) { |
| 71 | + childProcess.kill(); |
| 72 | + } |
| 73 | + process.exit(); |
| 74 | +}; |
| 75 | + |
| 76 | +const watchOutputFile = () => { |
| 77 | + fs.watch(OUT_FILE, (eventType) => { |
| 78 | + if (eventType === "change") { |
| 79 | + runOutputFile(); |
| 80 | + } |
| 81 | + }); |
| 82 | +}; |
| 83 | + |
| 84 | +process.on("exit", cleanUp); |
| 85 | +process.on("SIGINT", cleanUp); |
| 86 | +process.on("SIGTERM", cleanUp); |
| 87 | +process.on("uncaughtException", cleanUp); |
| 88 | + |
| 89 | +if (IS_WATCH_MODE) { |
| 90 | + context(buildOptions) |
| 91 | + .then(async (ctx) => { |
| 92 | + await ctx.watch(); |
| 93 | + console.log( |
| 94 | + `Watching ${buildOptions.entryPoints[0]}... (out: ${ESBUILD_NAME} '${OUT_FILE}')`, |
| 95 | + ); |
| 96 | + runOutputFile(); |
| 97 | + watchOutputFile(); |
| 98 | + }) |
| 99 | + .catch((e) => { |
| 100 | + console.error( |
| 101 | + `Error setting up watch mode for ${ESBUILD_NAME}: ${e.message}`, |
| 102 | + ); |
| 103 | + process.exit(1); |
| 104 | + }); |
| 105 | +} else { |
| 106 | + build(buildOptions) |
| 107 | + .then(() => { |
| 108 | + console.log(`${ESBUILD_NAME} has been built to ${OUT_FILE}`); |
| 109 | + }) |
| 110 | + .catch((e) => { |
| 111 | + console.error(`Error building ${ESBUILD_NAME}: ${e.message}`); |
| 112 | + process.exit(1); |
| 113 | + }); |
| 114 | +} |
0 commit comments