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/dist/index.cjs ADDED
@@ -0,0 +1,424 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __objRest = (source, exclude) => {
25
+ var target = {};
26
+ for (var prop in source)
27
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
28
+ target[prop] = source[prop];
29
+ if (source != null && __getOwnPropSymbols)
30
+ for (var prop of __getOwnPropSymbols(source)) {
31
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
32
+ target[prop] = source[prop];
33
+ }
34
+ return target;
35
+ };
36
+ var __export = (target, all) => {
37
+ for (var name in all)
38
+ __defProp(target, name, { get: all[name], enumerable: true });
39
+ };
40
+ var __copyProps = (to, from, except, desc) => {
41
+ if (from && typeof from === "object" || typeof from === "function") {
42
+ for (let key of __getOwnPropNames(from))
43
+ if (!__hasOwnProp.call(to, key) && key !== except)
44
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
45
+ }
46
+ return to;
47
+ };
48
+ 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));
49
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
50
+
51
+ // src/index.ts
52
+ var src_exports = {};
53
+ __export(src_exports, {
54
+ default: () => allPlugins
55
+ });
56
+ module.exports = __toCommonJS(src_exports);
57
+ var import_promises5 = __toESM(require("fs/promises"), 1);
58
+
59
+ // src/utils.ts
60
+ var import_vite = require("vite");
61
+ var import_path = __toESM(require("path"), 1);
62
+ var import_promises = __toESM(require("fs/promises"), 1);
63
+ function getRoot(config) {
64
+ return (0, import_vite.normalizePath)(config.root || process.cwd());
65
+ }
66
+ function getOutDir(config, force) {
67
+ const p = (0, import_vite.normalizePath)(config.build.outDir);
68
+ if (!force)
69
+ return p;
70
+ return import_path.default.join(import_path.default.dirname(p), force);
71
+ }
72
+ function getOutput(config, suffix) {
73
+ var _a, _b;
74
+ 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 ?? "");
75
+ }
76
+ function pathRelativeToApi(filePath, config) {
77
+ const root = getRoot(config);
78
+ return (0, import_vite.normalizePath)(import_path.default.relative(import_path.default.join(root, "api"), filePath));
79
+ }
80
+ async function copyDir(src, dest) {
81
+ await import_promises.default.mkdir(dest, { recursive: true });
82
+ const entries = await import_promises.default.readdir(src, { withFileTypes: true });
83
+ for (const entry of entries) {
84
+ const srcPath = import_path.default.join(src, entry.name);
85
+ const destPath = import_path.default.join(dest, entry.name);
86
+ entry.isDirectory() ? await copyDir(srcPath, destPath) : await import_promises.default.copyFile(srcPath, destPath);
87
+ }
88
+ }
89
+
90
+ // src/config.ts
91
+ var import_path2 = __toESM(require("path"), 1);
92
+
93
+ // src/schemas/config/config.ts
94
+ var import_zod = require("zod");
95
+ var HasOrMissing = import_zod.z.array(import_zod.z.union([
96
+ import_zod.z.object({
97
+ type: import_zod.z.literal("host"),
98
+ value: import_zod.z.string()
99
+ }).strict(),
100
+ import_zod.z.object({
101
+ type: import_zod.z.literal("header"),
102
+ key: import_zod.z.string(),
103
+ value: import_zod.z.string().optional()
104
+ }).strict(),
105
+ import_zod.z.object({
106
+ type: import_zod.z.literal("cookie"),
107
+ key: import_zod.z.string(),
108
+ value: import_zod.z.string().optional()
109
+ }).strict(),
110
+ import_zod.z.object({
111
+ type: import_zod.z.literal("query"),
112
+ key: import_zod.z.string(),
113
+ value: import_zod.z.string().optional()
114
+ }).strict()
115
+ ])).optional();
116
+ var vercelOutputConfigSchema = import_zod.z.object({
117
+ version: import_zod.z.literal(3),
118
+ routes: import_zod.z.array(import_zod.z.union([
119
+ import_zod.z.object({
120
+ src: import_zod.z.string(),
121
+ dest: import_zod.z.string().optional(),
122
+ headers: import_zod.z.record(import_zod.z.string()).optional(),
123
+ methods: import_zod.z.array(import_zod.z.string()).optional(),
124
+ status: import_zod.z.number().int().positive().optional(),
125
+ continue: import_zod.z.boolean().optional(),
126
+ check: import_zod.z.boolean().optional(),
127
+ missing: HasOrMissing,
128
+ has: HasOrMissing,
129
+ locale: import_zod.z.object({
130
+ redirect: import_zod.z.record(import_zod.z.string()).optional(),
131
+ cookie: import_zod.z.string().optional()
132
+ }).strict().optional(),
133
+ middlewarePath: import_zod.z.string().optional()
134
+ }).strict(),
135
+ import_zod.z.object({
136
+ handle: import_zod.z.union([
137
+ import_zod.z.literal("rewrite"),
138
+ import_zod.z.literal("filesystem"),
139
+ import_zod.z.literal("resource"),
140
+ import_zod.z.literal("miss"),
141
+ import_zod.z.literal("hit"),
142
+ import_zod.z.literal("error")
143
+ ]),
144
+ src: import_zod.z.string().optional(),
145
+ dest: import_zod.z.string().optional(),
146
+ status: import_zod.z.number().optional()
147
+ }).strict()
148
+ ])).optional(),
149
+ images: import_zod.z.object({
150
+ sizes: import_zod.z.tuple([
151
+ import_zod.z.number().int().positive(),
152
+ import_zod.z.number().int().positive()
153
+ ]),
154
+ domains: import_zod.z.array(import_zod.z.string()).nonempty().optional(),
155
+ minimumCacheTTL: import_zod.z.number().int().positive().optional(),
156
+ formats: import_zod.z.union([import_zod.z.literal("image/avif"), import_zod.z.literal("image/webp")]).array().nonempty().optional(),
157
+ dangerouslyAllowSVG: import_zod.z.boolean().optional(),
158
+ contentSecurityPolicy: import_zod.z.string().optional()
159
+ }).strict().optional(),
160
+ wildcard: import_zod.z.array(import_zod.z.object({
161
+ domain: import_zod.z.string(),
162
+ value: import_zod.z.string()
163
+ }).strict()).optional(),
164
+ overrides: import_zod.z.record(import_zod.z.object({
165
+ path: import_zod.z.string().optional(),
166
+ contentType: import_zod.z.string().optional()
167
+ }).strict()).optional(),
168
+ cache: import_zod.z.array(import_zod.z.string()).optional()
169
+ }).strict();
170
+
171
+ // src/config.ts
172
+ var import_promises2 = __toESM(require("fs/promises"), 1);
173
+ var import_routing_utils = require("@vercel/routing-utils");
174
+ function getConfig(resolvedConfig, config) {
175
+ var _a, _b, _c, _d, _e;
176
+ const { routes } = (0, import_routing_utils.getTransformedRoutes)({
177
+ nowConfig: {
178
+ cleanUrls: true
179
+ }
180
+ });
181
+ return vercelOutputConfigSchema.parse(__spreadProps(__spreadValues({
182
+ version: 3
183
+ }, (_a = resolvedConfig.vercel) == null ? void 0 : _a.config), {
184
+ routes: [
185
+ ...routes ?? [],
186
+ ...(config == null ? void 0 : config.routes) ?? [],
187
+ ...((_c = (_b = resolvedConfig.vercel) == null ? void 0 : _b.config) == null ? void 0 : _c.routes) ?? []
188
+ ],
189
+ overrides: __spreadValues(__spreadValues({}, (_e = (_d = resolvedConfig.vercel) == null ? void 0 : _d.config) == null ? void 0 : _e.overrides), config == null ? void 0 : config.overrides)
190
+ }));
191
+ }
192
+ function getConfigDestination(resolvedConfig) {
193
+ return import_path2.default.join(getOutput(resolvedConfig), "config.json");
194
+ }
195
+ async function writeConfig(resolvedConfig, config) {
196
+ await import_promises2.default.writeFile(getConfigDestination(resolvedConfig), JSON.stringify(getConfig(resolvedConfig, config), void 0, 2), "utf-8");
197
+ }
198
+
199
+ // src/build.ts
200
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
201
+ var import_path3 = __toESM(require("path"), 1);
202
+ var import_esbuild = require("esbuild");
203
+
204
+ // src/assert.ts
205
+ var import_libassert = require("@brillout/libassert");
206
+ var libName = "vite-plugin-vercel";
207
+ function assert(condition, errorMessage) {
208
+ if (condition) {
209
+ return;
210
+ }
211
+ const err = (0, import_libassert.newError)(`[${libName}][Wrong Usage] ${errorMessage}`, 2);
212
+ throw err;
213
+ }
214
+
215
+ // src/schemas/config/vc-config.ts
216
+ var import_zod2 = require("zod");
217
+ var vercelOutputEdgeVcConfigSchema = import_zod2.z.object({
218
+ runtime: import_zod2.z.literal("edge"),
219
+ entrypoint: import_zod2.z.string(),
220
+ envVarsInUse: import_zod2.z.array(import_zod2.z.string()).optional()
221
+ }).strict();
222
+ var vercelOutputServerlessVcConfigSchema = import_zod2.z.object({
223
+ runtime: import_zod2.z.string(),
224
+ handler: import_zod2.z.string(),
225
+ memory: import_zod2.z.number().int().min(128).max(3008).optional(),
226
+ maxDuration: import_zod2.z.number().int().positive().optional(),
227
+ environment: import_zod2.z.record(import_zod2.z.string()).optional(),
228
+ regions: import_zod2.z.array(import_zod2.z.string()).optional()
229
+ }).strict();
230
+ var vercelOutputServerlessNodeVcConfigSchema = vercelOutputServerlessVcConfigSchema.extend({
231
+ launcherType: import_zod2.z.literal("Nodejs"),
232
+ shouldAddHelpers: import_zod2.z.boolean().optional(),
233
+ shouldAddSourcemapSupport: import_zod2.z.boolean().optional(),
234
+ awsLambdaHandler: import_zod2.z.string().optional()
235
+ }).strict();
236
+ var vercelOutputVcConfigSchema = import_zod2.z.union([
237
+ vercelOutputEdgeVcConfigSchema,
238
+ vercelOutputServerlessVcConfigSchema,
239
+ vercelOutputServerlessNodeVcConfigSchema
240
+ ]);
241
+
242
+ // src/build.ts
243
+ var import_promises3 = __toESM(require("fs/promises"), 1);
244
+ function getAdditionalEndpoints(resolvedConfig) {
245
+ var _a;
246
+ return (((_a = resolvedConfig.vercel) == null ? void 0 : _a.additionalEndpoints) ?? []).map((e) => __spreadProps(__spreadValues({}, e), {
247
+ destination: e.destination + ".func"
248
+ }));
249
+ }
250
+ function getEntries(resolvedConfig) {
251
+ const apiEntries = import_fast_glob.default.sync(`${getRoot(resolvedConfig)}/api/**/*.*([a-zA-Z0-9])`).filter((filepath) => !import_path3.default.basename(filepath).startsWith("_"));
252
+ return apiEntries.reduce((entryPoints, filePath) => {
253
+ const outFilePath = pathRelativeToApi(filePath, resolvedConfig);
254
+ const parsed = import_path3.default.parse(outFilePath);
255
+ const entry = {
256
+ source: filePath,
257
+ destination: `api/${import_path3.default.join(parsed.dir, parsed.name)}.func`
258
+ };
259
+ entryPoints.push(entry);
260
+ return entryPoints;
261
+ }, getAdditionalEndpoints(resolvedConfig));
262
+ }
263
+ var standardBuildOptions = {
264
+ bundle: true,
265
+ target: "es2020",
266
+ format: "cjs",
267
+ platform: "node",
268
+ logLevel: "info",
269
+ minify: true
270
+ };
271
+ async function buildFn(resolvedConfig, entry, buildOptions) {
272
+ assert(entry.destination.length > 0, `Endpoint ${typeof entry.source === "string" ? entry.source : "-"} does not have build destination`);
273
+ const outfile = import_path3.default.join(getOutput(resolvedConfig, "functions"), entry.destination, "index.js");
274
+ const options = Object.assign({}, standardBuildOptions, { outfile });
275
+ if (buildOptions) {
276
+ Object.assign(options, buildOptions);
277
+ }
278
+ if (!options.stdin) {
279
+ if (typeof entry.source === "string") {
280
+ options.entryPoints = [entry.source];
281
+ } else {
282
+ assert(typeof entry.source === "object", `\`{ source }\` must be a string or an object`);
283
+ assert(typeof entry.source.contents === "string", `\`{ contents }\` must be a string`);
284
+ options.stdin = entry.source;
285
+ }
286
+ }
287
+ await (0, import_esbuild.build)(options);
288
+ await writeVcConfig(resolvedConfig, entry.destination);
289
+ }
290
+ async function writeVcConfig(resolvedConfig, destination) {
291
+ var _a;
292
+ const vcConfig = import_path3.default.join(getOutput(resolvedConfig, "functions"), destination, ".vc-config.json");
293
+ await import_promises3.default.writeFile(vcConfig, JSON.stringify(vercelOutputVcConfigSchema.parse({
294
+ runtime: "nodejs16.x",
295
+ handler: "index.js",
296
+ maxDuration: (_a = resolvedConfig.vercel) == null ? void 0 : _a.defaultMaxDuration,
297
+ launcherType: "Nodejs",
298
+ shouldAddHelpers: true
299
+ }), void 0, 2), "utf-8");
300
+ }
301
+ async function buildEndpoints(resolvedConfig) {
302
+ const entries = getEntries(resolvedConfig);
303
+ for (const entry of entries) {
304
+ await buildFn(resolvedConfig, entry);
305
+ }
306
+ }
307
+
308
+ // src/prerender.ts
309
+ var import_path4 = __toESM(require("path"), 1);
310
+
311
+ // src/schemas/config/prerender-config.ts
312
+ var import_zod3 = require("zod");
313
+ var vercelOutputPrerenderConfigSchema = import_zod3.z.object({
314
+ expiration: import_zod3.z.union([import_zod3.z.number().int().positive(), import_zod3.z.literal(false)]),
315
+ group: import_zod3.z.number().int().optional(),
316
+ bypassToken: import_zod3.z.string().optional(),
317
+ fallback: import_zod3.z.string().optional(),
318
+ allowQuery: import_zod3.z.array(import_zod3.z.string()).optional()
319
+ }).strict();
320
+
321
+ // src/prerender.ts
322
+ var import_promises4 = __toESM(require("fs/promises"), 1);
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
+ var group = 1;
332
+ async function writePrerenderConfig(resolvedConfig, destination, isr) {
333
+ const parsed = import_path4.default.parse(destination);
334
+ const outfile = import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".prerender-config.json");
335
+ await import_promises4.default.mkdir(import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir), { recursive: true });
336
+ await import_promises4.default.writeFile(outfile, JSON.stringify(vercelOutputPrerenderConfigSchema.parse(__spreadValues({
337
+ group: group++
338
+ }, isr)), void 0, 2), "utf-8");
339
+ }
340
+ function getPrerenderSymlinkInfo(resolvedConfig, destination, target) {
341
+ const parsed = import_path4.default.parse(destination);
342
+ const targetParsed = import_path4.default.parse(target);
343
+ return {
344
+ target: import_path4.default.join(getOutput(resolvedConfig, "functions"), targetParsed.dir, targetParsed.name + ".func"),
345
+ link: import_path4.default.join(getOutput(resolvedConfig, "functions"), parsed.dir, parsed.name + ".func")
346
+ };
347
+ }
348
+ async function buildPrerenderConfigs(resolvedConfig) {
349
+ const isr = await getIsrConfig(resolvedConfig);
350
+ const entries = Object.entries(isr);
351
+ const rewrites = [];
352
+ for (const _a of entries) {
353
+ const [destination, _b] = _a, _c = _b, { symlink, route } = _c, isr2 = __objRest(_c, ["symlink", "route"]);
354
+ await writePrerenderConfig(resolvedConfig, destination, isr2);
355
+ if (symlink) {
356
+ const info = getPrerenderSymlinkInfo(resolvedConfig, destination, symlink);
357
+ await copyDir(info.target, info.link);
358
+ }
359
+ if (route) {
360
+ rewrites.push({
361
+ src: `(${route})`,
362
+ dest: `${destination}/?__original_path=$1`
363
+ });
364
+ }
365
+ }
366
+ return rewrites;
367
+ }
368
+ async function getIsrConfig(resolvedConfig) {
369
+ var _a;
370
+ const isr = ((_a = resolvedConfig.vercel) == null ? void 0 : _a.isr) ?? {};
371
+ if (typeof isr === "function") {
372
+ return await isr();
373
+ }
374
+ return isr;
375
+ }
376
+
377
+ // src/index.ts
378
+ function vercelPlugin() {
379
+ let resolvedConfig;
380
+ return {
381
+ apply: "build",
382
+ name: "vite-plugin-vercel",
383
+ enforce: "post",
384
+ configResolved(config) {
385
+ resolvedConfig = config;
386
+ },
387
+ async buildStart() {
388
+ if (process.env.VERCEL_ENV === "production" && !process.env.ENABLE_FILE_SYSTEM_API) {
389
+ throw new Error("Missing ENABLE_FILE_SYSTEM_API=1 to your environment variables in your project settings");
390
+ }
391
+ if (!resolvedConfig.build.ssr) {
392
+ await cleanOutputDirectory(resolvedConfig);
393
+ } else {
394
+ await copyDistClientToOutputStatic(resolvedConfig);
395
+ }
396
+ },
397
+ async writeBundle() {
398
+ var _a;
399
+ if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr))
400
+ return;
401
+ const overrides = await execPrerender(resolvedConfig);
402
+ await buildEndpoints(resolvedConfig);
403
+ const rewrites = await buildPrerenderConfigs(resolvedConfig);
404
+ await writeConfig(resolvedConfig, {
405
+ routes: [{ handle: "filesystem" }, ...rewrites],
406
+ overrides
407
+ });
408
+ }
409
+ };
410
+ }
411
+ async function copyDistClientToOutputStatic(resolvedConfig) {
412
+ await copyDir(getOutDir(resolvedConfig, "client"), getOutput(resolvedConfig, "static"));
413
+ }
414
+ async function cleanOutputDirectory(resolvedConfig) {
415
+ await import_promises5.default.rm(getOutput(resolvedConfig), {
416
+ recursive: true,
417
+ force: true
418
+ });
419
+ }
420
+ function allPlugins() {
421
+ return [vercelPlugin()];
422
+ }
423
+ // Annotate the CommonJS export names for ESM import in node:
424
+ 0 && (module.exports = {});