-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathworkbench-chunking.ts
More file actions
109 lines (94 loc) · 3.23 KB
/
workbench-chunking.ts
File metadata and controls
109 lines (94 loc) · 3.23 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
type ModulePreloadContext = {
hostId: string;
hostType: "html" | "js";
};
const OBSERVATORY_WORLD_CANVAS_ID =
"/src/features/observatory/components/ObservatoryWorldCanvas.tsx";
const OBSERVATORY_FLOW_RUNTIME_CHUNK_NAME = "ObservatoryFlowRuntimeScene";
const PHYSICS_CHUNK_PREFIX = "assets/vendor-physics";
function normalizeModuleId(id: string) {
return id.split("\\").join("/");
}
function matchesNodeModulePackage(id: string, packageName: string) {
const normalizedId = normalizeModuleId(id);
const packageRoot = `/node_modules/${packageName}`;
const packageIndex = normalizedId.indexOf(packageRoot);
if (packageIndex === -1) {
return false;
}
const remainder = normalizedId.slice(packageIndex + packageRoot.length);
return remainder === "" || remainder.startsWith("/") || remainder.startsWith("?");
}
export function resolveWorkbenchManualChunk(id: string) {
if (
matchesNodeModulePackage(id, "three") ||
matchesNodeModulePackage(id, "three-mesh-bvh")
) {
return "vendor-three";
}
if (matchesNodeModulePackage(id, "zustand")) {
return "vendor-state";
}
if (
matchesNodeModulePackage(id, "@react-three/fiber") ||
matchesNodeModulePackage(id, "@react-three/drei") ||
matchesNodeModulePackage(id, "suspend-react") ||
matchesNodeModulePackage(id, "camera-controls") ||
matchesNodeModulePackage(id, "maath") ||
matchesNodeModulePackage(id, "meshline") ||
matchesNodeModulePackage(id, "three-stdlib") ||
matchesNodeModulePackage(id, "troika-three-text") ||
matchesNodeModulePackage(id, "troika-three-utils") ||
matchesNodeModulePackage(id, "troika-worker-utils")
) {
return "vendor-r3f";
}
if (
matchesNodeModulePackage(id, "@react-three/rapier")
) {
return "vendor-physics-react";
}
if (matchesNodeModulePackage(id, "@dimforge/rapier3d-compat")) {
return "vendor-physics-core";
}
if (
matchesNodeModulePackage(id, "codemirror") ||
matchesNodeModulePackage(id, "@codemirror/autocomplete") ||
matchesNodeModulePackage(id, "@codemirror/lang-yaml") ||
matchesNodeModulePackage(id, "@codemirror/language") ||
matchesNodeModulePackage(id, "@codemirror/lint") ||
matchesNodeModulePackage(id, "@codemirror/search") ||
matchesNodeModulePackage(id, "@codemirror/state") ||
matchesNodeModulePackage(id, "@codemirror/theme-one-dark") ||
matchesNodeModulePackage(id, "@codemirror/view")
) {
return "vendor-codemirror";
}
if (
matchesNodeModulePackage(id, "react-resizable-panels") ||
matchesNodeModulePackage(id, "react-syntax-highlighter") ||
matchesNodeModulePackage(id, "lucide-react") ||
matchesNodeModulePackage(id, "@tabler/icons-react") ||
matchesNodeModulePackage(id, "motion")
) {
return "vendor-ui";
}
if (matchesNodeModulePackage(id, "yaml")) {
return "vendor-yaml";
}
return undefined;
}
export function resolveWorkbenchModulePreloadDependencies(
url: string,
deps: string[],
context: ModulePreloadContext,
) {
if (
context.hostType === "js" &&
normalizeModuleId(context.hostId).endsWith(OBSERVATORY_WORLD_CANVAS_ID) &&
url.includes(OBSERVATORY_FLOW_RUNTIME_CHUNK_NAME)
) {
return deps.filter((dep) => !dep.startsWith(PHYSICS_CHUNK_PREFIX));
}
return deps;
}