-
Notifications
You must be signed in to change notification settings - Fork 15
/
build.mjs
73 lines (58 loc) · 1.53 KB
/
build.mjs
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
import esbuild from "esbuild";
import fs from "fs";
const outputDir = "dist";
const shouldClean = process.argv.includes("--clean");
if (shouldClean) {
// Clean up any files in the output directory, or make sure the output directory exists
if (fs.existsSync(outputDir)) {
fs.rmSync(outputDir, {
recursive: true,
force: true,
});
}
fs.mkdirSync(outputDir);
}
const sharedOptions = {
entryPoints: ["src/index.ts"],
sourcemap: true,
bundle: true,
packages: "external",
target: "es6",
logLevel: "info",
};
const esmOptions = {
...sharedOptions,
outfile: `${outputDir}/index.mjs`,
format: "esm",
};
const cjsOptions = {
...sharedOptions,
outfile: `${outputDir}/index.cjs`,
format: "cjs",
};
const shouldWatch = process.argv.includes("--watch");
if (shouldWatch) {
const context = await esbuild.context(esmOptions);
await context.watch();
await context.serve();
} else {
let buildTargets = ["all"];
const buildsArgIndex = process.argv.indexOf("--builds");
if (buildsArgIndex >= 0) {
buildTargets = process.argv[buildsArgIndex + 1].split(",");
}
const builds = [];
buildTargets.forEach((buildTarget) => {
if (buildTarget === "esm" || buildTarget === "all") {
builds.push(esbuild.build(esmOptions));
}
if (buildTarget === "cjs" || buildTarget === "all") {
builds.push(esbuild.build(cjsOptions));
}
});
if (builds.length === 0) {
console.error("No valid builds specified for the --builds arg.");
process.exit(1);
}
await Promise.all(builds);
}