vite-plugin-vercel 0.0.3 → 0.0.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/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "0.0.3",
4
- "module": "./dist/index.mjs",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
5
  "files": [
6
6
  "dist",
7
7
  "*.d.ts"
8
8
  ],
9
- "main": "./dist/index.js",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
10
11
  "exports": {
11
12
  ".": {
12
- "import": "./dist/index.mjs",
13
- "require": "./dist/index.js"
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
14
15
  }
15
16
  },
16
17
  "types": "./index.d.ts",
@@ -19,28 +20,28 @@
19
20
  "repository": "https://github.com/magne4000/vite-plugin-vercel",
20
21
  "license": "MIT",
21
22
  "peerDependencies": {
22
- "vite": "^2.8.1"
23
+ "vite": "^2.9.9"
23
24
  },
24
25
  "devDependencies": {
25
- "@types/node": "^17.0.21",
26
- "@typescript-eslint/eslint-plugin": "^5.14.0",
27
- "@typescript-eslint/parser": "^5.14.0",
28
- "eslint": "^8.10.0",
29
- "eslint-plugin-solid": "^0.4.4",
30
- "tsup": "^5.12.0",
31
- "typescript": "^4.6.2"
26
+ "@types/node": "^17.0.34",
27
+ "@typescript-eslint/eslint-plugin": "^5.24.0",
28
+ "@typescript-eslint/parser": "^5.24.0",
29
+ "eslint": "^8.15.0",
30
+ "eslint-plugin-solid": "^0.4.7",
31
+ "tsup": "^5.12.8",
32
+ "typescript": "^4.6.4"
32
33
  },
33
34
  "dependencies": {
34
- "@brillout/libassert": "^0.5.4",
35
- "esbuild": "^0.14.25",
35
+ "@brillout/libassert": "^0.5.6",
36
+ "@vercel/routing-utils": "^1.13.2",
37
+ "esbuild": "^0.14.39",
36
38
  "fast-glob": "^3.2.11",
37
- "zod": "^3.14.2"
39
+ "zod": "^3.16.0"
38
40
  },
39
41
  "scripts": {
40
42
  "build": "tsup",
41
43
  "dev": "tsup --watch",
42
44
  "typecheck": "tsc -p tsconfig.json --noEmit",
43
45
  "lint:ts": "eslint . --max-warnings 0 --ignore-pattern dist"
44
- },
45
- "readme": "# vite-plugin-vercel\n\nThis is a wip Vercel adapter for [`vite`](https://vitejs.dev/).\n\n## Usage\n\nInstall as a dev dependency and add it to your Vite config like this:\n\n```ts\nimport { defineConfig } from 'vite';\nimport vercel from 'vite-plugin-vercel';\nimport ssr from 'vite-plugin-ssr';\n\nexport default defineConfig({\n plugins: [\n ssr(),\n vercel({\n dynamicRoutes: [\n {\n ssr: true,\n page: '/ssr',\n regex: '/((?!assets/)(?!api/).*)',\n },\n ],\n }),\n ],\n});\n```\n"
46
+ }
46
47
  }
