vite-plugin-vercel 0.1.4 → 0.1.6

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 CHANGED
@@ -14,7 +14,7 @@ Its purpose is to help you bundle your application in `.vercel` folder as suppor
14
14
  - see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L54)
15
15
  - [x] [ISR/Prerender functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/prerender-functions)
16
16
  - see [`isr` config](/packages/vercel/src/types.ts#L81). Also see implementation of [vite-plugin-ssr](/packages/vite-plugin-ssr/vite-plugin-ssr.ts) for example
17
- - [ ] [Edge functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions)
17
+ - [x] [Edge functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions)
18
18
  - [ ] [Images optimization support](https://vercel.com/docs/build-output-api/v3#build-output-configuration/supported-properties/images)
19
19
  - [ ] [Preview mode support](https://vercel.com/docs/build-output-api/v3#features/preview-mode)
20
20
  - [x] [Advanced config override](/packages/vercel/src/types.ts#L15)
package/dist/index.cjs CHANGED
@@ -17,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  }
18
18
  return to;
19
19
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
21
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
25
 
23
26
  // src/index.ts
@@ -36,7 +39,11 @@ function getRoot(config) {
36
39
  }
37
40
  function getOutput(config, suffix) {
38
41
  var _a, _b;
39
- return import_path.default.join(((_a = config.vercel) == null ? void 0 : _a.outDir) ? "" : getRoot(config), ((_b = config.vercel) == null ? void 0 : _b.outDir) ?? ".vercel/output", suffix ?? "");
42
+ return import_path.default.join(
43
+ ((_a = config.vercel) == null ? void 0 : _a.outDir) ? "" : getRoot(config),
44
+ ((_b = config.vercel) == null ? void 0 : _b.outDir) ?? ".vercel/output",
45
+ suffix ?? ""
46
+ );
40
47
  }
41
48
  function getPublic(config) {
42
49
  return import_path.default.join(getRoot(config), config.publicDir || "public");
@@ -51,60 +58,64 @@ var import_path2 = __toESM(require("path"), 1);
51
58
 
52
59
  // src/schemas/config/config.ts
53
60
  var import_zod = require("zod");
54
- var HasOrMissing = import_zod.z.array(import_zod.z.union([
55
- import_zod.z.object({
56
- type: import_zod.z.literal("host"),
57
- value: import_zod.z.string()
58
- }).strict(),
59
- import_zod.z.object({
60
- type: import_zod.z.literal("header"),
61
- key: import_zod.z.string(),
62
- value: import_zod.z.string().optional()
63
- }).strict(),
64
- import_zod.z.object({
65
- type: import_zod.z.literal("cookie"),
66
- key: import_zod.z.string(),
67
- value: import_zod.z.string().optional()
68
- }).strict(),
69
- import_zod.z.object({
70
- type: import_zod.z.literal("query"),
71
- key: import_zod.z.string(),
72
- value: import_zod.z.string().optional()
73
- }).strict()
74
- ])).optional();
75
- var vercelOutputConfigSchema = import_zod.z.object({
76
- version: import_zod.z.literal(3),
77
- routes: import_zod.z.array(import_zod.z.union([
61
+ var HasOrMissing = import_zod.z.array(
62
+ import_zod.z.union([
63
+ import_zod.z.object({
64
+ type: import_zod.z.literal("host"),
65
+ value: import_zod.z.string()
66
+ }).strict(),
78
67
  import_zod.z.object({
79
- src: import_zod.z.string(),
80
- dest: import_zod.z.string().optional(),
81
- headers: import_zod.z.record(import_zod.z.string()).optional(),
82
- methods: import_zod.z.array(import_zod.z.string()).optional(),
83
- status: import_zod.z.number().int().positive().optional(),
84
- continue: import_zod.z.boolean().optional(),
85
- check: import_zod.z.boolean().optional(),
86
- missing: HasOrMissing,
87
- has: HasOrMissing,
88
- locale: import_zod.z.object({
89
- redirect: import_zod.z.record(import_zod.z.string()).optional(),
90
- cookie: import_zod.z.string().optional()
91
- }).strict().optional(),
92
- middlewarePath: import_zod.z.string().optional()
68
+ type: import_zod.z.literal("header"),
69
+ key: import_zod.z.string(),
70
+ value: import_zod.z.string().optional()
93
71
  }).strict(),
94
72
  import_zod.z.object({
95
- handle: import_zod.z.union([
96
- import_zod.z.literal("rewrite"),
97
- import_zod.z.literal("filesystem"),
98
- import_zod.z.literal("resource"),
99
- import_zod.z.literal("miss"),
100
- import_zod.z.literal("hit"),
101
- import_zod.z.literal("error")
102
- ]),
103
- src: import_zod.z.string().optional(),
104
- dest: import_zod.z.string().optional(),
105
- status: import_zod.z.number().optional()
73
+ type: import_zod.z.literal("cookie"),
74
+ key: import_zod.z.string(),
75
+ value: import_zod.z.string().optional()
76
+ }).strict(),
77
+ import_zod.z.object({
78
+ type: import_zod.z.literal("query"),
79
+ key: import_zod.z.string(),
80
+ value: import_zod.z.string().optional()
106
81
  }).strict()
107
- ])).optional(),
82
+ ])
83
+ ).optional();
84
+ var vercelOutputConfigSchema = import_zod.z.object({
85
+ version: import_zod.z.literal(3),
86
+ routes: import_zod.z.array(
87
+ import_zod.z.union([
88
+ import_zod.z.object({
89
+ src: import_zod.z.string(),
90
+ dest: import_zod.z.string().optional(),
91
+ headers: import_zod.z.record(import_zod.z.string()).optional(),
92
+ methods: import_zod.z.array(import_zod.z.string()).optional(),
93
+ status: import_zod.z.number().int().positive().optional(),
94
+ continue: import_zod.z.boolean().optional(),
95
+ check: import_zod.z.boolean().optional(),
96
+ missing: HasOrMissing,
97
+ has: HasOrMissing,
98
+ locale: import_zod.z.object({
99
+ redirect: import_zod.z.record(import_zod.z.string()).optional(),
100
+ cookie: import_zod.z.string().optional()
101
+ }).strict().optional(),
102
+ middlewarePath: import_zod.z.string().optional()
103
+ }).strict(),
104
+ import_zod.z.object({
105
+ handle: import_zod.z.union([
106
+ import_zod.z.literal("rewrite"),
107
+ import_zod.z.literal("filesystem"),
108
+ import_zod.z.literal("resource"),
109
+ import_zod.z.literal("miss"),
110
+ import_zod.z.literal("hit"),
111
+ import_zod.z.literal("error")
112
+ ]),
113
+ src: import_zod.z.string().optional(),
114
+ dest: import_zod.z.string().optional(),
115
+ status: import_zod.z.number().optional()
116
+ }).strict()
117
+ ])
118
+ ).optional(),
108
119
  images: import_zod.z.object({
109
120
  sizes: import_zod.z.tuple([
110
121
  import_zod.z.number().int().positive(),
@@ -116,14 +127,18 @@ var vercelOutputConfigSchema = import_zod.z.object({
116
127
  dangerouslyAllowSVG: import_zod.z.boolean().optional(),
117
128
  contentSecurityPolicy: import_zod.z.string().optional()
118
129
  }).strict().optional(),
119
- wildcard: import_zod.z.array(import_zod.z.object({
120
- domain: import_zod.z.string(),
121
- value: import_zod.z.string()
122
- }).strict()).optional(),
123
- overrides: import_zod.z.record(import_zod.z.object({
124
- path: import_zod.z.string().optional(),
125
- contentType: import_zod.z.string().optional()
126
- }).strict()).optional(),
130
+ wildcard: import_zod.z.array(
131
+ import_zod.z.object({
132
+ domain: import_zod.z.string(),
133
+ value: import_zod.z.string()
134
+ }).strict()
135
+ ).optional(),
136
+ overrides: import_zod.z.record(
137
+ import_zod.z.object({
138
+ path: import_zod.z.string().optional(),
139
+ contentType: import_zod.z.string().optional()
140
+ }).strict()
141
+ ).optional(),
127
142
  cache: import_zod.z.array(import_zod.z.string()).optional()
128
143
  }).strict();
129
144
 
@@ -144,18 +159,18 @@ function getConfig(resolvedConfig, rewrites, overrides) {
144
159
  ...rewrites ?? []
145
160
  ];
146
161
  const { routes, error } = (0, import_routing_utils.getTransformedRoutes)({
147
- nowConfig: {
148
- cleanUrls: ((_b = resolvedConfig.vercel) == null ? void 0 : _b.cleanUrls) ?? true,
149
- trailingSlash: (_c = resolvedConfig.vercel) == null ? void 0 : _c.trailingSlash,
150
- rewrites: reorderEnforce(_rewrites),
151
- redirects: ((_d = resolvedConfig.vercel) == null ? void 0 : _d.redirects) ? reorderEnforce((_e = resolvedConfig.vercel) == null ? void 0 : _e.redirects) : void 0
152
- }
162
+ cleanUrls: ((_b = resolvedConfig.vercel) == null ? void 0 : _b.cleanUrls) ?? true,
163
+ trailingSlash: (_c = resolvedConfig.vercel) == null ? void 0 : _c.trailingSlash,
164
+ rewrites: reorderEnforce(_rewrites),
165
+ redirects: ((_d = resolvedConfig.vercel) == null ? void 0 : _d.redirects) ? reorderEnforce((_e = resolvedConfig.vercel) == null ? void 0 : _e.redirects) : void 0
153
166
  });
154
167
  if (error) {
155
168
  throw error;
156
169
  }
157
170
  if (((_g = (_f = resolvedConfig.vercel) == null ? void 0 : _f.config) == null ? void 0 : _g.routes) && resolvedConfig.vercel.config.routes.length > 0) {
158
- console.warn("It is discouraged to use `vercel.config.routes` to override routes. Prefer using `vercel.rewrites` and `vercel.redirects`.");
171
+ console.warn(
172
+ "It is discouraged to use `vercel.config.routes` to override routes. Prefer using `vercel.rewrites` and `vercel.redirects`."
173
+ );
159
174
  }
160
175
  return vercelOutputConfigSchema.parse({
161
176
  version: 3,
@@ -174,7 +189,15 @@ function getConfigDestination(resolvedConfig) {
174
189
  return import_path2.default.join(getOutput(resolvedConfig), "config.json");
175
190
  }
176
191
  async function writeConfig(resolvedConfig, rewrites, overrides) {
177
- await import_promises.default.writeFile(getConfigDestination(resolvedConfig), JSON.stringify(getConfig(resolvedConfig, rewrites, overrides), void 0, 2), "utf-8");
192
+ await import_promises.default.writeFile(
193
+ getConfigDestination(resolvedConfig),
194
+ JSON.stringify(
195
+ getConfig(resolvedConfig, rewrites, overrides),
196
+ void 0,
197
+ 2
198
+ ),
199
+ "utf-8"
200
+ );
178
201
  }
179
202
 
180
203
  // src/build.ts
@@ -233,11 +256,17 @@ function getAdditionalEndpoints(resolvedConfig) {
233
256
  function getEntries(resolvedConfig) {
234
257
  const apiEntries = import_fast_glob.default.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !import_path3.default.basename(filepath).startsWith("_"));
235
258
  if (apiEntries.length > 0) {
236
- console.warn("@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.");
259
+ console.warn(
260
+ "@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."
261
+ );
237
262
  }
238
263
  const otherApiEntries = import_fast_glob.default.sync(`${getRoot(resolvedConfig)}/_api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !import_path3.default.basename(filepath).startsWith("_"));
239
264
  return [...apiEntries, ...otherApiEntries].reduce((entryPoints, filePath) => {
240
- const outFilePath = pathRelativeTo(filePath, resolvedConfig, filePath.includes("/_api/") ? "_api" : "api");
265
+ const outFilePath = pathRelativeTo(
266
+ filePath,
267
+ resolvedConfig,
268
+ filePath.includes("/_api/") ? "_api" : "api"
269
+ );
241
270
  const parsed = import_path3.default.parse(outFilePath);
242
271
  entryPoints.push({
243
272
  source: filePath,
@@ -256,8 +285,15 @@ var standardBuildOptions = {
256
285
  minify: true
257
286
  };
258
287
  async function buildFn(resolvedConfig, entry, buildOptions) {
259
- assert(entry.destination.length > 0, `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`);
260
- const outfile = import_path3.default.join(getOutput(resolvedConfig, "functions"), entry.destination, "index.js");
288
+ assert(
289
+ entry.destination.length > 0,
290
+ `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`
291
+ );
292
+ const outfile = import_path3.default.join(
293
+ getOutput(resolvedConfig, "functions"),
294
+ entry.destination,
295
+ "index.js"
296
+ );
261
297
  const options = Object.assign({}, standardBuildOptions, { outfile });
262
298
  if (buildOptions) {
263
299
  Object.assign(options, buildOptions);
@@ -266,24 +302,47 @@ async function buildFn(resolvedConfig, entry, buildOptions) {
266
302
  if (typeof entry.source === "string") {
267
303
  options.entryPoints = [entry.source];
268
304
  } else {
269
- assert(typeof entry.source === "object", `\`{ source }\` must be a string or an object`);
270
- assert(typeof entry.source.contents === "string", `\`{ contents }\` must be a string`);
305
+ assert(
306
+ typeof entry.source === "object",
307
+ `\`{ source }\` must be a string or an object`
308
+ );
309
+ assert(
310
+ typeof entry.source.contents === "string",
311
+ `\`{ contents }\` must be a string`
312
+ );
271
313
  options.stdin = entry.source;
272
314
  }
273
315
  }
274
316
  await (0, import_esbuild.build)(options);
275
- await writeVcConfig(resolvedConfig, entry.destination);
317
+ await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
276
318
  }
277
- async function writeVcConfig(resolvedConfig, destination) {
319
+ async function writeVcConfig(resolvedConfig, destination, edge) {
278
320
  var _a;
279
- const vcConfig = import_path3.default.join(getOutput(resolvedConfig, "functions"), destination, ".vc-config.json");
280
- await import_promises2.default.writeFile(vcConfig, JSON.stringify(vercelOutputVcConfigSchema.parse({
281
- runtime: "nodejs16.x",
282
- handler: "index.js",
283
- maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
284
- launcherType: "Nodejs",
285
- shouldAddHelpers: true
286
- }), void 0, 2), "utf-8");
321
+ const vcConfig = import_path3.default.join(
322
+ getOutput(resolvedConfig, "functions"),
323
+ destination,
324
+ ".vc-config.json"
325
+ );
326
+ await import_promises2.default.writeFile(
327
+ vcConfig,
328
+ JSON.stringify(
329
+ vercelOutputVcConfigSchema.parse(
330
+ edge ? {
331
+ runtime: "edge",
332
+ entrypoint: "index.js"
333
+ } : {
334
+ runtime: "nodejs16.x",
335
+ handler: "index.js",
336
+ maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
337
+ launcherType: "Nodejs",
338
+ shouldAddHelpers: true
339
+ }
340
+ ),
341
+ void 0,
342
+ 2
343
+ ),
344
+ "utf-8"
345
+ );
287
346
  }
288
347
  function getSourceAndDestination(destination) {
289
348
  if (destination.startsWith("api/")) {
@@ -328,19 +387,42 @@ function execPrerender(resolvedConfig) {
328
387
  var group = 1;
329
388
  async function writePrerenderConfig(resolvedConfig, destination, isr) {
330
389
  const parsed = import_path4.default.parse(destination);
331
- const outfile = import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".prerender-config.json");
332
- await import_promises3.default.mkdir(import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir), { recursive: true });
333
- await import_promises3.default.writeFile(outfile, JSON.stringify(vercelOutputPrerenderConfigSchema.parse({
334
- group: group++,
335
- ...isr
336
- }), void 0, 2), "utf-8");
390
+ const outfile = import_path4.default.join(
391
+ getOutput(resolvedConfig, "functions"),
392
+ parsed.dir,
393
+ parsed.name + ".prerender-config.json"
394
+ );
395
+ await import_promises3.default.mkdir(
396
+ import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir),
397
+ { recursive: true }
398
+ );
399
+ await import_promises3.default.writeFile(
400
+ outfile,
401
+ JSON.stringify(
402
+ vercelOutputPrerenderConfigSchema.parse({
403
+ group: group++,
404
+ ...isr
405
+ }),
406
+ void 0,
407
+ 2
408
+ ),
409
+ "utf-8"
410
+ );
337
411
  }
338
412
  function getPrerenderSymlinkInfo(resolvedConfig, destination, target) {
339
413
  const parsed = import_path4.default.parse(destination);
340
414
  const targetParsed = import_path4.default.parse(target);
341
415
  return {
342
- target: import_path4.default.join(getOutput(resolvedConfig, "functions"), targetParsed.dir, targetParsed.name + ".func"),
343
- link: import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".func")
416
+ target: import_path4.default.join(
417
+ getOutput(resolvedConfig, "functions"),
418
+ targetParsed.dir,
419
+ targetParsed.name + ".func"
420
+ ),
421
+ link: import_path4.default.join(
422
+ getOutput(resolvedConfig, "functions"),
423
+ parsed.dir,
424
+ parsed.name + ".func"
425
+ )
344
426
  };
345
427
  }
346
428
  async function buildPrerenderConfigs(resolvedConfig) {
@@ -350,7 +432,11 @@ async function buildPrerenderConfigs(resolvedConfig) {
350
432
  for (const [destination, { symlink, route, ...isr2 }] of entries) {
351
433
  await writePrerenderConfig(resolvedConfig, destination, isr2);
352
434
  if (symlink) {
353
- const info = getPrerenderSymlinkInfo(resolvedConfig, destination, symlink);
435
+ const info = getPrerenderSymlinkInfo(
436
+ resolvedConfig,
437
+ destination,
438
+ symlink
439
+ );
354
440
  await copyDir(info.target, info.link);
355
441
  }
356
442
  if (route) {
@@ -393,7 +479,9 @@ function vercelPlugin() {
393
479
  },
394
480
  async buildStart() {
395
481
  if (process.env.VERCEL_ENV === "production" && !process.env.ENABLE_VC_BUILD) {
396
- throw new Error("Missing ENABLE_VC_BUILD=1 to your environment variables in your project settings");
482
+ throw new Error(
483
+ "Missing ENABLE_VC_BUILD=1 to your environment variables in your project settings"
484
+ );
397
485
  }
398
486
  },
399
487
  async writeBundle() {
@@ -424,7 +512,9 @@ async function computeStaticHtmlOverrides(resolvedConfig) {
424
512
  const files = await getStaticHtmlFiles(staticAbsolutePath);
425
513
  const publicDir = getPublic(resolvedConfig);
426
514
  const publicFiles = await getStaticHtmlFiles(publicDir);
427
- files.push(...publicFiles.map((f) => f.replace(publicDir, staticAbsolutePath)));
515
+ files.push(
516
+ ...publicFiles.map((f) => f.replace(publicDir, staticAbsolutePath))
517
+ );
428
518
  return files.reduce((acc, curr) => {
429
519
  const relPath = import_path5.default.relative(staticAbsolutePath, curr);
430
520
  const parsed = import_path5.default.parse(relPath);
package/dist/index.d.ts CHANGED
@@ -1,7 +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
+ import { Rewrite, Redirect } from '@vercel/routing-utils';
5
5
 
6
6
  /**
7
7
  * Schema definition for `.vercel/output/config.json`
@@ -411,7 +411,7 @@ declare const vercelOutputConfigSchema: z.ZodObject<{
411
411
  cache?: string[] | undefined;
412
412
  version: 3;
413
413
  }>;
414
- declare type VercelOutputConfig = z.infer<typeof vercelOutputConfigSchema>;
414
+ type VercelOutputConfig = z.infer<typeof vercelOutputConfigSchema>;
415
415
 
416
416
  /**
417
417
  * Schema definition for `.vercel/output/config.json`
@@ -486,7 +486,7 @@ declare const vercelOutputVcConfigSchema: z.ZodUnion<[z.ZodObject<{
486
486
  handler: string;
487
487
  launcherType: "Nodejs";
488
488
  }>]>;
489
- declare type VercelOutputVcConfig = z.infer<typeof vercelOutputVcConfigSchema>;
489
+ type VercelOutputVcConfig = z.infer<typeof vercelOutputVcConfigSchema>;
490
490
 
491
491
  /**
492
492
  * Schema definition for `.vercel/output/config.json`
@@ -512,12 +512,12 @@ declare const vercelOutputPrerenderConfigSchema: z.ZodObject<{
512
512
  allowQuery?: string[] | undefined;
513
513
  expiration: number | false;
514
514
  }>;
515
- declare type VercelOutputPrerenderConfig = z.infer<typeof vercelOutputPrerenderConfigSchema>;
515
+ type VercelOutputPrerenderConfig = z.infer<typeof vercelOutputPrerenderConfigSchema>;
516
516
 
517
- declare type ViteVercelRewrite = NonNullable<VercelConfig['rewrites']>[number] & {
517
+ type ViteVercelRewrite = Rewrite & {
518
518
  enforce?: 'pre' | 'post';
519
519
  };
520
- declare type ViteVercelRedirect = NonNullable<VercelConfig['redirects']>[number] & {
520
+ type ViteVercelRedirect = Redirect & {
521
521
  enforce?: 'pre' | 'post';
522
522
  };
523
523
  interface ViteVercelConfig {
@@ -561,8 +561,8 @@ interface ViteVercelConfig {
561
561
  */
562
562
  rewrites?: ViteVercelRewrite[];
563
563
  redirects?: ViteVercelRedirect[];
564
- cleanUrls?: VercelConfig['cleanUrls'];
565
- trailingSlash?: VercelConfig['trailingSlash'];
564
+ cleanUrls?: boolean;
565
+ trailingSlash?: boolean;
566
566
  additionalEndpoints?: ViteVercelApiEntry[];
567
567
  /**
568
568
  * Advanced configuration to override .vercel/output/config.json
@@ -592,7 +592,7 @@ interface ViteVercelConfig {
592
592
  */
593
593
  isr?: Record<string, VercelOutputIsr> | (() => Promise<Record<string, VercelOutputIsr>> | Record<string, VercelOutputIsr>);
594
594
  /**
595
- * Defaults to `.vercel/output`. Mostly useful for testing prupose
595
+ * Defaults to `.vercel/output`. Mostly useful for testing purpose
596
596
  * @protected
597
597
  */
598
598
  outDir?: string;
@@ -604,8 +604,8 @@ interface VercelOutputIsr extends VercelOutputPrerenderConfig {
604
604
  /**
605
605
  * Keys are path relative to .vercel/output/static directory
606
606
  */
607
- declare type ViteVercelPrerenderRoute = VercelOutputConfig['overrides'];
608
- declare type ViteVercelPrerenderFn = (resolvedConfig: ResolvedConfig) => ViteVercelPrerenderRoute | Promise<ViteVercelPrerenderRoute>;
607
+ type ViteVercelPrerenderRoute = VercelOutputConfig['overrides'];
608
+ type ViteVercelPrerenderFn = (resolvedConfig: ResolvedConfig) => ViteVercelPrerenderRoute | Promise<ViteVercelPrerenderRoute>;
609
609
  interface ViteVercelApiEntry {
610
610
  /**
611
611
  * Path to entry file, or stdin config
@@ -624,6 +624,10 @@ interface ViteVercelApiEntry {
624
624
  * Set to `false` to disable
625
625
  */
626
626
  addRoute?: boolean;
627
+ /**
628
+ * Set to `true` to mark this function as an Edge Function
629
+ */
630
+ edge?: boolean;
627
631
  }
628
632
 
629
633
  declare function allPlugins(): Plugin[];
package/dist/index.js CHANGED
@@ -9,7 +9,11 @@ function getRoot(config) {
9
9
  }
10
10
  function getOutput(config, suffix) {
11
11
  var _a, _b;
12
- return path.join(((_a = config.vercel) == null ? void 0 : _a.outDir) ? "" : getRoot(config), ((_b = config.vercel) == null ? void 0 : _b.outDir) ?? ".vercel/output", suffix ?? "");
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
+ );
13
17
  }
14
18
  function getPublic(config) {
15
19
  return path.join(getRoot(config), config.publicDir || "public");
@@ -24,60 +28,64 @@ import path2 from "path";
24
28
 
25
29
  // src/schemas/config/config.ts
26
30
  import { z } from "zod";
27
- var HasOrMissing = z.array(z.union([
28
- z.object({
29
- type: z.literal("host"),
30
- value: z.string()
31
- }).strict(),
32
- z.object({
33
- type: z.literal("header"),
34
- key: z.string(),
35
- value: z.string().optional()
36
- }).strict(),
37
- z.object({
38
- type: z.literal("cookie"),
39
- key: z.string(),
40
- value: z.string().optional()
41
- }).strict(),
42
- z.object({
43
- type: z.literal("query"),
44
- key: z.string(),
45
- value: z.string().optional()
46
- }).strict()
47
- ])).optional();
48
- var vercelOutputConfigSchema = z.object({
49
- version: z.literal(3),
50
- 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(),
51
37
  z.object({
52
- src: z.string(),
53
- dest: z.string().optional(),
54
- headers: z.record(z.string()).optional(),
55
- methods: z.array(z.string()).optional(),
56
- status: z.number().int().positive().optional(),
57
- continue: z.boolean().optional(),
58
- check: z.boolean().optional(),
59
- missing: HasOrMissing,
60
- has: HasOrMissing,
61
- locale: z.object({
62
- redirect: z.record(z.string()).optional(),
63
- cookie: z.string().optional()
64
- }).strict().optional(),
65
- middlewarePath: z.string().optional()
38
+ type: z.literal("header"),
39
+ key: z.string(),
40
+ value: z.string().optional()
66
41
  }).strict(),
67
42
  z.object({
68
- handle: z.union([
69
- z.literal("rewrite"),
70
- z.literal("filesystem"),
71
- z.literal("resource"),
72
- z.literal("miss"),
73
- z.literal("hit"),
74
- z.literal("error")
75
- ]),
76
- src: z.string().optional(),
77
- dest: z.string().optional(),
78
- 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()
79
51
  }).strict()
80
- ])).optional(),
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(),
81
89
  images: z.object({
82
90
  sizes: z.tuple([
83
91
  z.number().int().positive(),
@@ -89,14 +97,18 @@ var vercelOutputConfigSchema = z.object({
89
97
  dangerouslyAllowSVG: z.boolean().optional(),
90
98
  contentSecurityPolicy: z.string().optional()
91
99
  }).strict().optional(),
92
- wildcard: z.array(z.object({
93
- domain: z.string(),
94
- value: z.string()
95
- }).strict()).optional(),
96
- overrides: z.record(z.object({
97
- path: z.string().optional(),
98
- contentType: z.string().optional()
99
- }).strict()).optional(),
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(),
100
112
  cache: z.array(z.string()).optional()
101
113
  }).strict();
102
114
 
@@ -117,18 +129,18 @@ function getConfig(resolvedConfig, rewrites, overrides) {
117
129
  ...rewrites ?? []
118
130
  ];
119
131
  const { routes, error } = getTransformedRoutes({
120
- nowConfig: {
121
- cleanUrls: ((_b = resolvedConfig.vercel) == null ? void 0 : _b.cleanUrls) ?? true,
122
- trailingSlash: (_c = resolvedConfig.vercel) == null ? void 0 : _c.trailingSlash,
123
- rewrites: reorderEnforce(_rewrites),
124
- redirects: ((_d = resolvedConfig.vercel) == null ? void 0 : _d.redirects) ? reorderEnforce((_e = resolvedConfig.vercel) == null ? void 0 : _e.redirects) : void 0
125
- }
132
+ cleanUrls: ((_b = resolvedConfig.vercel) == null ? void 0 : _b.cleanUrls) ?? true,
133
+ trailingSlash: (_c = resolvedConfig.vercel) == null ? void 0 : _c.trailingSlash,
134
+ rewrites: reorderEnforce(_rewrites),
135
+ redirects: ((_d = resolvedConfig.vercel) == null ? void 0 : _d.redirects) ? reorderEnforce((_e = resolvedConfig.vercel) == null ? void 0 : _e.redirects) : void 0
126
136
  });
127
137
  if (error) {
128
138
  throw error;
129
139
  }
130
140
  if (((_g = (_f = resolvedConfig.vercel) == null ? void 0 : _f.config) == null ? void 0 : _g.routes) && resolvedConfig.vercel.config.routes.length > 0) {
131
- console.warn("It is discouraged to use `vercel.config.routes` to override routes. Prefer using `vercel.rewrites` and `vercel.redirects`.");
141
+ console.warn(
142
+ "It is discouraged to use `vercel.config.routes` to override routes. Prefer using `vercel.rewrites` and `vercel.redirects`."
143
+ );
132
144
  }
133
145
  return vercelOutputConfigSchema.parse({
134
146
  version: 3,
@@ -147,7 +159,15 @@ function getConfigDestination(resolvedConfig) {
147
159
  return path2.join(getOutput(resolvedConfig), "config.json");
148
160
  }
149
161
  async function writeConfig(resolvedConfig, rewrites, overrides) {
150
- await fs.writeFile(getConfigDestination(resolvedConfig), JSON.stringify(getConfig(resolvedConfig, rewrites, overrides), void 0, 2), "utf-8");
162
+ await fs.writeFile(
163
+ getConfigDestination(resolvedConfig),
164
+ JSON.stringify(
165
+ getConfig(resolvedConfig, rewrites, overrides),
166
+ void 0,
167
+ 2
168
+ ),
169
+ "utf-8"
170
+ );
151
171
  }
152
172
 
153
173
  // src/build.ts
@@ -206,11 +226,17 @@ function getAdditionalEndpoints(resolvedConfig) {
206
226
  function getEntries(resolvedConfig) {
207
227
  const apiEntries = glob.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
208
228
  if (apiEntries.length > 0) {
209
- console.warn("@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.");
229
+ console.warn(
230
+ "@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."
231
+ );
210
232
  }
211
233
  const otherApiEntries = glob.sync(`${getRoot(resolvedConfig)}/_api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
212
234
  return [...apiEntries, ...otherApiEntries].reduce((entryPoints, filePath) => {
213
- const outFilePath = pathRelativeTo(filePath, resolvedConfig, filePath.includes("/_api/") ? "_api" : "api");
235
+ const outFilePath = pathRelativeTo(
236
+ filePath,
237
+ resolvedConfig,
238
+ filePath.includes("/_api/") ? "_api" : "api"
239
+ );
214
240
  const parsed = path3.parse(outFilePath);
215
241
  entryPoints.push({
216
242
  source: filePath,
@@ -229,8 +255,15 @@ var standardBuildOptions = {
229
255
  minify: true
230
256
  };
231
257
  async function buildFn(resolvedConfig, entry, buildOptions) {
232
- assert(entry.destination.length > 0, `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`);
233
- const outfile = path3.join(getOutput(resolvedConfig, "functions"), entry.destination, "index.js");
258
+ assert(
259
+ entry.destination.length > 0,
260
+ `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`
261
+ );
262
+ const outfile = path3.join(
263
+ getOutput(resolvedConfig, "functions"),
264
+ entry.destination,
265
+ "index.js"
266
+ );
234
267
  const options = Object.assign({}, standardBuildOptions, { outfile });
235
268
  if (buildOptions) {
236
269
  Object.assign(options, buildOptions);
@@ -239,24 +272,47 @@ async function buildFn(resolvedConfig, entry, buildOptions) {
239
272
  if (typeof entry.source === "string") {
240
273
  options.entryPoints = [entry.source];
241
274
  } else {
242
- assert(typeof entry.source === "object", `\`{ source }\` must be a string or an object`);
243
- assert(typeof entry.source.contents === "string", `\`{ contents }\` must be a string`);
275
+ assert(
276
+ typeof entry.source === "object",
277
+ `\`{ source }\` must be a string or an object`
278
+ );
279
+ assert(
280
+ typeof entry.source.contents === "string",
281
+ `\`{ contents }\` must be a string`
282
+ );
244
283
  options.stdin = entry.source;
245
284
  }
246
285
  }
247
286
  await build(options);
248
- await writeVcConfig(resolvedConfig, entry.destination);
287
+ await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
249
288
  }
250
- async function writeVcConfig(resolvedConfig, destination) {
289
+ async function writeVcConfig(resolvedConfig, destination, edge) {
251
290
  var _a;
252
- const vcConfig = path3.join(getOutput(resolvedConfig, "functions"), destination, ".vc-config.json");
253
- await fs2.writeFile(vcConfig, JSON.stringify(vercelOutputVcConfigSchema.parse({
254
- runtime: "nodejs16.x",
255
- handler: "index.js",
256
- maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
257
- launcherType: "Nodejs",
258
- shouldAddHelpers: true
259
- }), void 0, 2), "utf-8");
291
+ const vcConfig = path3.join(
292
+ getOutput(resolvedConfig, "functions"),
293
+ destination,
294
+ ".vc-config.json"
295
+ );
296
+ await fs2.writeFile(
297
+ vcConfig,
298
+ JSON.stringify(
299
+ vercelOutputVcConfigSchema.parse(
300
+ edge ? {
301
+ runtime: "edge",
302
+ entrypoint: "index.js"
303
+ } : {
304
+ runtime: "nodejs16.x",
305
+ handler: "index.js",
306
+ maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
307
+ launcherType: "Nodejs",
308
+ shouldAddHelpers: true
309
+ }
310
+ ),
311
+ void 0,
312
+ 2
313
+ ),
314
+ "utf-8"
315
+ );
260
316
  }
261
317
  function getSourceAndDestination(destination) {
262
318
  if (destination.startsWith("api/")) {
@@ -301,19 +357,42 @@ function execPrerender(resolvedConfig) {
301
357
  var group = 1;
302
358
  async function writePrerenderConfig(resolvedConfig, destination, isr) {
303
359
  const parsed = path4.parse(destination);
304
- const outfile = path4.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".prerender-config.json");
305
- await fs3.mkdir(path4.join(getOutput(resolvedConfig, "functions"), parsed.dir), { recursive: true });
306
- await fs3.writeFile(outfile, JSON.stringify(vercelOutputPrerenderConfigSchema.parse({
307
- group: group++,
308
- ...isr
309
- }), void 0, 2), "utf-8");
360
+ const outfile = path4.join(
361
+ getOutput(resolvedConfig, "functions"),
362
+ parsed.dir,
363
+ parsed.name + ".prerender-config.json"
364
+ );
365
+ await fs3.mkdir(
366
+ path4.join(getOutput(resolvedConfig, "functions"), parsed.dir),
367
+ { recursive: true }
368
+ );
369
+ await fs3.writeFile(
370
+ outfile,
371
+ JSON.stringify(
372
+ vercelOutputPrerenderConfigSchema.parse({
373
+ group: group++,
374
+ ...isr
375
+ }),
376
+ void 0,
377
+ 2
378
+ ),
379
+ "utf-8"
380
+ );
310
381
  }
311
382
  function getPrerenderSymlinkInfo(resolvedConfig, destination, target) {
312
383
  const parsed = path4.parse(destination);
313
384
  const targetParsed = path4.parse(target);
314
385
  return {
315
- target: path4.join(getOutput(resolvedConfig, "functions"), targetParsed.dir, targetParsed.name + ".func"),
316
- link: path4.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".func")
386
+ target: path4.join(
387
+ getOutput(resolvedConfig, "functions"),
388
+ targetParsed.dir,
389
+ targetParsed.name + ".func"
390
+ ),
391
+ link: path4.join(
392
+ getOutput(resolvedConfig, "functions"),
393
+ parsed.dir,
394
+ parsed.name + ".func"
395
+ )
317
396
  };
318
397
  }
319
398
  async function buildPrerenderConfigs(resolvedConfig) {
@@ -323,7 +402,11 @@ async function buildPrerenderConfigs(resolvedConfig) {
323
402
  for (const [destination, { symlink, route, ...isr2 }] of entries) {
324
403
  await writePrerenderConfig(resolvedConfig, destination, isr2);
325
404
  if (symlink) {
326
- const info = getPrerenderSymlinkInfo(resolvedConfig, destination, symlink);
405
+ const info = getPrerenderSymlinkInfo(
406
+ resolvedConfig,
407
+ destination,
408
+ symlink
409
+ );
327
410
  await copyDir(info.target, info.link);
328
411
  }
329
412
  if (route) {
@@ -366,7 +449,9 @@ function vercelPlugin() {
366
449
  },
367
450
  async buildStart() {
368
451
  if (process.env.VERCEL_ENV === "production" && !process.env.ENABLE_VC_BUILD) {
369
- throw new Error("Missing ENABLE_VC_BUILD=1 to your environment variables in your project settings");
452
+ throw new Error(
453
+ "Missing ENABLE_VC_BUILD=1 to your environment variables in your project settings"
454
+ );
370
455
  }
371
456
  },
372
457
  async writeBundle() {
@@ -397,7 +482,9 @@ async function computeStaticHtmlOverrides(resolvedConfig) {
397
482
  const files = await getStaticHtmlFiles(staticAbsolutePath);
398
483
  const publicDir = getPublic(resolvedConfig);
399
484
  const publicFiles = await getStaticHtmlFiles(publicDir);
400
- files.push(...publicFiles.map((f) => f.replace(publicDir, staticAbsolutePath)));
485
+ files.push(
486
+ ...publicFiles.map((f) => f.replace(publicDir, staticAbsolutePath))
487
+ );
401
488
  return files.reduce((acc, curr) => {
402
489
  const relPath = path5.relative(staticAbsolutePath, curr);
403
490
  const parsed = path5.parse(relPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -23,20 +23,20 @@
23
23
  "vite": "^3.0.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@types/node": "^16.11.45",
27
- "@typescript-eslint/eslint-plugin": "^5.31.0",
28
- "@typescript-eslint/parser": "^5.31.0",
29
- "eslint": "^8.20.0",
30
- "tsup": "^6.2.0",
31
- "typescript": "^4.7.4",
32
- "vite": "^3.0.3"
26
+ "@types/node": "^16.18.11",
27
+ "@typescript-eslint/eslint-plugin": "^5.48.1",
28
+ "@typescript-eslint/parser": "^5.48.1",
29
+ "eslint": "^8.31.0",
30
+ "tsup": "^6.5.0",
31
+ "typescript": "^4.9.4",
32
+ "vite": "^3.2.5"
33
33
  },
34
34
  "dependencies": {
35
35
  "@brillout/libassert": "^0.5.8",
36
- "@vercel/routing-utils": "^1.13.5",
37
- "esbuild": "^0.14.50",
38
- "fast-glob": "^3.2.11",
39
- "zod": "^3.17.10"
36
+ "@vercel/routing-utils": "^2.1.3",
37
+ "esbuild": "^0.16.16",
38
+ "fast-glob": "^3.2.12",
39
+ "zod": "^3.20.2"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsup",