-
Notifications
You must be signed in to change notification settings - Fork 13
/
vike.ts
603 lines (515 loc) · 18.6 KB
/
vike.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { prerender as prerenderCli } from "vike/prerender";
import type { PageContextServer } from "vike/types";
import { type Plugin, type ResolvedConfig, type UserConfig, normalizePath } from "vite";
import type {
VercelOutputIsr,
ViteVercelApiEntry,
ViteVercelConfig,
ViteVercelPrerenderFn,
ViteVercelPrerenderRoute,
} from "vite-plugin-vercel";
import "vike/__internal/setup";
// @ts-ignore
import { newError } from "@brillout/libassert";
import { nanoid } from "nanoid";
import { type PageFile, type PageRoutes, getPagesAndRoutes, route } from "vike/__internal";
import { getParametrizedRoute } from "./route-regex";
declare module "vite" {
export interface UserConfig {
vercel?: ViteVercelConfig;
}
}
const libName = "vite-plugin-vercel:vike";
const rendererDestination = "ssr_";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export function assert(condition: unknown, errorMessage: string): asserts condition {
if (condition) {
return;
}
const err = newError(`[${libName}][Wrong Usage] ${errorMessage}`, 2);
throw err;
}
interface MissingPageContextOverrides {
_prerenderResult: {
filePath: string;
fileContent: string;
};
/**
* @deprecated
*/
_pageId: string;
_baseUrl: string;
is404?: boolean;
_urlProcessor: (url: string) => string;
_pageFilesAll: PageFile[];
_allPageIds: string[];
_baseServer: string;
_urlHandler: null | ((url: string) => string);
}
type PageContextForRoute = Parameters<typeof route>[0];
type PageContext = PageContextServer & MissingPageContextOverrides & PageContextForRoute;
export function getRoot(config: UserConfig | ResolvedConfig): string {
return normalizePath(config.root || process.cwd());
}
function getOutDirRoot(config: ResolvedConfig) {
const outDir = config.build.outDir;
return outDir.endsWith("/server") || outDir.endsWith("/client") ? path.normalize(path.join(outDir, "..")) : outDir;
}
export function getOutput(
config: ResolvedConfig,
suffix?: "functions" | `functions/${string}.func` | "static",
): string {
return path.join(
config.vercel?.outDir ? "" : getRoot(config),
config.vercel?.outDir ?? ".vercel/output",
suffix ?? "",
);
}
export function getOutDir(config: ResolvedConfig, force?: "client" | "server"): string {
const p = path.join(config.root, normalizePath(config.build.outDir));
if (!force) return p;
return path.join(path.dirname(p), force);
}
async function copyDir(src: string, dest: string) {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
function assertHeaders(exports: unknown): Record<string, string> | null {
if (exports === null || typeof exports !== "object") return null;
if (!("headers" in exports)) return null;
const headers = (exports as { headers: unknown }).headers;
if (headers === null || headers === undefined) {
return null;
}
assert(typeof headers === "object", " `{ headers }` must be an object");
for (const value of Object.values(headers)) {
assert(typeof value === "string", " `{ headers }` must only contains string values");
}
return headers as Record<string, string>;
}
function assertEdge(exports: unknown): boolean | null {
if (exports === null || typeof exports !== "object") return null;
if (!("edge" in exports)) return null;
const edge = (exports as { edge: unknown }).edge;
assert(typeof edge === "boolean", " `{ edge }` must be a boolean");
return edge;
}
function extractIsr(exports: unknown) {
if (exports === null || typeof exports !== "object") return null;
if (!("isr" in exports)) return null;
const isr = (exports as { isr: unknown }).isr;
assert(
typeof isr === "boolean" ||
(typeof isr === "object" &&
typeof (isr as Record<string, unknown>).expiration === "number" &&
(
isr as {
expiration: number;
}
).expiration > 0),
" `{ expiration }` must be a positive number",
);
return isr;
}
function assertIsr(resolvedConfig: UserConfig | ResolvedConfig, exports: unknown): number | null {
const isr = extractIsr(exports);
if (isr === null || isr === undefined) return null;
if (isr === true) {
assert(
typeof resolvedConfig.vercel?.expiration === "number" && resolvedConfig.vercel?.expiration > 0,
"`export const isr = true;` requires a default positive value for `expiration` in vite config",
);
return resolvedConfig.vercel?.expiration;
}
return (
isr as {
expiration: number;
}
).expiration;
}
function getRouteMatch(myroute: Awaited<ReturnType<typeof route>>): unknown {
let routeMatch: unknown = null;
if ("_routeMatch" in myroute.pageContextAddendum) {
// Since 0.4.157
routeMatch = myroute.pageContextAddendum._routeMatch;
} else if ("_routeMatches" in myroute.pageContextAddendum) {
// Before 0.4.145
routeMatch = (myroute.pageContextAddendum._routeMatches as unknown[])?.[0];
} else if ("_debugRouteMatches" in myroute.pageContextAddendum) {
// Between 0.4.145 and 0.4.157 (still in place but prefer using _routeMatch)
routeMatch = myroute.pageContextAddendum._debugRouteMatches?.[0];
}
return routeMatch;
}
export const prerender: ViteVercelPrerenderFn = async (
resolvedConfig: ResolvedConfig,
): Promise<ViteVercelPrerenderRoute> => {
const routes: ViteVercelPrerenderRoute = {};
await prerenderCli({
viteConfig: {
root: getRoot(resolvedConfig),
build: {
outDir: getOutDirRoot(resolvedConfig),
},
},
async onPagePrerender(pageContext: PageContext) {
const { filePath, fileContent } = pageContext._prerenderResult;
const isr = assertIsr(resolvedConfig, pageContext.exports);
const foundRoute = await route(pageContext);
if (!pageContext.is404) {
assert(foundRoute, `Page with id ${pageContext.pageId ?? pageContext._pageId} not found`);
const routeMatch = getRouteMatch(foundRoute);
// if ISR + Filesystem routing -> ISR prevails
if (
typeof isr === "number" &&
routeMatch &&
typeof routeMatch !== "string" &&
(routeMatch as { routeType: string }).routeType === "FILESYSTEM"
) {
return;
}
}
const relPath = path.relative(getOutDir(resolvedConfig, "client"), filePath);
const newFilePath = path.join(getOutput(resolvedConfig, "static"), relPath);
const parsed = path.parse(relPath);
const pathJoined = parsed.name === "index" ? parsed.dir : path.join(parsed.dir, parsed.name);
if (relPath.endsWith(".html")) {
routes[relPath] = {
path: pathJoined === "index" ? "" : pathJoined,
};
}
await fs.mkdir(path.dirname(newFilePath), { recursive: true });
await fs.writeFile(newFilePath, fileContent);
},
});
return routes;
};
function getRouteDynamicRoute(pageRoutes: PageRoutes, pageId: string) {
for (const route of pageRoutes) {
if (route.pageId === pageId) {
if (route.routeType === "STRING") {
return getParametrizedRoute(route.routeString);
}
if (route.routeType === "FUNCTION") {
// route.routeType === 'FUNCTION'
return () => {};
}
}
}
return null;
}
function getRouteFsRoute(pageRoutes: PageRoutes, pageId: string) {
for (const route of pageRoutes) {
if (route.pageId === pageId && route.routeType === "FILESYSTEM") {
return route.routeString;
}
}
return null;
}
export async function getSsrEdgeEndpoint(): Promise<ViteVercelApiEntry["source"]> {
const sourcefile = path.join(__dirname, "..", "templates", "ssr_edge_.template.ts");
const contents = await fs.readFile(sourcefile, "utf-8");
const resolveDir = path.dirname(sourcefile);
return {
contents: contents,
sourcefile,
loader: sourcefile.endsWith(".ts")
? "ts"
: sourcefile.endsWith(".tsx")
? "tsx"
: sourcefile.endsWith(".js")
? "js"
: sourcefile.endsWith(".jsx")
? "jsx"
: "default",
resolveDir,
};
}
export async function getSsrEndpoint(source?: string) {
const sourcefile = source ?? path.join(__dirname, "..", "templates", "ssr_.template.ts");
const contents = await fs.readFile(sourcefile, "utf-8");
const resolveDir = path.dirname(sourcefile);
return {
source: {
contents: contents,
sourcefile,
loader: sourcefile.endsWith(".ts")
? "ts"
: sourcefile.endsWith(".tsx")
? "tsx"
: sourcefile.endsWith(".js")
? "js"
: sourcefile.endsWith(".jsx")
? "jsx"
: "default",
resolveDir,
},
destination: rendererDestination,
route: false,
} satisfies ViteVercelApiEntry;
}
export interface Options {
/**
* A pattern that matches each incoming pathname that should be caught by vike.
* As this rule is inserted last, a simple catch-all rule excluding /api/* should be enough.
* Defaults to `(?!/api).*`
* @see {@link https://vercel.com/docs/project-configuration#project-configuration/rewrites}
*/
source?: string;
}
export function vikeVercelPlugin(options: Options = {}): Plugin {
return {
name: libName,
apply: "build",
async config(userConfig): Promise<UserConfig> {
// wait for vike second build step with `ssr` flag
if (!userConfig.build?.ssr) return {};
const getSsrEndpointIfNotPresent = async (endpoints: ViteVercelApiEntry[]): Promise<ViteVercelApiEntry[]> => {
return endpoints.flatMap((e) => e.destination).some((d) => d === rendererDestination)
? // vite deep merges config
[]
: [await getSsrEndpoint()];
};
const additionalEndpoints: Required<UserConfig>["vercel"]["additionalEndpoints"] = [
async () => {
const userEndpoints: ViteVercelApiEntry[] = [];
if (Array.isArray(userConfig.vercel?.additionalEndpoints)) {
for (const endpoint of userConfig.vercel.additionalEndpoints) {
if (typeof endpoint === "function") {
const res = await endpoint();
if (Array.isArray(res)) {
userEndpoints.push(...res);
} else {
userEndpoints.push(res);
}
} else {
userEndpoints.push(endpoint);
}
}
}
return getSsrEndpointIfNotPresent(userEndpoints);
},
];
return {
vitePluginSsr: {
prerender: {
disableAutoRun: true,
},
},
vercel: {
prerender: userConfig.vercel?.prerender ?? prerender,
additionalEndpoints,
defaultSupportsResponseStreaming: true,
rewrites: [
{
source: options.source ? `(${options.source})` : "((?!/api).*)",
destination: `/${rendererDestination}/?__original_path=$1`,
enforce: "post",
},
],
},
} as UserConfig;
},
} as Plugin;
}
/**
* vps 0.4 compat
* @deprecated
* @param pageId
* @param pageFilesAll
*/
function findPageFile(pageId: string, pageFilesAll: PageFile[]) {
return pageFilesAll.find((p) => p.pageId === pageId && p.fileType === ".page");
}
export function vitePluginVercelVikeConfigPlugin(): Plugin {
return {
name: "vite-plugin-vercel:vike-config",
apply: "build",
async config(userConfig): Promise<UserConfig> {
let memoizedP: ReturnType<typeof _getPagesWithConfigs> | undefined = undefined;
async function getPagesWithConfigs() {
if (memoizedP) return memoizedP;
memoizedP = _getPagesWithConfigs();
return memoizedP;
}
async function _getPagesWithConfigs() {
const { pageFilesAll, allPageIds, pageRoutes, pageConfigs } = await getPagesAndRoutes();
const isLegacy = pageFilesAll.length > 0;
if (isLegacy) {
await Promise.all(pageFilesAll.map((p) => p.loadFile?.()));
}
return Promise.all(
allPageIds.map(async (pageId) => {
let page: {
config: unknown;
filePath: string;
};
if (isLegacy) {
const _page = await findPageFile(pageId, pageFilesAll);
assert(
_page,
`Cannot find page ${pageId}. Contact the vite-plugin-vercel maintainer on GitHub / Discord`,
);
page = {
config: _page.fileExports,
filePath: _page.filePath,
};
} else {
const pageConfig = pageConfigs.find((p) => p.pageId === pageId);
assert(
pageConfig,
`Cannot find page config ${pageId}. Contact the vite-plugin-vercel maintainer on GitHub / Discord`,
);
const simplePageConfig: Record<string, unknown> = {};
for (const [k, v] of Object.entries(pageConfig.configValues)) {
simplePageConfig[k] = v.value;
}
page = {
config: simplePageConfig,
filePath: pageConfig.pageId,
};
}
const route = getRouteDynamicRoute(pageRoutes, pageId) ?? getRouteFsRoute(pageRoutes, pageId);
const rawIsr = extractIsr(page.config);
let isr = assertIsr(userConfig, page.config);
const edge = assertEdge(page.config);
const headers = assertHeaders(page.config);
// if ISR + Function routing -> warn because ISR is not unsupported in this case
if (typeof route === "function" && isr) {
console.warn(
`Page ${pageId}: ISR is not supported when using route function. Remove \`{ isr }\` config or use a route string if possible.`,
);
isr = null;
}
if (edge && rawIsr !== null && typeof rawIsr === "object") {
throw new Error(
`Page ${pageId}: ISR cannot be enabled for edge functions. Remove \`{ isr }\` config or set \`{ edge: false }\`.`,
);
}
return {
pageId,
// used for debug purpose
filePath: page.filePath,
isr,
edge,
headers,
route: typeof route === "string" ? getParametrizedRoute(route) : null,
};
}),
);
}
const edgeSource = await getSsrEdgeEndpoint();
return {
vercel: {
async headers() {
const pagesWithConfigs = await getPagesWithConfigs();
return pagesWithConfigs
.filter((page) => {
if (!page.route) {
console.warn(
`Page ${page.pageId}: headers is not supported when using route function. Remove \`{ headers }\` config or use a route string if possible.`,
);
}
return page.headers !== null && page.headers !== undefined && page.route;
})
.flatMap((page) => {
const headers = Object.entries(page.headers ?? {}).map(([key, value]) => ({
key,
value,
}));
return [
{
source: `${page.route}`,
headers,
},
{
source: `${page.route}/index\\.pageContext\\.json`,
headers,
},
];
});
},
additionalEndpoints: [
async () => {
const pagesWithConfigs = await getPagesWithConfigs();
return pagesWithConfigs
.filter((page) => {
return page.edge === true;
})
.map((page) => {
if (!page.route) {
console.warn(
`Page ${page.pageId}: edge is not supported when using route function. Remove \`{ edge }\` config or use a route string if possible.`,
);
}
const destination = `${page.pageId.replace(/\/index$/g, "")}-edge-${nanoid()}`;
return {
source: edgeSource,
destination,
route: page.route ? `${page.route}(?:\\/index\\.pageContext\\.json)?` : undefined,
edge: true,
};
});
},
],
isr: async () => {
let userIsr: Record<string, VercelOutputIsr> = {};
if (userConfig.vercel?.isr) {
if (typeof userConfig.vercel.isr === "function") {
userIsr = await userConfig.vercel.isr();
} else {
userIsr = userConfig.vercel.isr;
}
}
const pagesWithConfigs = await getPagesWithConfigs();
return pagesWithConfigs
.filter((p) => typeof p.isr === "number")
.reduce((acc, cur) => {
const path = `${cur.pageId.replace(/\/index$/g, "")}-${nanoid()}`;
acc[path] = {
// biome-ignore lint/style/noNonNullAssertion: filtered
expiration: cur.isr!,
symlink: rendererDestination,
route: cur.route ? `${cur.route}(?:\\/index\\.pageContext\\.json)?` : undefined,
};
return acc;
}, userIsr);
},
},
};
},
} as Plugin;
}
export function vitePluginVercelVikeCopyStaticAssetsPlugins(): Plugin {
let resolvedConfig: ResolvedConfig;
return {
apply: "build",
name: "vite-plugin-vercel:vike-copy-static-assets",
enforce: "post",
configResolved(config) {
resolvedConfig = config;
},
async closeBundle() {
if (!resolvedConfig.build?.ssr) return;
await copyDistClientToOutputStatic(resolvedConfig);
},
};
}
async function copyDistClientToOutputStatic(resolvedConfig: ResolvedConfig) {
await copyDir(getOutDir(resolvedConfig, "client"), getOutput(resolvedConfig, "static"));
}
export default function allPlugins(options: Options = {}): Plugin[] {
return [vitePluginVercelVikeConfigPlugin(), vikeVercelPlugin(options), vitePluginVercelVikeCopyStaticAssetsPlugins()];
}