package/dist/index.mjs DELETED
@@ -1,386 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
-
18
- // src/index.ts
19
- import fs3 from "fs/promises";
20
-
21
- // src/utils.ts
22
- import { normalizePath } from "vite";
23
- import path from "path";
24
- import fs from "fs/promises";
25
- function getRoot(config) {
26
- return normalizePath(config.root || process.cwd());
27
- }
28
- function getOutDir(config, force) {
29
- const p = normalizePath(config.build.outDir);
30
- if (!force)
31
- return p;
32
- return path.join(path.dirname(p), force);
33
- }
34
- function getOutput(config, suffix) {
35
- var _a, _b;
36
- return path.join(((_a = config.vercel) == null ? void 0 : _a.outDir) ? "" : getRoot(config), ((_b = config.vercel) == null ? void 0 : _b.outDir) ?? ".output", suffix ?? "");
37
- }
38
- function pathRelativeToApi(filePath, config) {
39
- const root = getRoot(config);
40
- return normalizePath(path.relative(path.join(root, "api"), filePath));
41
- }
42
- async function copyDir(src, dest) {
43
- await fs.mkdir(dest, { recursive: true });
44
- const entries = await fs.readdir(src, { withFileTypes: true });
45
- for (const entry of entries) {
46
- const srcPath = path.join(src, entry.name);
47
- const destPath = path.join(dest, entry.name);
48
- entry.isDirectory() ? await copyDir(srcPath, destPath) : await fs.copyFile(srcPath, destPath);
49
- }
50
- }
51
-
52
- // src/manifests.ts
53
- import path2 from "path";
54
-
55
- // src/assert.ts
56
- import { newError } from "@brillout/libassert";
57
- var libName = "vite-plugin-vercel";
58
- function assert(condition, errorMessage) {
59
- if (condition) {
60
- return;
61
- }
62
- const err = newError(`[${libName}][Wrong Usage] ${errorMessage}`, 2);
63
- throw err;
64
- }
65
-
66
- // src/schemas/manifests/routes.ts
67
- import { z } from "zod";
68
- var routesManifestRedirectSchema = z.object({
69
- source: z.string(),
70
- destination: z.string(),
71
- statusCode: z.number().refine((n) => [301, 302, 307, 308].includes(n)),
72
- regex: z.string()
73
- }).strict();
74
- var routesManifestHeaderSchema = z.object({
75
- source: z.string(),
76
- headers: z.array(z.object({
77
- key: z.string(),
78
- value: z.string()
79
- }).strict()),
80
- regex: z.string()
81
- }).strict();
82
- var routesManifestRewriteSchema = z.object({
83
- source: z.string(),
84
- has: z.array(z.object({
85
- key: z.string(),
86
- value: z.string(),
87
- type: z.enum(["header", "cookie", "host", "query"])
88
- }).strict()).optional(),
89
- destination: z.string(),
90
- regex: z.string()
91
- }).strict();
92
- var routesManifestDynamicRouteSchema = z.object({
93
- page: z.string(),
94
- regex: z.string(),
95
- routeKeys: z.record(z.string()).optional(),
96
- namedRegex: z.string().optional()
97
- }).strict();
98
- var routesManifestSchema = z.object({
99
- version: z.literal(3),
100
- basePath: z.string().regex(/^\/.*/),
101
- pages404: z.boolean(),
102
- redirects: z.array(routesManifestRedirectSchema).optional(),
103
- headers: z.array(routesManifestHeaderSchema).optional(),
104
- rewrites: z.array(routesManifestRewriteSchema).optional(),
105
- dynamicRoutes: z.array(routesManifestDynamicRouteSchema).optional()
106
- }).strict();
107
-
108
- // src/schemas/manifests/functions.ts
109
- import { z as z2 } from "zod";
110
- var functionsManifestSchemaPage = z2.object({
111
- runtime: z2.string().optional(),
112
- handler: z2.string().optional(),
113
- regions: z2.array(z2.string()).optional(),
114
- maxDuration: z2.number().min(1).max(900).optional(),
115
- memory: z2.number().min(128).max(3008).optional()
116
- }).strict();
117
- var functionsManifestSchemaPageWeb = functionsManifestSchemaPage.omit({
118
- runtime: true
119
- }).merge(z2.object({
120
- runtime: z2.literal("web"),
121
- env: z2.array(z2.string()),
122
- files: z2.array(z2.string()),
123
- name: z2.string(),
124
- page: z2.string(),
125
- regexp: z2.string(),
126
- sortingIndex: z2.number()
127
- }).strict());
128
- var functionsManifestSchema = z2.object({
129
- version: z2.literal(1),
130
- pages: z2.object({
131
- "_middleware.js": functionsManifestSchemaPageWeb.optional()
132
- }).and(z2.record(z2.intersection(functionsManifestSchemaPageWeb.omit({
133
- runtime: true
134
- }).partial(), functionsManifestSchemaPage)))
135
- });
136
-
137
- // src/schemas/manifests/prerender.ts
138
- import { z as z3 } from "zod";
139
- var prerenderManifestSchemaRoute = z3.object({
140
- initialRevalidateSeconds: z3.number(),
141
- srcRoute: z3.string(),
142
- dataRoute: z3.string()
143
- }).strict();
144
- var prerenderManifestSchemaDynamicRoute = z3.object({
145
- routeRegex: z3.string(),
146
- fallback: z3.string().or(z3.null()),
147
- dataRoute: z3.string(),
148
- dataRouteRegex: z3.string()
149
- }).strict();
150
- var prerenderManifestSchema = z3.object({
151
- version: z3.literal(3),
152
- routes: z3.record(prerenderManifestSchemaRoute),
153
- dynamicRoutes: z3.record(prerenderManifestSchemaDynamicRoute),
154
- preview: z3.object({
155
- previewModeId: z3.string().or(z3.null())
156
- }).strict()
157
- }).strict();
158
-
159
- // src/manifests.ts
160
- function getPrerenderManifest(resolvedConfig, isrPages) {
161
- var _a, _b;
162
- const prerenderManifestDefault = (_a = resolvedConfig.vercel) == null ? void 0 : _a.prerenderManifest;
163
- const routes = Object.entries((isrPages == null ? void 0 : isrPages.routes) ?? {}).reduce((acc, [key, val]) => {
164
- var _a2, _b2, _c, _d, _e, _f, _g;
165
- const srcRoute = (val == null ? void 0 : val.srcRoute) ?? ((_b2 = (_a2 = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.routes) == null ? void 0 : _a2[key]) == null ? void 0 : _b2.srcRoute);
166
- assert(typeof srcRoute === "string", `\`[prerender-manifest] { srcRoute }\` is required for route ${key}`);
167
- acc[key === "/" ? "/index" : key] = {
168
- initialRevalidateSeconds: (val == null ? void 0 : val.initialRevalidateSeconds) ?? ((_d = (_c = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.routes) == null ? void 0 : _c[key]) == null ? void 0 : _d.initialRevalidateSeconds) ?? ((_e = resolvedConfig.vercel) == null ? void 0 : _e.initialRevalidateSeconds) ?? 86400,
169
- srcRoute,
170
- dataRoute: (val == null ? void 0 : val.dataRoute) ?? ((_g = (_f = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.routes) == null ? void 0 : _f[key]) == null ? void 0 : _g.dataRoute) ?? ""
171
- };
172
- return acc;
173
- }, {});
174
- const dynamicRoutes = Object.entries((isrPages == null ? void 0 : isrPages.dynamicRoutes) ?? {}).reduce((acc, [key, val]) => {
175
- var _a2;
176
- const override = (_a2 = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.dynamicRoutes) == null ? void 0 : _a2[key];
177
- const routeRegex = (val == null ? void 0 : val.routeRegex) ?? (override == null ? void 0 : override.routeRegex);
178
- const fallback = (val == null ? void 0 : val.fallback) === null || typeof (val == null ? void 0 : val.fallback) === "string" ? val == null ? void 0 : val.fallback : (override == null ? void 0 : override.fallback) === null || typeof (override == null ? void 0 : override.fallback) === "string" ? override == null ? void 0 : override.fallback : null;
179
- assert(routeRegex, `\`[prerender-manifest] { routeRegex }\` is required for route ${key}`);
180
- acc[key] = {
181
- routeRegex,
182
- fallback,
183
- dataRoute: (val == null ? void 0 : val.dataRoute) ?? (override == null ? void 0 : override.dataRoute) ?? "",
184
- dataRouteRegex: (val == null ? void 0 : val.dataRouteRegex) ?? (override == null ? void 0 : override.dataRouteRegex) ?? ""
185
- };
186
- return acc;
187
- }, {});
188
- return prerenderManifestSchema.parse({
189
- version: 3,
190
- routes,
191
- dynamicRoutes,
192
- preview: {
193
- previewModeId: ((_b = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.preview) == null ? void 0 : _b.previewModeId) ?? null
194
- }
195
- });
196
- }
197
- function getPrerenderManifestDestination(resolvedConfig) {
198
- return path2.join(getOutput(resolvedConfig), "prerender-manifest.json");
199
- }
200
- function getRoutesManifest(resolvedConfig, ssr) {
201
- var _a;
202
- const routesManifest = (_a = resolvedConfig.vercel) == null ? void 0 : _a.routesManifest;
203
- const allRewrites = [
204
- ...(routesManifest == null ? void 0 : routesManifest.rewrites) ?? [],
205
- ...(ssr == null ? void 0 : ssr.rewrites) ?? []
206
- ];
207
- const allDynamicRoutes = [
208
- ...(routesManifest == null ? void 0 : routesManifest.dynamicRoutes) ?? [],
209
- ...(ssr == null ? void 0 : ssr.dynamicRoutes) ?? []
210
- ];
211
- const allHeaders = [
212
- ...(routesManifest == null ? void 0 : routesManifest.headers) ?? [],
213
- ...(ssr == null ? void 0 : ssr.headers) ?? []
214
- ];
215
- return routesManifestSchema.parse({
216
- version: 3,
217
- basePath: (routesManifest == null ? void 0 : routesManifest.basePath) ?? "/",
218
- pages404: (routesManifest == null ? void 0 : routesManifest.pages404) ?? true,
219
- dynamicRoutes: allDynamicRoutes.length > 0 ? allDynamicRoutes : void 0,
220
- rewrites: allRewrites.length > 0 ? allRewrites : void 0,
221
- redirects: routesManifest == null ? void 0 : routesManifest.redirects,
222
- headers: allHeaders.length > 0 ? allHeaders : void 0
223
- });
224
- }
225
- function getRoutesManifestDestination(resolvedConfig) {
226
- return path2.join(getOutput(resolvedConfig), "routes-manifest.json");
227
- }
228
- function getFunctionsManifest(pages) {
229
- return functionsManifestSchema.parse({
230
- version: 1,
231
- pages
232
- });
233
- }
234
- function getFunctionsManifestDestination(resolvedConfig) {
235
- return path2.join(getOutput(resolvedConfig), "functions-manifest.json");
236
- }
237
-
238
- // src/build.ts
239
- import glob from "fast-glob";
240
- import path3 from "path";
241
- import { build } from "esbuild";
242
- import fs2 from "fs/promises";
243
- function getPagesEndpoints(resolvedConfig) {
244
- var _a;
245
- const apiEndpoints = (((_a = resolvedConfig.vercel) == null ? void 0 : _a.pagesEndpoints) ?? []).map((p) => path3.isAbsolute(p) ? p : path3.resolve(getRoot(resolvedConfig), p));
246
- return new Set(apiEndpoints);
247
- }
248
- function getApiEntries(resolvedConfig) {
249
- var _a;
250
- const pagesEndpoints = getPagesEndpoints(resolvedConfig);
251
- const apiEntries = glob.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
252
- return apiEntries.reduce((entryPoints, filePath) => {
253
- const outFilePath = pathRelativeToApi(filePath, resolvedConfig);
254
- const parsed = path3.parse(outFilePath);
255
- const alsoPage = pagesEndpoints.has(filePath);
256
- const entry = {
257
- source: filePath,
258
- destination: [`api/${path3.join(parsed.dir, parsed.name)}`]
259
- };
260
- if (alsoPage) {
261
- entry.destination.push(`${path3.join(parsed.dir, parsed.name)}`);
262
- }
263
- entryPoints.push(entry);
264
- return entryPoints;
265
- }, ((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []);
266
- }
267
- var standardBuildOptions = {
268
- bundle: true,
269
- target: "es2020",
270
- format: "cjs",
271
- platform: "node",
272
- logLevel: "info",
273
- minify: true
274
- };
275
- async function buildFn(resolvedConfig, entry, buildOptions) {
276
- var _a, _b, _c, _d;
277
- if (!Array.isArray(entry.destination)) {
278
- entry.destination = [entry.destination];
279
- }
280
- assert(entry.destination.length > 0, `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`);
281
- const [firstDestination, ...remainingDestinations] = entry.destination;
282
- const pages = ((_b = (_a = resolvedConfig.vercel) == null ? void 0 : _a.functionsManifest) == null ? void 0 : _b.pages) ?? {};
283
- const fnManifests = {};
284
- const outfile = path3.join(getOutput(resolvedConfig, "server/pages"), firstDestination + ".js");
285
- const options = Object.assign({}, standardBuildOptions, { outfile });
286
- if (buildOptions) {
287
- Object.assign(options, buildOptions);
288
- }
289
- if (!options.stdin) {
290
- if (typeof entry.source === "string") {
291
- options.entryPoints = [entry.source];
292
- } else {
293
- assert(typeof entry.source === "object", `\`{ source }\` must be a string or an object`);
294
- assert(typeof entry.source.contents === "string", `\`{ contents }\` must be a string`);
295
- options.stdin = entry.source;
296
- }
297
- }
298
- await build(options);
299
- fnManifests[firstDestination + ".js"] = __spreadValues({
300
- maxDuration: (_c = resolvedConfig.vercel) == null ? void 0 : _c.defaultMaxDuration
301
- }, pages[firstDestination + ".js"]);
302
- for (const dest of remainingDestinations) {
303
- await fs2.mkdir(path3.join(getOutput(resolvedConfig, "server/pages"), path3.dirname(dest)), {
304
- recursive: true
305
- });
306
- await fs2.copyFile(outfile, path3.join(getOutput(resolvedConfig, "server/pages"), dest + ".js"));
307
- fnManifests[dest + ".js"] = __spreadValues({
308
- maxDuration: (_d = resolvedConfig.vercel) == null ? void 0 : _d.defaultMaxDuration
309
- }, pages[dest + ".js"] ?? pages[firstDestination + ".js"]);
310
- }
311
- return fnManifests;
312
- }
313
- async function buildApiEndpoints(resolvedConfig) {
314
- const entries = getApiEntries(resolvedConfig);
315
- const fnManifests = {};
316
- for (const entry of entries) {
317
- Object.assign(fnManifests, await buildFn(resolvedConfig, entry));
318
- }
319
- return fnManifests;
320
- }
321
-
322
- // src/prerender.ts
323
- function execPrerender(resolvedConfig) {
324
- var _a;
325
- const prerender = (_a = resolvedConfig.vercel) == null ? void 0 : _a.prerender;
326
- if (prerender === false) {
327
- return;
328
- }
329
- return prerender == null ? void 0 : prerender(resolvedConfig);
330
- }
331
-
332
- // src/index.ts
333
- function vercelPlugin() {
334
- let resolvedConfig;
335
- return {
336
- apply: "build",
337
- name: "vite-plugin-vercel",
338
- configResolved(config) {
339
- resolvedConfig = config;
340
- },
341
- async buildStart() {
342
- if (process.env.VERCEL_ENV === "production" && !process.env.ENABLE_FILE_SYSTEM_API) {
343
- throw new Error("Missing ENABLE_FILE_SYSTEM_API=1 to your environment variables in your project settings");
344
- }
345
- if (resolvedConfig.build.ssr)
346
- return;
347
- await cleanOutputDirectory(resolvedConfig);
348
- },
349
- async writeBundle() {
350
- var _a;
351
- if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr)) {
352
- await copyDistClientToOutputStatic(resolvedConfig);
353
- return;
354
- }
355
- const isrPages = await execPrerender(resolvedConfig);
356
- const fnManifests = await buildApiEndpoints(resolvedConfig);
357
- await generateFunctionsManifest(resolvedConfig, fnManifests);
358
- await generateRoutesManifest(resolvedConfig, isrPages == null ? void 0 : isrPages.ssr);
359
- await generatePrerenderManifest(resolvedConfig, isrPages == null ? void 0 : isrPages.isr);
360
- }
361
- };
362
- }
363
- async function copyDistClientToOutputStatic(resolvedConfig) {
364
- await copyDir(getOutDir(resolvedConfig), getOutput(resolvedConfig, "static"));
365
- }
366
- async function cleanOutputDirectory(resolvedConfig) {
367
- await fs3.rm(getOutput(resolvedConfig), {
368
- recursive: true,
369
- force: true
370
- });
371
- }
372
- async function generatePrerenderManifest(resolvedConfig, isrPages) {
373
- await fs3.writeFile(getPrerenderManifestDestination(resolvedConfig), JSON.stringify(getPrerenderManifest(resolvedConfig, isrPages), void 0, 2));
374
- }
375
- async function generateRoutesManifest(resolvedConfig, ssr) {
376
- await fs3.writeFile(getRoutesManifestDestination(resolvedConfig), JSON.stringify(getRoutesManifest(resolvedConfig, ssr), void 0, 2));
377
- }
378
- async function generateFunctionsManifest(resolvedConfig, fnManifests) {
379
- await fs3.writeFile(getFunctionsManifestDestination(resolvedConfig), JSON.stringify(getFunctionsManifest(fnManifests), void 0, 2));
380
- }
381
- function allPlugins() {
382
- return [vercelPlugin()];
383
- }
384
- export {
385
- allPlugins as default
386
- };