-
Notifications
You must be signed in to change notification settings - Fork 6
/
esbuild.mjs
87 lines (76 loc) · 2.13 KB
/
esbuild.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
80
81
82
83
84
85
86
87
//@ts-check
import { build } from "esbuild";
import * as path from "node:path";
import { writeFile, mkdir } from "node:fs/promises";
const cyan = (/** @type {string} */ text) => `\u001B[${36}m${text}\u001B[${39}m`;
/** @type {import("esbuild").Plugin} */
const extensionTransformerPlugin = {
name: "extensionTransformerPlugin",
setup(build) {
if (build.initialOptions.write === false)
throw "extensionTransformerPlugin Sets write to false to modify the files. Cannot be set to false";
build.initialOptions.write = false;
if (
build.initialOptions.format === "esm" &&
build.initialOptions.outExtension?.[".js"] === ".mjs"
) {
build.onEnd(async (res) => {
if (!build.initialOptions.outdir) throw "options.outdir is required";
Promise.all(
(res.outputFiles ?? []).map(async ({ path: filePath, text }) => {
const contents = text.replace(
/from "\.\/(?<filename>.+)"/g,
(_, filename) => `from "./${filename}.mjs"`
);
try {
await mkdir(path.join(filePath, "../"), {
recursive: true,
});
} catch {}
writeFile(filePath, contents);
})
);
});
}
},
};
/** @type {Omit<import("esbuild").BuildOptions, "entryPoints"> & { entryPoints: string[] }} */
const buildConfig = {
entryPoints: [
"src/createTRPCRemix.ts",
"src/trpcLoader.ts",
"src/index.ts",
"src/withTRPC.tsx",
"src/adapter/index.ts",
],
treeShaking: true,
outdir: "dist/",
platform: "node",
target: ["es2020"],
// minify: true,
};
/** @type {(Partial<Omit<import("esbuild").BuildOptions, "entryPoints">> & { entryPoints?: string[]; name: string})[]} */
const builds = [
{
name: "commonjs build",
format: "cjs",
},
{
plugins: [extensionTransformerPlugin],
name: "esm build",
outExtension: { ".js": ".mjs" },
splitting: true,
format: "esm",
},
];
for (const { name: buildName, entryPoints, ...currentBuild } of builds) {
console.log(cyan(`Building: ${buildName}`));
build({
...buildConfig,
...currentBuild,
entryPoints: [...buildConfig.entryPoints, ...(entryPoints ?? [])],
}).catch((/** @type {unknown} */ err) => {
console.error(err);
process.exit(1);
});
}