-
-
Notifications
You must be signed in to change notification settings - Fork 801
Expand file tree
/
Copy pathbuild.config.ts
More file actions
188 lines (178 loc) · 5.99 KB
/
build.config.ts
File metadata and controls
188 lines (178 loc) · 5.99 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { defineBuildConfig } from "obuild/config";
import { resolveModulePath } from "exsolve";
import { traceNodeModules } from "nf3";
import { mkdir, readFile, rmdir, writeFile } from "node:fs/promises";
import type { CodeSplittingOptions } from "rolldown";
const isStub = process.argv.includes("--stub");
const pkg = await import("./package.json", { with: { type: "json" } }).then((r) => r.default || r);
const tracePkgs = [
"cookie-es", // used by azure runtime
"croner", // used by internal/task
"defu", // used by open-api runtime
"destr", // used by node-server and deno-server
"get-port-please", // used by dev server
"rendu", // used by HTML renderer template
"scule", // used by runtime config
"source-map", // used by dev error runtime
"ufo", // used by presets and runtime
"unctx", // used by internal/context
"youch", // used by error handler
"youch-core", // used by error handler
];
export default defineBuildConfig({
entries: [
{
type: "bundle",
input: ["src/builder.ts", "src/cli/index.ts", "src/types/index.ts", "src/vite.ts"],
},
{
type: "transform",
input: "src/runtime/",
outDir: "dist/runtime",
},
{
type: "transform",
input: "src/presets/",
outDir: "dist/presets",
filter: (id) => id.includes("runtime/"),
dts: false,
},
],
hooks: {
rolldownConfig(config) {
config.platform = "node";
config.resolve ??= {};
config.resolve.alias ??= {};
Object.assign(config.resolve.alias, {
"node-fetch-native/proxy": "node-fetch-native/native",
"node-fetch-native": "node-fetch-native/native",
});
config.external ??= [];
(config.external as string[]).push(
"nitro",
...Object.keys(pkg.exports || {}).map((key) => key.replace(/^./, "nitro")),
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
...tracePkgs,
"typescript",
"firebase-functions",
"@scalar/api-reference",
"get-port-please",
"cloudflare:workers",
"@cloudflare/workers-types",
// unplugin deps
"@rspack/core",
"@farmfe/core",
"webpack",
"unloader"
);
},
rolldownOutput(config) {
(config.codeSplitting as CodeSplittingOptions).groups?.unshift(
{
test: /src[/\\]build[/\\](plugins|virtual|\w+\.ts)/,
name: "_build/common",
},
{ test: /src[/\\](utils)[/\\]/, name: "_chunks/utils" }
);
config.chunkFileNames = (chunk) => {
if (chunk.name.startsWith("_")) {
return `[name].mjs`;
}
if (chunk.name === "rolldown-runtime") {
return `_common.mjs`;
}
if (chunk.moduleIds.every((id) => id.includes("node_modules"))) {
const pkgNames = [
...new Set(
chunk.moduleIds
.map(
(id) =>
id.match(/.*[/\\]node_modules[/\\](?<package>@[^/\\]+[/\\][^/\\]+|[^/\\]+)/)
?.groups?.package
)
.filter(Boolean)
.map((name) => name!.split(/[/\\]/).pop()!)
.filter(Boolean)
),
].sort((a, b) => a.length - b.length);
let chunkName = "";
for (const name of pkgNames) {
const separator = chunkName ? "+" : "";
if ((chunkName + separator + name).length > 30) {
break;
}
chunkName += separator + name;
}
return `_libs/${chunkName || "_"}.mjs`;
}
if (chunk.moduleIds.every((id) => /src[/\\]cli[/\\]/.test(id))) {
return `cli/_chunks/[name].mjs`;
}
if (chunk.moduleIds.every((id) => /build[/\\]vite[/\\]/.test(id))) {
return `_build/vite.[name].mjs`;
}
if (chunk.moduleIds.every((id) => /build[/\\]rolldown[/\\]/.test(id))) {
return `_build/rolldown.mjs`;
}
if (chunk.moduleIds.every((id) => /build[/\\]rollup[/\\]|build[/\\]plugins/.test(id))) {
return `_build/rollup.mjs`;
}
if (chunk.moduleIds.every((id) => /src[/\\]dev[/\\]|src[/\\]runtime/.test(id))) {
return `_dev.mjs`;
}
if (chunk.moduleIds.every((id) => /src[/\\]presets/.test(id))) {
return `_presets.mjs`;
}
if (
chunk.moduleIds.every((id) => /src[/\\]build[/\\]|src[/\\]presets|src[/\\]utils/.test(id))
) {
return `_build/shared.mjs`;
}
if (chunk.moduleIds.every((id) => /src[/\\](runner|dev|runtime)/.test(id))) {
return `_chunks/dev.mjs`;
}
return "_chunks/nitro.mjs";
};
},
async end() {
if (isStub) {
return;
}
// Bundle docs
const { DocsManager, DocsSourceFS, exportDocsToFS } = await import("mdzilla");
const man = new DocsManager(new DocsSourceFS("./docs"));
await man.load();
await rmdir("./skills/nitro/docs").catch(() => {});
await mkdir("./skills/nitro/docs", { recursive: true });
await exportDocsToFS(man, "./skills/nitro/docs", {
title: "Nitro Documentation",
tocFile: "TOC.md",
filter: (e: { entry: { path: string } }) => !e.entry.path.startsWith("/blog"),
});
// Trace included dependencies
await traceNodeModules(
tracePkgs.map((pkg) => resolveModulePath(pkg)),
{
hooks: {
tracedPackages(packages) {
// Avoid tracing direct dependencies
const deps = new Set([
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
]);
for (const dep of deps) {
delete packages[dep];
}
},
},
}
);
// Vite types
await writeFile(
"dist/vite.d.mts",
`import "vite/client";\nimport "nitro/vite/types";\n${await readFile("dist/vite.d.mts", "utf8")}`
);
},
},
});