forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.ts
166 lines (144 loc) · 4.77 KB
/
assets.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
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
import * as path from "path";
import type * as esbuild from "esbuild";
import type { RemixConfig } from "../config";
import invariant from "../invariant";
import { getRouteModuleExports } from "./routeExports";
import { getHash } from "./utils/crypto";
import { createUrl } from "./utils/url";
type Route = RemixConfig["routes"][string];
export interface AssetsManifest {
version: string;
url?: string;
entry: {
module: string;
imports: string[];
};
routes: {
[routeId: string]: {
id: string;
parentId?: string;
path?: string;
index?: boolean;
caseSensitive?: boolean;
module: string;
imports?: string[];
hasAction: boolean;
hasLoader: boolean;
hasCatchBoundary: boolean;
hasErrorBoundary: boolean;
};
};
}
export async function createAssetsManifest(
config: RemixConfig,
metafile: esbuild.Metafile
): Promise<AssetsManifest> {
function resolveUrl(outputPath: string): string {
return createUrl(
config.publicPath,
path.relative(config.assetsBuildDirectory, path.resolve(outputPath))
);
}
function resolveImports(
imports: esbuild.Metafile["outputs"][string]["imports"]
): string[] {
return imports
.filter((im) => im.kind === "import-statement")
.map((im) => resolveUrl(im.path));
}
let entryClientFile = path.resolve(
config.appDirectory,
config.entryClientFile
);
let routesByFile: Map<string, Route[]> = Object.keys(config.routes).reduce(
(map, key) => {
let route = config.routes[key];
map.set(
route.file,
map.has(route.file) ? [...map.get(route.file), route] : [route]
);
return map;
},
new Map()
);
let entry: AssetsManifest["entry"] | undefined;
let routes: AssetsManifest["routes"] = {};
for (let key of Object.keys(metafile.outputs).sort()) {
let output = metafile.outputs[key];
if (!output.entryPoint) continue;
// When using yarn-pnp, esbuild-plugin-pnp resolves files under the pnp namespace, even entry.client.tsx
let entryPointFile = output.entryPoint.replace(/^pnp:/, "");
if (path.resolve(entryPointFile) === entryClientFile) {
entry = {
module: resolveUrl(key),
imports: resolveImports(output.imports),
};
// Only parse routes otherwise dynamic imports can fall into here and fail the build
} else if (entryPointFile.startsWith("browser-route-module:")) {
entryPointFile = entryPointFile.replace(
/(^browser-route-module:|\?browser$)/g,
""
);
let groupedRoute = routesByFile.get(entryPointFile);
invariant(
groupedRoute,
`Cannot get route(s) for entry point ${output.entryPoint}`
);
for (let route of groupedRoute) {
let sourceExports = await getRouteModuleExports(config, route.id);
routes[route.id] = {
id: route.id,
parentId: route.parentId,
path: route.path,
index: route.index,
caseSensitive: route.caseSensitive,
module: resolveUrl(key),
imports: resolveImports(output.imports),
hasAction: sourceExports.includes("action"),
hasLoader: sourceExports.includes("loader"),
hasCatchBoundary: sourceExports.includes("CatchBoundary"),
hasErrorBoundary: sourceExports.includes("ErrorBoundary"),
};
}
}
}
invariant(entry, `Missing output for entry point`);
optimizeRoutes(routes, entry.imports);
let version = getHash(JSON.stringify({ entry, routes })).slice(0, 8);
return { version, entry, routes };
}
type ImportsCache = { [routeId: string]: string[] };
function optimizeRoutes(
routes: AssetsManifest["routes"],
entryImports: string[]
): void {
// This cache is an optimization that allows us to avoid pruning the same
// route's imports more than once.
let importsCache: ImportsCache = Object.create(null);
for (let key in routes) {
optimizeRouteImports(key, routes, entryImports, importsCache);
}
}
function optimizeRouteImports(
routeId: string,
routes: AssetsManifest["routes"],
parentImports: string[],
importsCache: ImportsCache
): string[] {
if (importsCache[routeId]) return importsCache[routeId];
let route = routes[routeId];
if (route.parentId) {
parentImports = parentImports.concat(
optimizeRouteImports(route.parentId, routes, parentImports, importsCache)
);
}
let routeImports = (route.imports || []).filter(
(url) => !parentImports.includes(url)
);
// Setting `route.imports = undefined` prevents `imports: []` from showing up
// in the manifest JSON when there are no imports.
route.imports = routeImports.length > 0 ? routeImports : undefined;
// Cache so the next lookup for this route is faster.
importsCache[routeId] = routeImports;
return routeImports;
}