vite-plugin-vercel 0.0.2 → 0.0.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/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "0.0.2",
4
- "module": "./dist/index.mjs",
3
+ "version": "0.0.5",
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
- "myzod": "^1.8.7"
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,393 +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 * as myzod from "myzod";
68
- function record(schema) {
69
- return myzod.object({
70
- [myzod.keySignature]: schema
71
- });
72
- }
73
- var routesManifestRedirectSchema = myzod.object({
74
- source: myzod.string(),
75
- destination: myzod.string(),
76
- statusCode: myzod.literals(301, 302, 307, 308),
77
- regex: myzod.string()
78
- });
79
- var routesManifestHeaderSchema = myzod.object({
80
- source: myzod.string(),
81
- headers: myzod.array(myzod.object({
82
- key: myzod.string(),
83
- value: myzod.string()
84
- })),
85
- regex: myzod.string()
86
- });
87
- var routesManifestRewriteSchema = myzod.object({
88
- source: myzod.string(),
89
- has: myzod.array(myzod.object({
90
- key: myzod.string(),
91
- value: myzod.string(),
92
- type: myzod.literals("header", "cookie", "host", "query")
93
- })).optional(),
94
- destination: myzod.string(),
95
- regex: myzod.string()
96
- });
97
- var routesManifestDynamicRouteSchema = myzod.object({
98
- page: myzod.string(),
99
- regex: myzod.string(),
100
- routeKeys: record(myzod.string()).optional(),
101
- namedRegex: myzod.string().optional()
102
- });
103
- var routesManifestSchema = myzod.object({
104
- version: myzod.literal(3),
105
- basePath: myzod.string().pattern(/^\/.*/),
106
- pages404: myzod.boolean(),
107
- redirects: myzod.array(routesManifestRedirectSchema).optional(),
108
- headers: myzod.array(routesManifestHeaderSchema).optional(),
109
- rewrites: myzod.array(routesManifestRewriteSchema).optional(),
110
- dynamicRoutes: myzod.array(routesManifestDynamicRouteSchema).optional()
111
- });
112
-
113
- // src/schemas/manifests/functions.ts
114
- import * as myzod2 from "myzod";
115
- function record2(schema) {
116
- return myzod2.object({
117
- [myzod2.keySignature]: schema
118
- });
119
- }
120
- var functionsManifestSchemaPage = myzod2.object({
121
- runtime: myzod2.string().optional(),
122
- handler: myzod2.string().optional(),
123
- regions: myzod2.array(myzod2.string()).optional(),
124
- maxDuration: myzod2.number().min(1).max(900).optional(),
125
- memory: myzod2.number().min(128).max(3008).optional()
126
- });
127
- var functionsManifestSchemaPageWeb = myzod2.omit(functionsManifestSchemaPage, ["runtime"]).and(myzod2.object({
128
- runtime: myzod2.literal("web"),
129
- env: myzod2.array(myzod2.string()),
130
- files: myzod2.array(myzod2.string()),
131
- name: myzod2.string(),
132
- page: myzod2.string(),
133
- regexp: myzod2.string(),
134
- sortingIndex: myzod2.number()
135
- }));
136
- var functionsManifestSchema = myzod2.object({
137
- version: myzod2.literal(1),
138
- pages: myzod2.object({
139
- "_middleware.js": functionsManifestSchemaPageWeb.optional()
140
- }).and(record2(myzod2.intersection(myzod2.partial(myzod2.omit(functionsManifestSchemaPageWeb, ["runtime"])), functionsManifestSchemaPage)))
141
- });
142
-
143
- // src/schemas/manifests/prerender.ts
144
- import * as myzod3 from "myzod";
145
- function record3(schema) {
146
- return myzod3.object({
147
- [myzod3.keySignature]: schema
148
- });
149
- }
150
- var prerenderManifestSchemaRoute = myzod3.object({
151
- initialRevalidateSeconds: myzod3.number(),
152
- srcRoute: myzod3.string(),
153
- dataRoute: myzod3.string()
154
- });
155
- var prerenderManifestSchemaDynamicRoute = myzod3.object({
156
- routeRegex: myzod3.string(),
157
- fallback: myzod3.string().or(myzod3.null()),
158
- dataRoute: myzod3.string(),
159
- dataRouteRegex: myzod3.string()
160
- });
161
- var prerenderManifestSchema = myzod3.object({
162
- version: myzod3.literal(3),
163
- routes: record3(prerenderManifestSchemaRoute),
164
- dynamicRoutes: record3(prerenderManifestSchemaDynamicRoute),
165
- preview: myzod3.object({
166
- previewModeId: myzod3.string().or(myzod3.null())
167
- })
168
- });
169
-
170
- // src/manifests.ts
171
- function getPrerenderManifest(resolvedConfig, isrPages) {
172
- var _a, _b;
173
- const prerenderManifestDefault = (_a = resolvedConfig.vercel) == null ? void 0 : _a.prerenderManifest;
174
- const routes = Object.entries((isrPages == null ? void 0 : isrPages.routes) ?? {}).reduce((acc, [key, val]) => {
175
- var _a2, _b2, _c, _d, _e, _f, _g;
176
- 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);
177
- assert(typeof srcRoute === "string", `\`[prerender-manifest] { srcRoute }\` is required for route ${key}`);
178
- acc[key === "/" ? "/index" : key] = {
179
- 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,
180
- srcRoute,
181
- 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) ?? ""
182
- };
183
- return acc;
184
- }, {});
185
- const uniqueRoutes = Array.from(new Set(Object.values(routes).map((r) => r.srcRoute)));
186
- const dynamicRoutes = uniqueRoutes.reduce((acc, cur) => {
187
- var _a2, _b2, _c, _d, _e, _f;
188
- acc[cur] = {
189
- routeRegex: "^" + cur + "$",
190
- dataRoute: ((_b2 = (_a2 = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.dynamicRoutes) == null ? void 0 : _a2[cur]) == null ? void 0 : _b2.dataRoute) ?? "",
191
- fallback: ((_d = (_c = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.dynamicRoutes) == null ? void 0 : _c[cur]) == null ? void 0 : _d.fallback) ?? null,
192
- dataRouteRegex: ((_f = (_e = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.dynamicRoutes) == null ? void 0 : _e[cur]) == null ? void 0 : _f.dataRouteRegex) ?? ""
193
- };
194
- return acc;
195
- }, {});
196
- return prerenderManifestSchema.parse({
197
- version: 3,
198
- routes,
199
- dynamicRoutes,
200
- preview: {
201
- previewModeId: ((_b = prerenderManifestDefault == null ? void 0 : prerenderManifestDefault.preview) == null ? void 0 : _b.previewModeId) ?? null
202
- }
203
- }, {
204
- collectErrors: true
205
- });
206
- }
207
- function getPrerenderManifestDestination(resolvedConfig) {
208
- return path2.join(getOutput(resolvedConfig), "prerender-manifest.json");
209
- }
210
- function getRoutesManifest(resolvedConfig, ssr) {
211
- var _a;
212
- const routesManifest = (_a = resolvedConfig.vercel) == null ? void 0 : _a.routesManifest;
213
- const allRewrites = [
214
- ...(ssr == null ? void 0 : ssr.rewrites) ?? [],
215
- ...(routesManifest == null ? void 0 : routesManifest.rewrites) ?? []
216
- ];
217
- const allDynamicRoutes = [
218
- ...(ssr == null ? void 0 : ssr.dynamicRoutes) ?? [],
219
- ...(routesManifest == null ? void 0 : routesManifest.dynamicRoutes) ?? []
220
- ];
221
- return routesManifestSchema.parse({
222
- version: 3,
223
- basePath: (routesManifest == null ? void 0 : routesManifest.basePath) ?? "/",
224
- pages404: (routesManifest == null ? void 0 : routesManifest.pages404) ?? true,
225
- dynamicRoutes: allDynamicRoutes.length > 0 ? allDynamicRoutes : void 0,
226
- rewrites: allRewrites.length > 0 ? allRewrites : void 0,
227
- redirects: routesManifest == null ? void 0 : routesManifest.redirects,
228
- headers: routesManifest == null ? void 0 : routesManifest.headers
229
- }, {
230
- collectErrors: true
231
- });
232
- }
233
- function getRoutesManifestDestination(resolvedConfig) {
234
- return path2.join(getOutput(resolvedConfig), "routes-manifest.json");
235
- }
236
- function getFunctionsManifest(pages) {
237
- return functionsManifestSchema.parse({
238
- version: 1,
239
- pages
240
- }, {
241
- collectErrors: true
242
- });
243
- }
244
- function getFunctionsManifestDestination(resolvedConfig) {
245
- return path2.join(getOutput(resolvedConfig), "functions-manifest.json");
246
- }
247
-
248
- // src/build.ts
249
- import glob from "fast-glob";
250
- import path3 from "path";
251
- import { build } from "esbuild";
252
- import fs2 from "fs/promises";
253
- function getPagesEndpoints(resolvedConfig) {
254
- var _a;
255
- const apiEndpoints = (((_a = resolvedConfig.vercel) == null ? void 0 : _a.pagesEndpoints) ?? []).map((p) => path3.isAbsolute(p) ? p : path3.resolve(getRoot(resolvedConfig), p));
256
- return new Set(apiEndpoints);
257
- }
258
- function getApiEntries(resolvedConfig) {
259
- var _a;
260
- const pagesEndpoints = getPagesEndpoints(resolvedConfig);
261
- const apiEntries = glob.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !path3.basename(filepath).startsWith("_"));
262
- return apiEntries.reduce((entryPoints, filePath) => {
263
- const outFilePath = pathRelativeToApi(filePath, resolvedConfig);
264
- const parsed = path3.parse(outFilePath);
265
- const alsoPage = pagesEndpoints.has(filePath);
266
- const entry = {
267
- source: filePath,
268
- destination: [`api/${path3.join(parsed.dir, parsed.name)}`]
269
- };
270
- if (alsoPage) {
271
- entry.destination.push(`${path3.join(parsed.dir, parsed.name)}`);
272
- }
273
- entryPoints.push(entry);
274
- return entryPoints;
275
- }, ((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []);
276
- }
277
- var standardBuildOptions = {
278
- bundle: true,
279
- target: "es2020",
280
- format: "cjs",
281
- platform: "node",
282
- logLevel: "info",
283
- minify: true
284
- };
285
- async function buildFn(resolvedConfig, entry, buildOptions) {
286
- var _a, _b, _c, _d;
287
- if (!Array.isArray(entry.destination)) {
288
- entry.destination = [entry.destination];
289
- }
290
- assert(entry.destination.length > 0, `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`);
291
- const [firstDestination, ...remainingDestinations] = entry.destination;
292
- const pages = ((_b = (_a = resolvedConfig.vercel) == null ? void 0 : _a.functionsManifest) == null ? void 0 : _b.pages) ?? {};
293
- const fnManifests = {};
294
- const outfile = path3.join(getOutput(resolvedConfig, "server/pages"), firstDestination + ".js");
295
- const options = Object.assign({}, standardBuildOptions, { outfile });
296
- if (buildOptions) {
297
- Object.assign(options, buildOptions);
298
- }
299
- if (!options.stdin) {
300
- if (typeof entry.source === "string") {
301
- options.entryPoints = [entry.source];
302
- } else {
303
- assert(typeof entry.source === "object", `\`{ source }\` must be a string or an object`);
304
- assert(typeof entry.source.contents === "string", `\`{ contents }\` must be a string`);
305
- options.stdin = entry.source;
306
- }
307
- }
308
- await build(options);
309
- fnManifests[firstDestination + ".js"] = __spreadValues({
310
- maxDuration: (_c = resolvedConfig.vercel) == null ? void 0 : _c.defaultMaxDuration
311
- }, pages[firstDestination + ".js"]);
312
- for (const dest of remainingDestinations) {
313
- await fs2.mkdir(path3.join(getOutput(resolvedConfig, "server/pages"), path3.dirname(dest)), {
314
- recursive: true
315
- });
316
- await fs2.copyFile(outfile, path3.join(getOutput(resolvedConfig, "server/pages"), dest + ".js"));
317
- fnManifests[dest + ".js"] = __spreadValues({
318
- maxDuration: (_d = resolvedConfig.vercel) == null ? void 0 : _d.defaultMaxDuration
319
- }, pages[dest + ".js"] ?? pages[firstDestination + ".js"]);
320
- }
321
- return fnManifests;
322
- }
323
- async function buildApiEndpoints(resolvedConfig) {
324
- const entries = getApiEntries(resolvedConfig);
325
- const fnManifests = {};
326
- for (const entry of entries) {
327
- Object.assign(fnManifests, await buildFn(resolvedConfig, entry));
328
- }
329
- return fnManifests;
330
- }
331
-
332
- // src/prerender.ts
333
- function execPrerender(resolvedConfig) {
334
- var _a;
335
- const prerender = (_a = resolvedConfig.vercel) == null ? void 0 : _a.prerender;
336
- if (prerender === false) {
337
- return;
338
- }
339
- return prerender == null ? void 0 : prerender(resolvedConfig);
340
- }
341
-
342
- // src/index.ts
343
- function vercelPlugin() {
344
- let resolvedConfig;
345
- return {
346
- apply: "build",
347
- name: "vite-plugin-vercel",
348
- configResolved(config) {
349
- resolvedConfig = config;
350
- },
351
- async buildStart() {
352
- if (resolvedConfig.build.ssr)
353
- return;
354
- await cleanOutputDirectory(resolvedConfig);
355
- },
356
- async writeBundle() {
357
- var _a;
358
- if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr)) {
359
- await copyDistClientToOutputStatic(resolvedConfig);
360
- return;
361
- }
362
- const isrPages = await execPrerender(resolvedConfig);
363
- const fnManifests = await buildApiEndpoints(resolvedConfig);
364
- await generateFunctionsManifest(resolvedConfig, fnManifests);
365
- await generateRoutesManifest(resolvedConfig, isrPages == null ? void 0 : isrPages.ssr);
366
- await generatePrerenderManifest(resolvedConfig, isrPages == null ? void 0 : isrPages.isr);
367
- }
368
- };
369
- }
370
- async function copyDistClientToOutputStatic(resolvedConfig) {
371
- await copyDir(getOutDir(resolvedConfig), getOutput(resolvedConfig, "static"));
372
- }
373
- async function cleanOutputDirectory(resolvedConfig) {
374
- await fs3.rm(getOutput(resolvedConfig), {
375
- recursive: true,
376
- force: true
377
- });
378
- }
379
- async function generatePrerenderManifest(resolvedConfig, isrPages) {
380
- await fs3.writeFile(getPrerenderManifestDestination(resolvedConfig), JSON.stringify(getPrerenderManifest(resolvedConfig, isrPages), void 0, 2));
381
- }
382
- async function generateRoutesManifest(resolvedConfig, ssr) {
383
- await fs3.writeFile(getRoutesManifestDestination(resolvedConfig), JSON.stringify(getRoutesManifest(resolvedConfig, ssr), void 0, 2));
384
- }
385
- async function generateFunctionsManifest(resolvedConfig, fnManifests) {
386
- await fs3.writeFile(getFunctionsManifestDestination(resolvedConfig), JSON.stringify(getFunctionsManifest(fnManifests), void 0, 2));
387
- }
388
- function allPlugins() {
389
- return [vercelPlugin()];
390
- }
391
- export {
392
- allPlugins as default
393
- };