vite-plugin-vercel 0.1.2 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -6
- package/dist/index.cjs +279 -179
- package/dist/index.d.ts +22 -2
- package/dist/index.js +274 -181
- package/package.json +13 -13
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ResolvedConfig, Plugin } from 'vite';
|
|
2
2
|
import { StdinOptions, BuildOptions } from 'esbuild';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
+
import { VercelConfig } from '@vercel/routing-utils';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Schema definition for `.vercel/output/config.json`
|
|
@@ -513,6 +514,12 @@ declare const vercelOutputPrerenderConfigSchema: z.ZodObject<{
|
|
|
513
514
|
}>;
|
|
514
515
|
declare type VercelOutputPrerenderConfig = z.infer<typeof vercelOutputPrerenderConfigSchema>;
|
|
515
516
|
|
|
517
|
+
declare type ViteVercelRewrite = NonNullable<VercelConfig['rewrites']>[number] & {
|
|
518
|
+
enforce?: 'pre' | 'post';
|
|
519
|
+
};
|
|
520
|
+
declare type ViteVercelRedirect = NonNullable<VercelConfig['redirects']>[number] & {
|
|
521
|
+
enforce?: 'pre' | 'post';
|
|
522
|
+
};
|
|
516
523
|
interface ViteVercelConfig {
|
|
517
524
|
/**
|
|
518
525
|
* How long Functions should be allowed to run for every request in seconds.
|
|
@@ -552,6 +559,10 @@ interface ViteVercelConfig {
|
|
|
552
559
|
* }
|
|
553
560
|
* ```
|
|
554
561
|
*/
|
|
562
|
+
rewrites?: ViteVercelRewrite[];
|
|
563
|
+
redirects?: ViteVercelRedirect[];
|
|
564
|
+
cleanUrls?: VercelConfig['cleanUrls'];
|
|
565
|
+
trailingSlash?: VercelConfig['trailingSlash'];
|
|
555
566
|
additionalEndpoints?: ViteVercelApiEntry[];
|
|
556
567
|
/**
|
|
557
568
|
* Advanced configuration to override .vercel/output/config.json
|
|
@@ -581,7 +592,7 @@ interface ViteVercelConfig {
|
|
|
581
592
|
*/
|
|
582
593
|
isr?: Record<string, VercelOutputIsr> | (() => Promise<Record<string, VercelOutputIsr>> | Record<string, VercelOutputIsr>);
|
|
583
594
|
/**
|
|
584
|
-
* Defaults to `.vercel/output`. Mostly useful for testing
|
|
595
|
+
* Defaults to `.vercel/output`. Mostly useful for testing purpose
|
|
585
596
|
* @protected
|
|
586
597
|
*/
|
|
587
598
|
outDir?: string;
|
|
@@ -608,8 +619,17 @@ interface ViteVercelApiEntry {
|
|
|
608
619
|
* Override esbuild options
|
|
609
620
|
*/
|
|
610
621
|
buildOptions?: BuildOptions;
|
|
622
|
+
/**
|
|
623
|
+
* Automatically add a route for the function (mimics defaults Vercel behavior)
|
|
624
|
+
* Set to `false` to disable
|
|
625
|
+
*/
|
|
626
|
+
addRoute?: boolean;
|
|
627
|
+
/**
|
|
628
|
+
* Set to `true` to mark this function as an Edge Function
|
|
629
|
+
*/
|
|
630
|
+
edge?: boolean;
|
|
611
631
|
}
|
|
612
632
|
|
|
613
633
|
declare function allPlugins(): Plugin[];
|
|
614
634
|
|
|
615
|
-
export { VercelOutputConfig, VercelOutputIsr, VercelOutputPrerenderConfig, VercelOutputVcConfig, ViteVercelApiEntry, ViteVercelConfig, ViteVercelPrerenderFn, ViteVercelPrerenderRoute, allPlugins as default };
|
|
635
|
+
export { VercelOutputConfig, VercelOutputIsr, VercelOutputPrerenderConfig, VercelOutputVcConfig, ViteVercelApiEntry, ViteVercelConfig, ViteVercelPrerenderFn, ViteVercelPrerenderRoute, ViteVercelRedirect, ViteVercelRewrite, allPlugins as default };
|
package/dist/index.js
CHANGED
|
@@ -1,67 +1,26 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __objRest = (source, exclude) => {
|
|
21
|
-
var target = {};
|
|
22
|
-
for (var prop in source)
|
|
23
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
-
target[prop] = source[prop];
|
|
25
|
-
if (source != null && __getOwnPropSymbols)
|
|
26
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
}
|
|
30
|
-
return target;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
1
|
// src/index.ts
|
|
34
|
-
import
|
|
2
|
+
import fs4 from "fs/promises";
|
|
35
3
|
|
|
36
4
|
// src/utils.ts
|
|
37
5
|
import { normalizePath } from "vite";
|
|
38
6
|
import path from "path";
|
|
39
|
-
import fs from "fs/promises";
|
|
40
7
|
function getRoot(config) {
|
|
41
8
|
return normalizePath(config.root || process.cwd());
|
|
42
9
|
}
|
|
43
|
-
function getOutDir(config, force) {
|
|
44
|
-
const p = normalizePath(config.build.outDir);
|
|
45
|
-
if (!force)
|
|
46
|
-
return p;
|
|
47
|
-
return path.join(path.dirname(p), force);
|
|
48
|
-
}
|
|
49
10
|
function getOutput(config, suffix) {
|
|
50
11
|
var _a, _b;
|
|
51
|
-
return path.join(
|
|
12
|
+
return path.join(
|
|
13
|
+
((_a = config.vercel) == null ? void 0 : _a.outDir) ? "" : getRoot(config),
|
|
14
|
+
((_b = config.vercel) == null ? void 0 : _b.outDir) ?? ".vercel/output",
|
|
15
|
+
suffix ?? ""
|
|
16
|
+
);
|
|
52
17
|
}
|
|
53
|
-
function
|
|
54
|
-
|
|
55
|
-
return normalizePath(path.relative(path.join(root, "api"), filePath));
|
|
18
|
+
function getPublic(config) {
|
|
19
|
+
return path.join(getRoot(config), config.publicDir || "public");
|
|
56
20
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
for (const entry of entries) {
|
|
61
|
-
const srcPath = path.join(src, entry.name);
|
|
62
|
-
const destPath = path.join(dest, entry.name);
|
|
63
|
-
entry.isDirectory() ? await copyDir(srcPath, destPath) : await fs.copyFile(srcPath, destPath);
|
|
64
|
-
}
|
|
21
|
+
function pathRelativeTo(filePath, config, rel) {
|
|
22
|
+
const root = getRoot(config);
|
|
23
|
+
return normalizePath(path.relative(path.join(root, rel), filePath));
|
|
65
24
|
}
|
|
66
25
|
|
|
67
26
|
// src/config.ts
|
|
@@ -69,60 +28,64 @@ import path2 from "path";
|
|
|
69
28
|
|
|
70
29
|
// src/schemas/config/config.ts
|
|
71
30
|
import { z } from "zod";
|
|
72
|
-
var HasOrMissing = z.array(
|
|
73
|
-
z.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
type: z.literal("header"),
|
|
79
|
-
key: z.string(),
|
|
80
|
-
value: z.string().optional()
|
|
81
|
-
}).strict(),
|
|
82
|
-
z.object({
|
|
83
|
-
type: z.literal("cookie"),
|
|
84
|
-
key: z.string(),
|
|
85
|
-
value: z.string().optional()
|
|
86
|
-
}).strict(),
|
|
87
|
-
z.object({
|
|
88
|
-
type: z.literal("query"),
|
|
89
|
-
key: z.string(),
|
|
90
|
-
value: z.string().optional()
|
|
91
|
-
}).strict()
|
|
92
|
-
])).optional();
|
|
93
|
-
var vercelOutputConfigSchema = z.object({
|
|
94
|
-
version: z.literal(3),
|
|
95
|
-
routes: z.array(z.union([
|
|
31
|
+
var HasOrMissing = z.array(
|
|
32
|
+
z.union([
|
|
33
|
+
z.object({
|
|
34
|
+
type: z.literal("host"),
|
|
35
|
+
value: z.string()
|
|
36
|
+
}).strict(),
|
|
96
37
|
z.object({
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
methods: z.array(z.string()).optional(),
|
|
101
|
-
status: z.number().int().positive().optional(),
|
|
102
|
-
continue: z.boolean().optional(),
|
|
103
|
-
check: z.boolean().optional(),
|
|
104
|
-
missing: HasOrMissing,
|
|
105
|
-
has: HasOrMissing,
|
|
106
|
-
locale: z.object({
|
|
107
|
-
redirect: z.record(z.string()).optional(),
|
|
108
|
-
cookie: z.string().optional()
|
|
109
|
-
}).strict().optional(),
|
|
110
|
-
middlewarePath: z.string().optional()
|
|
38
|
+
type: z.literal("header"),
|
|
39
|
+
key: z.string(),
|
|
40
|
+
value: z.string().optional()
|
|
111
41
|
}).strict(),
|
|
112
42
|
z.object({
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
src: z.string().optional(),
|
|
122
|
-
dest: z.string().optional(),
|
|
123
|
-
status: z.number().optional()
|
|
43
|
+
type: z.literal("cookie"),
|
|
44
|
+
key: z.string(),
|
|
45
|
+
value: z.string().optional()
|
|
46
|
+
}).strict(),
|
|
47
|
+
z.object({
|
|
48
|
+
type: z.literal("query"),
|
|
49
|
+
key: z.string(),
|
|
50
|
+
value: z.string().optional()
|
|
124
51
|
}).strict()
|
|
125
|
-
])
|
|
52
|
+
])
|
|
53
|
+
).optional();
|
|
54
|
+
var vercelOutputConfigSchema = z.object({
|
|
55
|
+
version: z.literal(3),
|
|
56
|
+
routes: z.array(
|
|
57
|
+
z.union([
|
|
58
|
+
z.object({
|
|
59
|
+
src: z.string(),
|
|
60
|
+
dest: z.string().optional(),
|
|
61
|
+
headers: z.record(z.string()).optional(),
|
|
62
|
+
methods: z.array(z.string()).optional(),
|
|
63
|
+
status: z.number().int().positive().optional(),
|
|
64
|
+
continue: z.boolean().optional(),
|
|
65
|
+
check: z.boolean().optional(),
|
|
66
|
+
missing: HasOrMissing,
|
|
67
|
+
has: HasOrMissing,
|
|
68
|
+
locale: z.object({
|
|
69
|
+
redirect: z.record(z.string()).optional(),
|
|
70
|
+
cookie: z.string().optional()
|
|
71
|
+
}).strict().optional(),
|
|
72
|
+
middlewarePath: z.string().optional()
|
|
73
|
+
}).strict(),
|
|
74
|
+
z.object({
|
|
75
|
+
handle: z.union([
|
|
76
|
+
z.literal("rewrite"),
|
|
77
|
+
z.literal("filesystem"),
|
|
78
|
+
z.literal("resource"),
|
|
79
|
+
z.literal("miss"),
|
|
80
|
+
z.literal("hit"),
|
|
81
|
+
z.literal("error")
|
|
82
|
+
]),
|
|
83
|
+
src: z.string().optional(),
|
|
84
|
+
dest: z.string().optional(),
|
|
85
|
+
status: z.number().optional()
|
|
86
|
+
}).strict()
|
|
87
|
+
])
|
|
88
|
+
).optional(),
|
|
126
89
|
images: z.object({
|
|
127
90
|
sizes: z.tuple([
|
|
128
91
|
z.number().int().positive(),
|
|
@@ -134,43 +97,79 @@ var vercelOutputConfigSchema = z.object({
|
|
|
134
97
|
dangerouslyAllowSVG: z.boolean().optional(),
|
|
135
98
|
contentSecurityPolicy: z.string().optional()
|
|
136
99
|
}).strict().optional(),
|
|
137
|
-
wildcard: z.array(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
100
|
+
wildcard: z.array(
|
|
101
|
+
z.object({
|
|
102
|
+
domain: z.string(),
|
|
103
|
+
value: z.string()
|
|
104
|
+
}).strict()
|
|
105
|
+
).optional(),
|
|
106
|
+
overrides: z.record(
|
|
107
|
+
z.object({
|
|
108
|
+
path: z.string().optional(),
|
|
109
|
+
contentType: z.string().optional()
|
|
110
|
+
}).strict()
|
|
111
|
+
).optional(),
|
|
145
112
|
cache: z.array(z.string()).optional()
|
|
146
113
|
}).strict();
|
|
147
114
|
|
|
148
115
|
// src/config.ts
|
|
149
|
-
import
|
|
116
|
+
import fs from "fs/promises";
|
|
150
117
|
import { getTransformedRoutes } from "@vercel/routing-utils";
|
|
151
|
-
function
|
|
152
|
-
|
|
153
|
-
|
|
118
|
+
function reorderEnforce(arr) {
|
|
119
|
+
return [
|
|
120
|
+
...arr.filter((r) => r.enforce === "pre"),
|
|
121
|
+
...arr.filter((r) => !r.enforce),
|
|
122
|
+
...arr.filter((r) => r.enforce === "post")
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
function getConfig(resolvedConfig, rewrites, overrides) {
|
|
126
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
127
|
+
const _rewrites = [
|
|
128
|
+
...((_a = resolvedConfig.vercel) == null ? void 0 : _a.rewrites) ?? [],
|
|
129
|
+
...rewrites ?? []
|
|
130
|
+
];
|
|
131
|
+
const { routes, error } = getTransformedRoutes({
|
|
154
132
|
nowConfig: {
|
|
155
|
-
cleanUrls: true
|
|
133
|
+
cleanUrls: ((_b = resolvedConfig.vercel) == null ? void 0 : _b.cleanUrls) ?? true,
|
|
134
|
+
trailingSlash: (_c = resolvedConfig.vercel) == null ? void 0 : _c.trailingSlash,
|
|
135
|
+
rewrites: reorderEnforce(_rewrites),
|
|
136
|
+
redirects: ((_d = resolvedConfig.vercel) == null ? void 0 : _d.redirects) ? reorderEnforce((_e = resolvedConfig.vercel) == null ? void 0 : _e.redirects) : void 0
|
|
156
137
|
}
|
|
157
138
|
});
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
139
|
+
if (error) {
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
if (((_g = (_f = resolvedConfig.vercel) == null ? void 0 : _f.config) == null ? void 0 : _g.routes) && resolvedConfig.vercel.config.routes.length > 0) {
|
|
143
|
+
console.warn(
|
|
144
|
+
"It is discouraged to use `vercel.config.routes` to override routes. Prefer using `vercel.rewrites` and `vercel.redirects`."
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return vercelOutputConfigSchema.parse({
|
|
148
|
+
version: 3,
|
|
149
|
+
...(_h = resolvedConfig.vercel) == null ? void 0 : _h.config,
|
|
161
150
|
routes: [
|
|
162
151
|
...routes ?? [],
|
|
163
|
-
...(config == null ? void 0 :
|
|
164
|
-
...((_c = (_b = resolvedConfig.vercel) == null ? void 0 : _b.config) == null ? void 0 : _c.routes) ?? []
|
|
152
|
+
...((_j = (_i = resolvedConfig.vercel) == null ? void 0 : _i.config) == null ? void 0 : _j.routes) ?? []
|
|
165
153
|
],
|
|
166
|
-
overrides:
|
|
167
|
-
|
|
154
|
+
overrides: {
|
|
155
|
+
...(_l = (_k = resolvedConfig.vercel) == null ? void 0 : _k.config) == null ? void 0 : _l.overrides,
|
|
156
|
+
...overrides
|
|
157
|
+
}
|
|
158
|
+
});
|
|
168
159
|
}
|
|
169
160
|
function getConfigDestination(resolvedConfig) {
|
|
170
161
|
return path2.join(getOutput(resolvedConfig), "config.json");
|
|
171
162
|
}
|
|
172
|
-
async function writeConfig(resolvedConfig,
|
|
173
|
-
await
|
|
163
|
+
async function writeConfig(resolvedConfig, rewrites, overrides) {
|
|
164
|
+
await fs.writeFile(
|
|
165
|
+
getConfigDestination(resolvedConfig),
|
|
166
|
+
JSON.stringify(
|
|
167
|
+
getConfig(resolvedConfig, rewrites, overrides),
|
|
168
|
+
void 0,
|
|
169
|
+
2
|
|
170
|
+
),
|
|
171
|
+
"utf-8"
|
|
172
|
+
);
|
|
174
173
|
}
|
|
175
174
|
|
|
176
175
|
// src/build.ts
|
|
@@ -217,23 +216,35 @@ var vercelOutputVcConfigSchema = z2.union([
|
|
|
217
216
|
]);
|
|
218
217
|
|
|
219
218
|
// src/build.ts
|
|
220
|
-
import
|
|
219
|
+
import fs2 from "fs/promises";
|
|
221
220
|
function getAdditionalEndpoints(resolvedConfig) {
|
|
222
221
|
var _a;
|
|
223
|
-
return (((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []).map((e) =>
|
|
224
|
-
|
|
222
|
+
return (((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []).map((e) => ({
|
|
223
|
+
...e,
|
|
224
|
+
addRoute: e.addRoute ?? true,
|
|
225
|
+
destination: path3.resolve("/", e.destination) + ".func"
|
|
225
226
|
}));
|
|
226
227
|
}
|
|
227
228
|
function getEntries(resolvedConfig) {
|
|
228
229
|
const apiEntries = glob.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
|
|
229
|
-
|
|
230
|
-
|
|
230
|
+
if (apiEntries.length > 0) {
|
|
231
|
+
console.warn(
|
|
232
|
+
"@vercel/build is currently force building /api files itself, with no way to disable it. In order to avoid double compilation, you should temporarily rename /api to /_api while using this plugin. /_api functions are compiled under .vercel/output/functions/api/*.func as if they were in /api."
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
const otherApiEntries = glob.sync(`${getRoot(resolvedConfig)}/_api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
|
|
236
|
+
return [...apiEntries, ...otherApiEntries].reduce((entryPoints, filePath) => {
|
|
237
|
+
const outFilePath = pathRelativeTo(
|
|
238
|
+
filePath,
|
|
239
|
+
resolvedConfig,
|
|
240
|
+
filePath.includes("/_api/") ? "_api" : "api"
|
|
241
|
+
);
|
|
231
242
|
const parsed = path3.parse(outFilePath);
|
|
232
|
-
|
|
243
|
+
entryPoints.push({
|
|
233
244
|
source: filePath,
|
|
234
|
-
destination: `api/${path3.posix.join(parsed.dir, parsed.name)}.func
|
|
235
|
-
|
|
236
|
-
|
|
245
|
+
destination: `api/${path3.posix.join(parsed.dir, parsed.name)}.func`,
|
|
246
|
+
addRoute: true
|
|
247
|
+
});
|
|
237
248
|
return entryPoints;
|
|
238
249
|
}, getAdditionalEndpoints(resolvedConfig));
|
|
239
250
|
}
|
|
@@ -246,8 +257,15 @@ var standardBuildOptions = {
|
|
|
246
257
|
minify: true
|
|
247
258
|
};
|
|
248
259
|
async function buildFn(resolvedConfig, entry, buildOptions) {
|
|
249
|
-
assert(
|
|
250
|
-
|
|
260
|
+
assert(
|
|
261
|
+
entry.destination.length > 0,
|
|
262
|
+
`Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`
|
|
263
|
+
);
|
|
264
|
+
const outfile = path3.join(
|
|
265
|
+
getOutput(resolvedConfig, "functions"),
|
|
266
|
+
entry.destination,
|
|
267
|
+
"index.js"
|
|
268
|
+
);
|
|
251
269
|
const options = Object.assign({}, standardBuildOptions, { outfile });
|
|
252
270
|
if (buildOptions) {
|
|
253
271
|
Object.assign(options, buildOptions);
|
|
@@ -256,30 +274,63 @@ async function buildFn(resolvedConfig, entry, buildOptions) {
|
|
|
256
274
|
if (typeof entry.source === "string") {
|
|
257
275
|
options.entryPoints = [entry.source];
|
|
258
276
|
} else {
|
|
259
|
-
assert(
|
|
260
|
-
|
|
277
|
+
assert(
|
|
278
|
+
typeof entry.source === "object",
|
|
279
|
+
`\`{ source }\` must be a string or an object`
|
|
280
|
+
);
|
|
281
|
+
assert(
|
|
282
|
+
typeof entry.source.contents === "string",
|
|
283
|
+
`\`{ contents }\` must be a string`
|
|
284
|
+
);
|
|
261
285
|
options.stdin = entry.source;
|
|
262
286
|
}
|
|
263
287
|
}
|
|
264
288
|
await build(options);
|
|
265
|
-
await writeVcConfig(resolvedConfig, entry.destination);
|
|
289
|
+
await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
|
|
266
290
|
}
|
|
267
|
-
async function writeVcConfig(resolvedConfig, destination) {
|
|
291
|
+
async function writeVcConfig(resolvedConfig, destination, edge) {
|
|
268
292
|
var _a;
|
|
269
|
-
const vcConfig = path3.join(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
293
|
+
const vcConfig = path3.join(
|
|
294
|
+
getOutput(resolvedConfig, "functions"),
|
|
295
|
+
destination,
|
|
296
|
+
".vc-config.json"
|
|
297
|
+
);
|
|
298
|
+
await fs2.writeFile(
|
|
299
|
+
vcConfig,
|
|
300
|
+
JSON.stringify(
|
|
301
|
+
vercelOutputVcConfigSchema.parse(
|
|
302
|
+
edge ? {
|
|
303
|
+
runtime: "edge",
|
|
304
|
+
entrypoint: "index.js"
|
|
305
|
+
} : {
|
|
306
|
+
runtime: "nodejs16.x",
|
|
307
|
+
handler: "index.js",
|
|
308
|
+
maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
|
|
309
|
+
launcherType: "Nodejs",
|
|
310
|
+
shouldAddHelpers: true
|
|
311
|
+
}
|
|
312
|
+
),
|
|
313
|
+
void 0,
|
|
314
|
+
2
|
|
315
|
+
),
|
|
316
|
+
"utf-8"
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
function getSourceAndDestination(destination) {
|
|
320
|
+
if (destination.startsWith("api/")) {
|
|
321
|
+
return path3.posix.resolve("/", destination);
|
|
322
|
+
}
|
|
323
|
+
return path3.posix.resolve("/", destination, ":match*");
|
|
277
324
|
}
|
|
278
325
|
async function buildEndpoints(resolvedConfig) {
|
|
279
326
|
const entries = getEntries(resolvedConfig);
|
|
280
327
|
for (const entry of entries) {
|
|
281
328
|
await buildFn(resolvedConfig, entry);
|
|
282
329
|
}
|
|
330
|
+
return entries.filter((e) => e.addRoute !== false).map((e) => e.destination.replace(/\.func$/, "")).map((destination) => ({
|
|
331
|
+
source: getSourceAndDestination(destination),
|
|
332
|
+
destination: getSourceAndDestination(destination)
|
|
333
|
+
}));
|
|
283
334
|
}
|
|
284
335
|
|
|
285
336
|
// src/prerender.ts
|
|
@@ -296,7 +347,7 @@ var vercelOutputPrerenderConfigSchema = z3.object({
|
|
|
296
347
|
}).strict();
|
|
297
348
|
|
|
298
349
|
// src/prerender.ts
|
|
299
|
-
import
|
|
350
|
+
import fs3 from "fs/promises";
|
|
300
351
|
function execPrerender(resolvedConfig) {
|
|
301
352
|
var _a;
|
|
302
353
|
const prerender = (_a = resolvedConfig.vercel) == null ? void 0 : _a.prerender;
|
|
@@ -308,40 +359,76 @@ function execPrerender(resolvedConfig) {
|
|
|
308
359
|
var group = 1;
|
|
309
360
|
async function writePrerenderConfig(resolvedConfig, destination, isr) {
|
|
310
361
|
const parsed = path4.parse(destination);
|
|
311
|
-
const outfile = path4.join(
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
362
|
+
const outfile = path4.join(
|
|
363
|
+
getOutput(resolvedConfig, "functions"),
|
|
364
|
+
parsed.dir,
|
|
365
|
+
parsed.name + ".prerender-config.json"
|
|
366
|
+
);
|
|
367
|
+
await fs3.mkdir(
|
|
368
|
+
path4.join(getOutput(resolvedConfig, "functions"), parsed.dir),
|
|
369
|
+
{ recursive: true }
|
|
370
|
+
);
|
|
371
|
+
await fs3.writeFile(
|
|
372
|
+
outfile,
|
|
373
|
+
JSON.stringify(
|
|
374
|
+
vercelOutputPrerenderConfigSchema.parse({
|
|
375
|
+
group: group++,
|
|
376
|
+
...isr
|
|
377
|
+
}),
|
|
378
|
+
void 0,
|
|
379
|
+
2
|
|
380
|
+
),
|
|
381
|
+
"utf-8"
|
|
382
|
+
);
|
|
316
383
|
}
|
|
317
384
|
function getPrerenderSymlinkInfo(resolvedConfig, destination, target) {
|
|
318
385
|
const parsed = path4.parse(destination);
|
|
319
386
|
const targetParsed = path4.parse(target);
|
|
320
387
|
return {
|
|
321
|
-
target: path4.join(
|
|
322
|
-
|
|
388
|
+
target: path4.join(
|
|
389
|
+
getOutput(resolvedConfig, "functions"),
|
|
390
|
+
targetParsed.dir,
|
|
391
|
+
targetParsed.name + ".func"
|
|
392
|
+
),
|
|
393
|
+
link: path4.join(
|
|
394
|
+
getOutput(resolvedConfig, "functions"),
|
|
395
|
+
parsed.dir,
|
|
396
|
+
parsed.name + ".func"
|
|
397
|
+
)
|
|
323
398
|
};
|
|
324
399
|
}
|
|
325
400
|
async function buildPrerenderConfigs(resolvedConfig) {
|
|
326
401
|
const isr = await getIsrConfig(resolvedConfig);
|
|
327
402
|
const entries = Object.entries(isr);
|
|
328
403
|
const rewrites = [];
|
|
329
|
-
for (const
|
|
330
|
-
const [destination, _b] = _a, _c = _b, { symlink, route } = _c, isr2 = __objRest(_c, ["symlink", "route"]);
|
|
404
|
+
for (const [destination, { symlink, route, ...isr2 }] of entries) {
|
|
331
405
|
await writePrerenderConfig(resolvedConfig, destination, isr2);
|
|
332
406
|
if (symlink) {
|
|
333
|
-
const info = getPrerenderSymlinkInfo(
|
|
407
|
+
const info = getPrerenderSymlinkInfo(
|
|
408
|
+
resolvedConfig,
|
|
409
|
+
destination,
|
|
410
|
+
symlink
|
|
411
|
+
);
|
|
334
412
|
await copyDir(info.target, info.link);
|
|
335
413
|
}
|
|
336
414
|
if (route) {
|
|
337
415
|
rewrites.push({
|
|
338
|
-
|
|
339
|
-
|
|
416
|
+
source: `(${route})`,
|
|
417
|
+
destination: `${destination}/?__original_path=$1`
|
|
340
418
|
});
|
|
341
419
|
}
|
|
342
420
|
}
|
|
343
421
|
return rewrites;
|
|
344
422
|
}
|
|
423
|
+
async function copyDir(src, dest) {
|
|
424
|
+
await fs3.mkdir(dest, { recursive: true });
|
|
425
|
+
const entries = await fs3.readdir(src, { withFileTypes: true });
|
|
426
|
+
for (const entry of entries) {
|
|
427
|
+
const srcPath = path4.join(src, entry.name);
|
|
428
|
+
const destPath = path4.join(dest, entry.name);
|
|
429
|
+
entry.isDirectory() ? await copyDir(srcPath, destPath) : await fs3.copyFile(srcPath, destPath);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
345
432
|
async function getIsrConfig(resolvedConfig) {
|
|
346
433
|
var _a;
|
|
347
434
|
const isr = ((_a = resolvedConfig.vercel) == null ? void 0 : _a.isr) ?? {};
|
|
@@ -364,41 +451,42 @@ function vercelPlugin() {
|
|
|
364
451
|
},
|
|
365
452
|
async buildStart() {
|
|
366
453
|
if (process.env.VERCEL_ENV === "production" && !process.env.ENABLE_VC_BUILD) {
|
|
367
|
-
throw new Error(
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
await cleanOutputDirectory(resolvedConfig);
|
|
371
|
-
} else {
|
|
372
|
-
await copyDistClientToOutputStatic(resolvedConfig);
|
|
454
|
+
throw new Error(
|
|
455
|
+
"Missing ENABLE_VC_BUILD=1 to your environment variables in your project settings"
|
|
456
|
+
);
|
|
373
457
|
}
|
|
374
458
|
},
|
|
375
459
|
async writeBundle() {
|
|
376
460
|
var _a;
|
|
377
|
-
if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr))
|
|
461
|
+
if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr)) {
|
|
462
|
+
await cleanOutputDirectory(resolvedConfig);
|
|
378
463
|
return;
|
|
379
|
-
|
|
464
|
+
}
|
|
380
465
|
const overrides = await execPrerender(resolvedConfig);
|
|
381
|
-
await
|
|
382
|
-
const rewrites = await
|
|
383
|
-
await
|
|
384
|
-
|
|
385
|
-
|
|
466
|
+
const userOverrides = await computeStaticHtmlOverrides(resolvedConfig);
|
|
467
|
+
const rewrites = await buildEndpoints(resolvedConfig);
|
|
468
|
+
rewrites.push(...await buildPrerenderConfigs(resolvedConfig));
|
|
469
|
+
await writeConfig(resolvedConfig, rewrites, {
|
|
470
|
+
...userOverrides,
|
|
471
|
+
...overrides
|
|
386
472
|
});
|
|
387
473
|
}
|
|
388
474
|
};
|
|
389
475
|
}
|
|
390
|
-
async function copyDistClientToOutputStatic(resolvedConfig) {
|
|
391
|
-
await copyDir(getOutDir(resolvedConfig, "client"), getOutput(resolvedConfig, "static"));
|
|
392
|
-
}
|
|
393
476
|
async function cleanOutputDirectory(resolvedConfig) {
|
|
394
|
-
await
|
|
477
|
+
await fs4.rm(getOutput(resolvedConfig), {
|
|
395
478
|
recursive: true,
|
|
396
479
|
force: true
|
|
397
480
|
});
|
|
398
481
|
}
|
|
399
482
|
async function computeStaticHtmlOverrides(resolvedConfig) {
|
|
400
483
|
const staticAbsolutePath = getOutput(resolvedConfig, "static");
|
|
401
|
-
const files = await getStaticHtmlFiles(
|
|
484
|
+
const files = await getStaticHtmlFiles(staticAbsolutePath);
|
|
485
|
+
const publicDir = getPublic(resolvedConfig);
|
|
486
|
+
const publicFiles = await getStaticHtmlFiles(publicDir);
|
|
487
|
+
files.push(
|
|
488
|
+
...publicFiles.map((f) => f.replace(publicDir, staticAbsolutePath))
|
|
489
|
+
);
|
|
402
490
|
return files.reduce((acc, curr) => {
|
|
403
491
|
const relPath = path5.relative(staticAbsolutePath, curr);
|
|
404
492
|
const parsed = path5.parse(relPath);
|
|
@@ -409,12 +497,17 @@ async function computeStaticHtmlOverrides(resolvedConfig) {
|
|
|
409
497
|
return acc;
|
|
410
498
|
}, {});
|
|
411
499
|
}
|
|
412
|
-
async function getStaticHtmlFiles(
|
|
413
|
-
|
|
500
|
+
async function getStaticHtmlFiles(src) {
|
|
501
|
+
try {
|
|
502
|
+
await fs4.stat(src);
|
|
503
|
+
} catch (e) {
|
|
504
|
+
return [];
|
|
505
|
+
}
|
|
506
|
+
const entries = await fs4.readdir(src, { withFileTypes: true });
|
|
414
507
|
const htmlFiles = [];
|
|
415
508
|
for (const entry of entries) {
|
|
416
509
|
const srcPath = path5.join(src, entry.name);
|
|
417
|
-
entry.isDirectory() ? htmlFiles.push(...await getStaticHtmlFiles(
|
|
510
|
+
entry.isDirectory() ? htmlFiles.push(...await getStaticHtmlFiles(srcPath)) : srcPath.endsWith(".html") ? htmlFiles.push(srcPath) : void 0;
|
|
418
511
|
}
|
|
419
512
|
return htmlFiles;
|
|
420
513
|
}
|