-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
133 lines (113 loc) · 3.04 KB
/
esbuild.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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");
if (!fs.existsSync(nodeModulesPath)) {
return [];
}
return fs.readdirSync(nodeModulesPath).filter((module) => {
return !module.startsWith(".");
});
};
const markBuild = () => {
if (process.env.NODE_ENV === "production") {
return `""`;
}
const datetime = new Date(Date.now())
.toISOString()
.replaceAll("-", "")
.replaceAll(":", "")
.replace("T", ".")
.replace("Z", "")
.split(".");
const build = datetime[0] + "_" + datetime[1];
return `"+next.${build}"`;
}
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 OUT_WEB_FOLDER = IS_NPM_BUNDLE ? "dist/assets" : "build/bundle/assets";
const nodeModules = IS_NPM_BUNDLE ? getNodeModules() : [];
const myVars = {
'process.env.SNAPCRUD_BUILD_PREFIX': markBuild(),
}
const buildOptions = {
bundle: true,
define: myVars,
entryPoints: ["src/index.ts"],
external: nodeModules,
format: IS_NPM_BUNDLE ? "esm" : "cjs",
outfile: OUT_FILE,
platform: "node",
};
const buildWebOptions = {
bundle: true,
define: myVars,
entryPoints: ["src-web/style.css"],
minify: true,
outdir: OUT_WEB_FOLDER,
};
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);
});
build(buildWebOptions)
.then(() => {
console.log(`${ESBUILD_NAME} Web has been built to ${OUT_WEB_FOLDER}`);
})
.catch((e) => {
console.error(`Error building ${ESBUILD_NAME} Web: ${e.message}`);
process.exit(1);
});
}