-
-
Notifications
You must be signed in to change notification settings - Fork 882
Expand file tree
/
Copy pathchunks.ts
More file actions
120 lines (101 loc) · 3.09 KB
/
Copy pathchunks.ts
File metadata and controls
120 lines (101 loc) · 3.09 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
import type { Nitro } from "nitro/types";
// Tests in @test/unit/chunks.test.ts
const virtualRe = /^(?:\0|#|virtual:)/;
export const NODE_MODULES_RE = /node_modules[/\\](?!(?:nitro|nitro-nightly)[/\\])[^.]/;
export function libChunkName(id: string) {
const pkgName = pathToPkgName(id);
return pkgName ? `_libs/${pkgName}` : undefined;
}
export function pathToPkgName(path: string): string | undefined {
let pkgName = path.match(
/.*(?:[/\\])node_modules(?:[/\\])(?<name>@[^/\\]+[/\\][^/\\]+|[^/\\.][^/\\]*)/
)?.groups?.name;
if (pkgName?.endsWith("-nightly")) {
pkgName = pkgName.slice(0, -8);
}
return pkgName;
}
export function getChunkName(chunk: { name: string; moduleIds: string[] }, nitro: Nitro) {
// Known groups
if (chunk.name === "rolldown-runtime") {
return "_runtime.mjs";
}
// Library chunks
if (chunk.moduleIds.every((id) => NODE_MODULES_RE.test(id))) {
const chunkName = joinPkgNames(chunk.moduleIds);
if (chunkName.length > 30) {
return `${chunk.name}+[...].mjs`;
}
return `_libs/${chunkName || "_"}.mjs`;
}
// _ chunks are preserved (should be after library normalization)
if (chunk.name.startsWith("_")) {
return `${chunk.name}.mjs`;
}
// No moduleIds
if (chunk.moduleIds.length === 0) {
return `_chunks/${chunk.name}.mjs`;
}
const ids = chunk.moduleIds.filter((id) => !virtualRe.test(id));
// All virtual
if (ids.length === 0) {
if (chunk.moduleIds.every((id) => id.includes("virtual:raw"))) {
return `_raw/[name].mjs`;
}
return `_virtual/[name].mjs`;
}
// WASM chunk
if (ids.every((id) => id.endsWith(".wasm"))) {
return `_wasm/[name].mjs`;
}
// Chunks generate by other vite environments (we assume SSR for simplicity)
if (ids.every((id) => id.includes("vite/services"))) {
return `_ssr/[name].mjs`;
}
// Chunks from generated code
if (ids.every((id) => id.startsWith(nitro.options.buildDir))) {
return `_build/[name].mjs`;
}
// Try to match user defined routes or tasks
const mainId = ids.at(-1);
if (mainId) {
const routeHandler = nitro.routing.routes.routes
.flatMap((h) => h.data)
.find((h) => h.handler === mainId);
if (routeHandler?.route) {
return `_routes/${routeToFsPath(routeHandler.route).replace(/\[/g, "%5B").replace(/\]/g, "%5D")}.mjs`;
}
const taskHandler = Object.entries(nitro.options.tasks).find(
([_, task]) => task.handler === mainId
);
if (taskHandler) {
return `_tasks/[name].mjs`;
}
}
return `_chunks/[name].mjs`;
}
function joinPkgNames(moduleIds: string[]): string {
const names = [
...new Set(
moduleIds
.map((id) => pathToPkgName(id))
.filter(Boolean)
.map((name) => name!.replace(/^@/, "").replace(/[/\\]/g, "__"))
),
].sort();
return names.join("+");
}
export function routeToFsPath(route: string) {
return (
route
.split("/")
.slice(1)
.map((s) =>
s
.replace(/:(\w+)/g, "[$1]")
.replace(/\*+/g, "[...]")
.replace(/[^a-zA-Z0-9_.[\]]/g, "_")
)
.join("/") || "index"
);
}