-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.mjs
79 lines (72 loc) · 1.99 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
74
75
76
77
78
79
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
import { glob } from "glob";
const sharedOptions = {
sourcemap: "external",
sourcesContent: true,
minify: false,
allowOverwrite: true,
packages: "external",
platform: "neutral",
format: "esm",
target: "es2022",
};
async function main() {
// Start with a clean slate
await rm("pkg", { recursive: true, force: true });
// Build the source code for a neutral platform as ESM
await esbuild.build({
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
outdir: "pkg/dist-src",
bundle: false,
...sharedOptions,
sourcemap: false,
});
// Remove the types file from the dist-src folder
const typeFiles = await glob([
"./pkg/dist-src/**/types.js.map",
"./pkg/dist-src/**/types.js",
]);
for (const typeFile of typeFiles) {
await rm(typeFile);
}
const entryPoints = ["./pkg/dist-src/index.js"];
await esbuild.build({
entryPoints,
outdir: "pkg/dist-bundle",
bundle: true,
...sharedOptions,
});
// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
await copyFile("README.md", "pkg/README.md");
// Handle the package.json
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
// Remove unnecessary fields from the package.json
delete pkg.scripts;
delete pkg.prettier;
delete pkg.release;
delete pkg.jest;
await writeFile(
"pkg/package.json",
JSON.stringify(
{
...pkg,
files: ["dist-*/**", "bin/**"],
types: "dist-types/index.d.ts",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint, ncc
default: "./dist-bundle/index.js",
}
},
sideEffects: false,
},
null,
2
)
);
}
main();