-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathbuild.config.ts
75 lines (71 loc) · 2.13 KB
/
build.config.ts
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
import { glob, rm, readFile, writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { resolve } from "pathe";
import { normalize } from "pathe";
import { defineBuildConfig } from "unbuild";
const srcDir = fileURLToPath(new URL("src", import.meta.url));
const libDir = fileURLToPath(new URL("lib", import.meta.url));
export const distSubpaths = ["presets", "runtime", "types"];
export const libSubpaths = ["config", "meta", "runtime/meta"];
export const stubAlias = {
nitro: resolve(srcDir, "index.ts"),
...Object.fromEntries(
distSubpaths.map((subpath) => [
`nitro/${subpath}`,
resolve(srcDir, `${subpath}/index.ts`),
])
),
...Object.fromEntries(
libSubpaths.map((subpath) => [
`nitro/${subpath}`,
resolve(libDir, `${subpath.replace("/", "-")}.mjs`),
])
),
};
export default defineBuildConfig({
declaration: true,
name: "nitro",
entries: [
{ input: "src/cli/index.ts" },
{ input: "src/index.ts" },
{ input: "src/types/index.ts" },
{ input: "src/runtime/", outDir: "dist/runtime", format: "esm" },
{ input: "src/presets/", outDir: "dist/presets", format: "esm" },
],
hooks: {
async "build:done"(ctx) {
for await (const file of glob(resolve(ctx.options.outDir, "**/*.d.ts"))) {
if (file.includes("runtime") || file.includes("presets")) {
const dtsContents = (await readFile(file, "utf8")).replaceAll(
/ from "\.\/(.+)";$/gm,
(_, relativePath) => ` from "./${relativePath}.mjs";`
);
await writeFile(file.replace(/\.d.ts$/, ".d.mts"), dtsContents);
}
await rm(file);
}
},
},
externals: [
"nitro",
...[...distSubpaths, ...libSubpaths].map((subpath) => `nitro/${subpath}`),
"firebase-functions",
"@scalar/api-reference",
],
stubOptions: {
jiti: {
alias: stubAlias,
},
},
rollup: {
output: {
chunkFileNames(chunk: any) {
const id = normalize(chunk.moduleIds.at(-1));
if (id.includes("/src/cli/")) {
return "cli/[name].mjs";
}
return "_chunks/[name].mjs";
},
},
},
});