vite-plugin-vercel 10.0.0-beta.3 → 10.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -15
- package/dist/api.d.ts +1 -55
- package/dist/api.js +2 -9
- package/dist/index.d.ts +2 -8
- package/dist/index.js +9 -817
- package/dist/types.d.ts +1 -121
- package/dist/types.js +2 -1
- package/dist/universal-middleware-dev.d.ts +3 -17
- package/dist/universal-middleware-dev.js +4 -23
- package/dist/universal-middleware-prod.d.ts +3 -12
- package/dist/universal-middleware-prod.js +5 -19
- package/dist/utils.d.ts +3 -23
- package/dist/utils.js +2 -82
- package/package.json +10 -27
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-CR7Q2T4Q.js +0 -75
- package/dist/chunk-OCCU6UM5.js +0 -33
- package/dist/chunk-UTQ72LMT.js +0 -13
- package/dist/chunk-YN5MUEL2.js +0 -34
package/dist/index.js
CHANGED
|
@@ -1,820 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./chunk-CR7Q2T4Q.js";
|
|
10
|
-
import {
|
|
11
|
-
assert
|
|
12
|
-
} from "./chunk-UTQ72LMT.js";
|
|
13
|
-
|
|
14
|
-
// src/plugins/index.ts
|
|
15
|
-
import { installPhoton } from "@photonjs/runtime/vite";
|
|
16
|
-
|
|
17
|
-
// src/plugins/api.ts
|
|
18
|
-
function apiPlugin(pluginConfig) {
|
|
19
|
-
const outfiles = [];
|
|
20
|
-
return {
|
|
21
|
-
name: "vite-plugin-vercel:api",
|
|
22
|
-
api() {
|
|
23
|
-
return createAPI(outfiles, pluginConfig);
|
|
24
|
-
},
|
|
25
|
-
// Compute outfiles for the API
|
|
26
|
-
writeBundle(_opts, bundle2) {
|
|
27
|
-
if (this.environment.name !== "vercel_edge" && this.environment.name !== "vercel_node") return;
|
|
28
|
-
const entries = this.environment.config.photon.entries;
|
|
29
|
-
const server = this.environment.config.photon.server;
|
|
30
|
-
const entryMapByDestination = new Map(
|
|
31
|
-
[server, ...Object.values(entries)].map((e) => [photonEntryDestination(e, ".func/index"), e])
|
|
32
|
-
);
|
|
33
|
-
for (const entry of this.environment.config.photon.entries.filter((e) => e.type === "server-config")) {
|
|
34
|
-
entryMapByDestination.set(photonEntryDestination(entry, ".func/index"), entry);
|
|
35
|
-
}
|
|
36
|
-
for (const [key, value] of Object.entries(bundle2)) {
|
|
37
|
-
if (value.type === "chunk" && value.isEntry && entryMapByDestination.has(removeExtension(key))) {
|
|
38
|
-
outfiles.push({
|
|
39
|
-
type: "chunk",
|
|
40
|
-
root: this.environment.config.root,
|
|
41
|
-
outdir: this.environment.config.build.outDir,
|
|
42
|
-
filepath: key,
|
|
43
|
-
// biome-ignore lint/style/noNonNullAssertion: guarded by entryMap.has(...)
|
|
44
|
-
relatedEntry: entryMapByDestination.get(removeExtension(key))
|
|
45
|
-
});
|
|
46
|
-
} else if (value.type === "asset" && key.startsWith("functions/") || key === "config.json") {
|
|
47
|
-
outfiles.push({
|
|
48
|
-
type: "asset",
|
|
49
|
-
root: this.environment.config.root,
|
|
50
|
-
outdir: this.environment.config.build.outDir,
|
|
51
|
-
filepath: key
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
sharedDuringBuild: true
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function removeExtension(subject) {
|
|
60
|
-
return subject.replace(/\.[^/.]+$/, "");
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// src/plugins/bundle.ts
|
|
64
|
-
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
65
|
-
import { copyFile, mkdir, rm } from "fs/promises";
|
|
66
|
-
import { builtinModules } from "module";
|
|
67
|
-
import path2 from "path";
|
|
68
|
-
import { findRoot } from "@manypkg/find-root";
|
|
69
|
-
import { nodeFileTrace } from "@vercel/nft";
|
|
70
|
-
import { build } from "esbuild";
|
|
71
|
-
|
|
72
|
-
// src/helpers.ts
|
|
73
|
-
import path from "path";
|
|
74
|
-
function joinAbsolute(env_or_p0, p1, ...p) {
|
|
75
|
-
if (path.isAbsolute(p1)) {
|
|
76
|
-
return path.join(p1, ...p);
|
|
77
|
-
}
|
|
78
|
-
return path.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
|
|
79
|
-
}
|
|
80
|
-
function joinAbsolutePosix(env_or_p0, p1, ...p) {
|
|
81
|
-
if (path.isAbsolute(p1)) {
|
|
82
|
-
return path.posix.join(p1, ...p);
|
|
83
|
-
}
|
|
84
|
-
return path.posix.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/utils/edge.ts
|
|
88
|
-
var edgeConditions = ["edge-light", "worker", "browser", "module", "import", "default"];
|
|
89
|
-
|
|
90
|
-
// src/utils/env.ts
|
|
91
|
-
function isVercelLastBuildStep(env) {
|
|
92
|
-
const name = typeof env !== "string" ? env.name : env;
|
|
93
|
-
return name === "vercel_node";
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// src/utils/external.ts
|
|
97
|
-
var _external = ["async_hooks", "events", "buffer", "assert", "util"];
|
|
98
|
-
var edgeExternal = [..._external, ..._external.map((e) => `node:${e}`)];
|
|
99
|
-
|
|
100
|
-
// src/plugins/bundle.ts
|
|
101
|
-
var builtIns = new Set(builtinModules.flatMap((m) => [m, `node:${m}`]));
|
|
102
|
-
var edgeWasmPlugin = {
|
|
103
|
-
name: "edge-wasm-vercel",
|
|
104
|
-
setup(build2) {
|
|
105
|
-
build2.onResolve({ filter: /\.wasm/ }, (args) => {
|
|
106
|
-
return {
|
|
107
|
-
path: args.path.replace(/\.wasm\?module$/i, ".wasm"),
|
|
108
|
-
external: true
|
|
109
|
-
};
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
var dynamicNativeImportPlugin = {
|
|
114
|
-
name: "edge-dynamic-import-native",
|
|
115
|
-
setup(build2) {
|
|
116
|
-
build2.onResolve({ filter: /.*/ }, (args) => {
|
|
117
|
-
if (args.kind === "dynamic-import" && builtIns.has(args.path)) {
|
|
118
|
-
return { path: args.path, external: true };
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
var reactEdgePlugin = {
|
|
124
|
-
name: "react-edge-plugin",
|
|
125
|
-
setup(build2) {
|
|
126
|
-
build2.onResolve({ filter: /^react-dom\/server$/ }, (args) => {
|
|
127
|
-
const { path: _, ...rest } = args;
|
|
128
|
-
return build2.resolve("react-dom/server.edge", rest);
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
function bundlePlugin(pluginConfig) {
|
|
133
|
-
const bundledAssets = /* @__PURE__ */ new Map();
|
|
134
|
-
return [
|
|
135
|
-
{
|
|
136
|
-
name: "vite-plugin-vercel:bundle",
|
|
137
|
-
enforce: "post",
|
|
138
|
-
apply: "build",
|
|
139
|
-
generateBundle(_opts, bundle2) {
|
|
140
|
-
for (const b of Object.values(bundle2)) {
|
|
141
|
-
if (b.type === "asset") {
|
|
142
|
-
const originalFileNames = b.originalFileNames.map(
|
|
143
|
-
(relativePath) => path2.resolve(this.environment.config.root, relativePath)
|
|
144
|
-
);
|
|
145
|
-
const asset = {
|
|
146
|
-
env: this.environment.name,
|
|
147
|
-
root: this.environment.config.root,
|
|
148
|
-
outDir: this.environment.config.build.outDir,
|
|
149
|
-
outFile: joinAbsolute(this.environment, this.environment.config.build.outDir, b.fileName),
|
|
150
|
-
fileName: b.fileName
|
|
151
|
-
};
|
|
152
|
-
for (const originalFileName of originalFileNames) {
|
|
153
|
-
bundledAssets.set(originalFileName, asset);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
closeBundle: {
|
|
159
|
-
order: "post",
|
|
160
|
-
async handler() {
|
|
161
|
-
if (!isVercelLastBuildStep(this.environment)) return;
|
|
162
|
-
this.environment.logger.info("Creating Vercel bundles...");
|
|
163
|
-
const api = getVercelAPI(this);
|
|
164
|
-
const outfiles = api.getOutFiles();
|
|
165
|
-
for (const outfile of outfiles) {
|
|
166
|
-
if (outfile.type === "asset") {
|
|
167
|
-
const { source, destination } = getAbsoluteOutFileWithout_tmp(outfile);
|
|
168
|
-
await mkdir(path2.dirname(destination), { recursive: true });
|
|
169
|
-
await copyFile(source, destination);
|
|
170
|
-
} else {
|
|
171
|
-
await bundle(this.environment, bundledAssets, outfile);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
const tmpDir = pluginConfig.outDir ? path2.posix.join(pluginConfig.outDir, "_tmp") : ".vercel/output/_tmp";
|
|
175
|
-
await rm(tmpDir, { recursive: true });
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
sharedDuringBuild: true
|
|
179
|
-
}
|
|
180
|
-
];
|
|
181
|
-
}
|
|
182
|
-
function getAbsoluteOutFileWithout_tmp(outfile) {
|
|
183
|
-
const source = joinAbsolutePosix(outfile.root, outfile.outdir, outfile.filepath);
|
|
184
|
-
const destination = source.replace(outfile.outdir, outfile.outdir.replace(/_tmp(\/|\\|$)/, ""));
|
|
185
|
-
return {
|
|
186
|
-
source,
|
|
187
|
-
destination
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
async function bundle(environment, bundledAssets, outfile) {
|
|
191
|
-
const { source, destination } = getAbsoluteOutFileWithout_tmp(outfile);
|
|
192
|
-
const isEdge = Boolean(outfile.relatedEntry.vercel?.edge);
|
|
193
|
-
const buildOptions = {
|
|
194
|
-
format: "esm",
|
|
195
|
-
target: "es2022",
|
|
196
|
-
legalComments: "none",
|
|
197
|
-
bundle: true,
|
|
198
|
-
entryPoints: [source],
|
|
199
|
-
treeShaking: true,
|
|
200
|
-
logOverride: { "ignored-bare-import": "silent" }
|
|
201
|
-
};
|
|
202
|
-
if (isEdge) {
|
|
203
|
-
buildOptions.platform = "browser";
|
|
204
|
-
buildOptions.external = edgeExternal;
|
|
205
|
-
buildOptions.conditions = edgeConditions;
|
|
206
|
-
buildOptions.define = {
|
|
207
|
-
"process.env.NODE_ENV": JSON.stringify("production")
|
|
208
|
-
};
|
|
209
|
-
buildOptions.outExtension = { ".js": ".mjs" };
|
|
210
|
-
buildOptions.outfile = destination.replace(/\.mjs$/, ".js");
|
|
211
|
-
buildOptions.plugins = [edgeWasmPlugin, dynamicNativeImportPlugin, reactEdgePlugin];
|
|
212
|
-
} else {
|
|
213
|
-
buildOptions.platform = "node";
|
|
214
|
-
buildOptions.outfile = destination.replace(/\.js$/, ".mjs");
|
|
215
|
-
buildOptions.banner = {
|
|
216
|
-
js: `import { createRequire as topLevelCreateRequire } from 'module';
|
|
217
|
-
import { dirname as topLevelDirname } from 'path';
|
|
218
|
-
import { fileURLToPath as topLevelFileURLToPath } from 'url';
|
|
219
|
-
const require = topLevelCreateRequire(import.meta.url);
|
|
220
|
-
const __filename = topLevelFileURLToPath(import.meta.url);
|
|
221
|
-
const __dirname = topLevelDirname(__filename);
|
|
222
|
-
`
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
try {
|
|
226
|
-
await build(buildOptions);
|
|
227
|
-
} catch (e) {
|
|
228
|
-
throw new Error(`Error while bundling ${destination}`, { cause: e });
|
|
229
|
-
}
|
|
230
|
-
let base = environment.config.root;
|
|
231
|
-
try {
|
|
232
|
-
const dir = await findRoot(environment.config.root);
|
|
233
|
-
base = dir.rootDir;
|
|
234
|
-
} catch (_e) {
|
|
235
|
-
}
|
|
236
|
-
const entryFilePath = existsSync(outfile.relatedEntry.id) ? outfile.relatedEntry.id : outfile.relatedEntry.resolvedId && existsSync(outfile.relatedEntry.resolvedId) ? outfile.relatedEntry.resolvedId : null;
|
|
237
|
-
if (entryFilePath === null) {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
const { fileList, reasons } = await nodeFileTrace([entryFilePath], {
|
|
241
|
-
base,
|
|
242
|
-
processCwd: environment.config.root,
|
|
243
|
-
mixedModules: true,
|
|
244
|
-
// https://github.com/vercel/next.js/blob/7c8e2b4e50449ce2d2c83de0b5638c61b7de2362/packages/next/src/build/collect-build-traces.ts#L201
|
|
245
|
-
ignore: [
|
|
246
|
-
"**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js",
|
|
247
|
-
"**/*.d.ts",
|
|
248
|
-
"**/*.map",
|
|
249
|
-
"**/node_modules/webpack5/**/*"
|
|
250
|
-
],
|
|
251
|
-
async readFile(filepath) {
|
|
252
|
-
if (filepath.endsWith(".ts") || filepath.endsWith(".tsx")) {
|
|
253
|
-
const result = await build({
|
|
254
|
-
target: "es2022",
|
|
255
|
-
format: "esm",
|
|
256
|
-
platform: "node",
|
|
257
|
-
logLevel: "info",
|
|
258
|
-
logOverride: {
|
|
259
|
-
"ignored-bare-import": "verbose",
|
|
260
|
-
"require-resolve-not-external": "verbose"
|
|
261
|
-
},
|
|
262
|
-
minify: false,
|
|
263
|
-
plugins: [],
|
|
264
|
-
define: {
|
|
265
|
-
"process.env.NODE_ENV": '"production"',
|
|
266
|
-
"import.meta.env.NODE_ENV": '"production"'
|
|
267
|
-
},
|
|
268
|
-
entryPoints: [entryFilePath],
|
|
269
|
-
bundle: false,
|
|
270
|
-
write: false
|
|
271
|
-
});
|
|
272
|
-
return result.outputFiles[0].text;
|
|
273
|
-
}
|
|
274
|
-
return readFileSync(filepath, "utf-8");
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
for (const file of fileList) {
|
|
278
|
-
if (reasons.has(file) && reasons.get(file)?.type.includes("asset") && !file.endsWith(".js") && !file.endsWith(".cjs") && !file.endsWith(".mjs") && !file.endsWith("package.json")) {
|
|
279
|
-
const absolutePath = path2.join(base, file);
|
|
280
|
-
const assetRenamedByVite = bundledAssets.get(absolutePath);
|
|
281
|
-
const basename = path2.basename(assetRenamedByVite ? assetRenamedByVite.fileName : absolutePath);
|
|
282
|
-
if (assetRenamedByVite) {
|
|
283
|
-
writeFileSync(
|
|
284
|
-
destination,
|
|
285
|
-
readFileSync(destination, "utf-8").replaceAll(
|
|
286
|
-
`/${assetRenamedByVite.fileName}`,
|
|
287
|
-
`./${path2.basename(assetRenamedByVite.fileName)}`
|
|
288
|
-
)
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
await copyFile(absolutePath, path2.join(path2.dirname(destination), basename));
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// src/plugins/clean-outdir.ts
|
|
297
|
-
import fs from "fs";
|
|
298
|
-
import path3 from "path";
|
|
299
|
-
function vercelCleanupPlugin(pluginConfig) {
|
|
300
|
-
let alreadyRun = false;
|
|
301
|
-
return {
|
|
302
|
-
apply: "build",
|
|
303
|
-
name: "vite-plugin-vercel:cleanup",
|
|
304
|
-
enforce: "pre",
|
|
305
|
-
applyToEnvironment(env) {
|
|
306
|
-
return env.name === "client";
|
|
307
|
-
},
|
|
308
|
-
buildStart: {
|
|
309
|
-
order: "pre",
|
|
310
|
-
sequential: true,
|
|
311
|
-
handler() {
|
|
312
|
-
if (alreadyRun) return;
|
|
313
|
-
alreadyRun = true;
|
|
314
|
-
const absoluteOutdir = pluginConfig?.outDir && path3.isAbsolute(pluginConfig.outDir) ? pluginConfig.outDir : path3.join(this.environment.config.root, pluginConfig?.outDir ?? ".vercel/output");
|
|
315
|
-
cleanOutputDirectory(absoluteOutdir);
|
|
316
|
-
}
|
|
317
|
-
},
|
|
318
|
-
sharedDuringBuild: true
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
function cleanOutputDirectory(outdir) {
|
|
322
|
-
fs.rmSync(outdir, {
|
|
323
|
-
recursive: true,
|
|
324
|
-
force: true
|
|
325
|
-
});
|
|
326
|
-
fs.mkdirSync(outdir, { recursive: true });
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// src/plugins/loader.ts
|
|
330
|
-
import { targetLoader } from "@photonjs/core/vite";
|
|
331
|
-
import { getNodeVersion } from "@vercel/build-utils";
|
|
332
|
-
import { vercelOutputPrerenderConfigSchema } from "@vite-plugin-vercel/schemas";
|
|
333
|
-
import { toPathToRegexpV6 } from "convert-route/path-to-regexp-v6";
|
|
334
|
-
import { fromRou3 } from "convert-route/rou3";
|
|
335
|
-
|
|
336
|
-
// src/build.ts
|
|
337
|
-
import { vercelOutputVcConfigSchema } from "@vite-plugin-vercel/schemas";
|
|
338
|
-
function getVcConfig(pluginConfig, filename, options) {
|
|
339
|
-
return vercelOutputVcConfigSchema.parse(
|
|
340
|
-
options.edge ? {
|
|
341
|
-
runtime: "edge",
|
|
342
|
-
entrypoint: filename
|
|
343
|
-
} : {
|
|
344
|
-
runtime: options.nodeVersion.runtime,
|
|
345
|
-
handler: filename,
|
|
346
|
-
maxDuration: pluginConfig.defaultMaxDuration,
|
|
347
|
-
launcherType: "Nodejs",
|
|
348
|
-
shouldAddHelpers: true,
|
|
349
|
-
supportsResponseStreaming: options.streaming ?? pluginConfig.defaultSupportsResponseStreaming
|
|
350
|
-
}
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// src/plugins/loader.ts
|
|
355
|
-
var DUMMY = "__DUMMY__";
|
|
356
|
-
var re_DUMMY = new RegExp(`${DUMMY}$`);
|
|
357
|
-
var nonEdgeServers = ["express", "fastify"];
|
|
358
|
-
function loaderPlugin(pluginConfig) {
|
|
359
|
-
let nodeVersion;
|
|
360
|
-
return [
|
|
361
|
-
{
|
|
362
|
-
name: "vite-plugin-vercel:update-entries",
|
|
363
|
-
apply: "build",
|
|
364
|
-
buildStart: {
|
|
365
|
-
order: "post",
|
|
366
|
-
handler() {
|
|
367
|
-
for (const entry of [this.environment.config.photon.server, ...this.environment.config.photon.entries]) {
|
|
368
|
-
if (!entry.env) {
|
|
369
|
-
entry.env = entry.vercel?.edge ? "vercel_edge" : "vercel_node";
|
|
370
|
-
}
|
|
371
|
-
if (entry.env === "vercel_edge" || entry.env === "vercel_node") {
|
|
372
|
-
entry.target = `${photonEntryDestination(entry, ".func/index")}.js`;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
sharedDuringBuild: true
|
|
378
|
-
},
|
|
379
|
-
{
|
|
380
|
-
name: "vite-plugin-vercel:dummy",
|
|
381
|
-
enforce: "pre",
|
|
382
|
-
resolveId: {
|
|
383
|
-
filter: {
|
|
384
|
-
id: re_DUMMY
|
|
385
|
-
},
|
|
386
|
-
handler(id) {
|
|
387
|
-
return id;
|
|
388
|
-
}
|
|
389
|
-
},
|
|
390
|
-
load: {
|
|
391
|
-
filter: {
|
|
392
|
-
id: re_DUMMY
|
|
393
|
-
},
|
|
394
|
-
handler() {
|
|
395
|
-
return "export default {};";
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
},
|
|
399
|
-
...targetLoader("vercel", {
|
|
400
|
-
async buildStart() {
|
|
401
|
-
nodeVersion = await getNodeVersion(process.cwd());
|
|
402
|
-
},
|
|
403
|
-
applyToEnvironment(env) {
|
|
404
|
-
return env.name === "vercel_node" || env.name === "vercel_edge" || env.name === "vercel_client";
|
|
405
|
-
},
|
|
406
|
-
async load(_id, { meta }) {
|
|
407
|
-
const entry = meta;
|
|
408
|
-
const isEdge = Boolean(entry.vercel?.edge);
|
|
409
|
-
this.emitFile({
|
|
410
|
-
type: "asset",
|
|
411
|
-
fileName: photonEntryDestination(entry, ".func/.vc-config.json"),
|
|
412
|
-
source: JSON.stringify(
|
|
413
|
-
// Unpredictable things happen when the extension is .mjs on edge
|
|
414
|
-
getVcConfig(pluginConfig, isEdge ? "index.js" : "index.mjs", {
|
|
415
|
-
nodeVersion,
|
|
416
|
-
edge: isEdge,
|
|
417
|
-
streaming: entry.vercel?.streaming
|
|
418
|
-
}),
|
|
419
|
-
void 0,
|
|
420
|
-
2
|
|
421
|
-
)
|
|
422
|
-
});
|
|
423
|
-
if (entry.vercel?.isr) {
|
|
424
|
-
this.emitFile({
|
|
425
|
-
type: "asset",
|
|
426
|
-
fileName: photonEntryDestination(entry, ".prerender-config.json"),
|
|
427
|
-
source: JSON.stringify(vercelOutputPrerenderConfigSchema.parse(entry.vercel.isr), void 0, 2)
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
if (entry.route || entry.vercel?.route) {
|
|
431
|
-
pluginConfig.rewrites ??= [];
|
|
432
|
-
const source = typeof entry.vercel?.route === "string" ? `(${entry.vercel.route})` : typeof entry.route === "string" ? toPathToRegexpV6(fromRou3(entry.route)) : entryToPathtoregex(entry);
|
|
433
|
-
pluginConfig.rewrites.push({
|
|
434
|
-
enforce: entry.vercel?.enforce,
|
|
435
|
-
source,
|
|
436
|
-
destination: typeof entry.vercel?.route === "string" ? `/${photonEntryDestinationDefault(entry)}?__original_path=$1` : `/${photonEntryDestinationDefault(entry)}`
|
|
437
|
-
});
|
|
438
|
-
if (entry.vercel?.headers) {
|
|
439
|
-
pluginConfig.headers ??= [];
|
|
440
|
-
pluginConfig.headers.push({
|
|
441
|
-
source,
|
|
442
|
-
headers: Object.entries(entry.vercel.headers).map(([key, value]) => ({
|
|
443
|
-
key,
|
|
444
|
-
value
|
|
445
|
-
}))
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
const fn = isEdge ? "createEdgeHandler" : "createNodeHandler";
|
|
450
|
-
const isServerEntry = entry.type === "server";
|
|
451
|
-
if (isServerEntry) {
|
|
452
|
-
assert(entry.server, `Could not determine server for entry ${entry.id}`);
|
|
453
|
-
if (isEdge) {
|
|
454
|
-
assert(
|
|
455
|
-
!nonEdgeServers.includes(entry.server),
|
|
456
|
-
`${entry.server} is not compatible with Vercel Edge target. Either use another server like Hono or change target to Node`
|
|
457
|
-
);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
const importFrom = isServerEntry ? `@universal-middleware/vercel/${entry.server}` : "@universal-middleware/vercel";
|
|
461
|
-
const exportDefault = isServerEntry ? `export default ${fn}(handlerOrApp)` : `export default ${fn}(() => handlerOrApp)()`;
|
|
462
|
-
return `
|
|
463
|
-
import { ${fn} } from "photon:resolve-from-photon:${importFrom}";
|
|
464
|
-
import handlerOrApp from "${entry.resolvedId ?? entry.id}";
|
|
465
|
-
|
|
466
|
-
${exportDefault};
|
|
467
|
-
`;
|
|
468
|
-
}
|
|
469
|
-
})
|
|
470
|
-
];
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// src/plugins/routes.ts
|
|
474
|
-
function routesPlugins() {
|
|
475
|
-
return [
|
|
476
|
-
{
|
|
477
|
-
name: "vite-plugin-vercel:routes-dedupe",
|
|
478
|
-
apply: "build",
|
|
479
|
-
applyToEnvironment(env) {
|
|
480
|
-
return env.name === "ssr";
|
|
481
|
-
},
|
|
482
|
-
buildStart: {
|
|
483
|
-
// Ensure that this hook is executed close to last, ensuring that all plugins had time to inject their Photon entries
|
|
484
|
-
order: "post",
|
|
485
|
-
handler() {
|
|
486
|
-
const entriesEdge = this.environment.config.photon.entries.filter((e) => e.vercel?.edge);
|
|
487
|
-
const entriesNode = this.environment.config.photon.entries.filter((e) => !e.vercel?.edge);
|
|
488
|
-
const entriesToKeep = /* @__PURE__ */ new Set();
|
|
489
|
-
const defaultEnvIsEdge = this.environment.config.photon.defaultBuildEnv === "vercel_edge";
|
|
490
|
-
for (const envEntries of defaultEnvIsEdge ? [entriesEdge] : [entriesNode]) {
|
|
491
|
-
for (const page of envEntries.filter(
|
|
492
|
-
(p) => p.compositionMode === "isolated" || p.vercel?.isr || p.vercel?.route && p.vercel?.headers !== null && p.vercel?.headers !== void 0
|
|
493
|
-
)) {
|
|
494
|
-
entriesToKeep.add(page);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
for (const page of defaultEnvIsEdge ? entriesNode : entriesEdge) {
|
|
498
|
-
entriesToKeep.add(page);
|
|
499
|
-
}
|
|
500
|
-
console.log("entriesToKeep", entriesToKeep);
|
|
501
|
-
this.environment.config.photon.entries = this.environment.config.photon.entries.filter(
|
|
502
|
-
(e) => !e.vikeMeta || entriesToKeep.has(e)
|
|
503
|
-
);
|
|
504
|
-
}
|
|
505
|
-
},
|
|
506
|
-
sharedDuringBuild: true
|
|
507
|
-
}
|
|
508
|
-
];
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
// src/plugins/setupEnvs.ts
|
|
512
|
-
import {
|
|
513
|
-
BuildEnvironment,
|
|
514
|
-
createLogger,
|
|
515
|
-
createRunnableDevEnvironment,
|
|
516
|
-
mergeConfig
|
|
517
|
-
} from "vite";
|
|
518
|
-
import path4 from "path";
|
|
519
|
-
|
|
520
|
-
// src/config.ts
|
|
521
|
-
import { getTransformedRoutes, mergeRoutes, normalizeRoutes } from "@vercel/routing-utils";
|
|
522
|
-
import { vercelOutputConfigSchema } from "@vite-plugin-vercel/schemas";
|
|
523
|
-
function reorderEnforce(arr) {
|
|
524
|
-
return [
|
|
525
|
-
...arr.filter((r) => r.enforce === "pre"),
|
|
526
|
-
...arr.filter((r) => !r.enforce),
|
|
527
|
-
...arr.filter((r) => r.enforce === "post")
|
|
528
|
-
];
|
|
529
|
-
}
|
|
530
|
-
function getConfig(pluginConfig) {
|
|
531
|
-
const _rewrites = [...pluginConfig.rewrites ?? []];
|
|
532
|
-
const { routes, error } = getTransformedRoutes({
|
|
533
|
-
cleanUrls: pluginConfig.cleanUrls ?? true,
|
|
534
|
-
trailingSlash: pluginConfig.trailingSlash,
|
|
535
|
-
rewrites: reorderEnforce(_rewrites),
|
|
536
|
-
redirects: pluginConfig.redirects ? reorderEnforce(pluginConfig.redirects) : void 0,
|
|
537
|
-
headers: pluginConfig.headers
|
|
538
|
-
});
|
|
539
|
-
if (error) {
|
|
540
|
-
throw error;
|
|
541
|
-
}
|
|
542
|
-
if (pluginConfig.config?.routes && pluginConfig.config.routes.length > 0 && !pluginConfig.config.routes.every((r) => "continue" in r && r.continue)) {
|
|
543
|
-
console.warn(
|
|
544
|
-
'Did you forget to add `"continue": true` to your routes? See https://vercel.com/docs/build-output-api/v3/configuration#source-route\nIf not, it is discouraged to use `routes` config to override routes. Prefer using `rewrites` and `redirects`.'
|
|
545
|
-
);
|
|
546
|
-
}
|
|
547
|
-
let userRoutes = [];
|
|
548
|
-
let buildRoutes = [];
|
|
549
|
-
if (pluginConfig.config?.routes) {
|
|
550
|
-
const norm = normalizeRoutes(pluginConfig.config.routes);
|
|
551
|
-
if (norm.error) {
|
|
552
|
-
throw norm.error;
|
|
553
|
-
}
|
|
554
|
-
userRoutes = norm.routes ?? [];
|
|
555
|
-
}
|
|
556
|
-
if (routes) {
|
|
557
|
-
const norm = normalizeRoutes(routes);
|
|
558
|
-
if (norm.error) {
|
|
559
|
-
throw norm.error;
|
|
560
|
-
}
|
|
561
|
-
buildRoutes = norm.routes ?? [];
|
|
562
|
-
}
|
|
563
|
-
const cleanRoutes = mergeRoutes({
|
|
564
|
-
userRoutes,
|
|
565
|
-
builds: [
|
|
566
|
-
{
|
|
567
|
-
use: "@vercel/node",
|
|
568
|
-
entrypoint: "index.mjs",
|
|
569
|
-
routes: buildRoutes
|
|
570
|
-
}
|
|
571
|
-
]
|
|
572
|
-
});
|
|
573
|
-
return vercelOutputConfigSchema.parse({
|
|
574
|
-
version: 3,
|
|
575
|
-
...pluginConfig.config,
|
|
576
|
-
routes: cleanRoutes,
|
|
577
|
-
overrides: {
|
|
578
|
-
...pluginConfig.config?.overrides
|
|
579
|
-
}
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
// src/plugins/setupEnvs.ts
|
|
584
|
-
import stripAnsi from "strip-ansi";
|
|
585
|
-
|
|
586
|
-
// src/utils/const.ts
|
|
587
|
-
var virtualEntry = "virtual:vite-plugin-vercel:entry";
|
|
588
|
-
|
|
589
|
-
// src/plugins/setupEnvs.ts
|
|
590
|
-
var outDir = ".vercel/output";
|
|
591
|
-
var DUMMY2 = "__DUMMY__";
|
|
592
|
-
function setupEnvs(pluginConfig) {
|
|
593
|
-
return [
|
|
594
|
-
{
|
|
595
|
-
name: "vite-plugin-vercel:setup-envs",
|
|
596
|
-
buildApp: {
|
|
597
|
-
order: "post",
|
|
598
|
-
async handler(builder) {
|
|
599
|
-
await builder.build(builder.environments.vercel_client);
|
|
600
|
-
await builder.build(builder.environments.vercel_edge);
|
|
601
|
-
await builder.build(builder.environments.vercel_node);
|
|
602
|
-
}
|
|
603
|
-
},
|
|
604
|
-
config() {
|
|
605
|
-
const outDirOverride = pluginConfig.outDir ? {
|
|
606
|
-
build: {
|
|
607
|
-
outDir: path4.posix.join(pluginConfig.outDir, "_tmp")
|
|
608
|
-
}
|
|
609
|
-
} : {};
|
|
610
|
-
return {
|
|
611
|
-
environments: {
|
|
612
|
-
vercel_edge: createVercelEnvironmentOptions(outDirOverride),
|
|
613
|
-
vercel_node: createVercelEnvironmentOptions(outDirOverride),
|
|
614
|
-
vercel_client: {
|
|
615
|
-
build: {
|
|
616
|
-
outDir: path4.join(pluginConfig.outDir ?? outDir, "static"),
|
|
617
|
-
copyPublicDir: true
|
|
618
|
-
},
|
|
619
|
-
consumer: "client"
|
|
620
|
-
}
|
|
621
|
-
},
|
|
622
|
-
customLogger: createVercelLogger(),
|
|
623
|
-
// Required for environments to be taken into account
|
|
624
|
-
builder: {}
|
|
625
|
-
};
|
|
626
|
-
},
|
|
627
|
-
sharedDuringBuild: true
|
|
628
|
-
},
|
|
629
|
-
{
|
|
630
|
-
name: "vite-plugin-vercel:setup-envs:vercel_edge",
|
|
631
|
-
applyToEnvironment(env) {
|
|
632
|
-
return env.name === "vercel_edge";
|
|
633
|
-
},
|
|
634
|
-
configEnvironment(name, config, env) {
|
|
635
|
-
if (name !== "vercel_edge") return;
|
|
636
|
-
const isDev = env.command === "serve";
|
|
637
|
-
const conditions = !isDev ? {
|
|
638
|
-
conditions: edgeConditions
|
|
639
|
-
} : {};
|
|
640
|
-
return {
|
|
641
|
-
resolve: {
|
|
642
|
-
external: edgeExternal,
|
|
643
|
-
...conditions
|
|
644
|
-
},
|
|
645
|
-
build: {
|
|
646
|
-
target: "es2022",
|
|
647
|
-
rollupOptions: {
|
|
648
|
-
input: {},
|
|
649
|
-
treeshake: "smallest"
|
|
650
|
-
}
|
|
651
|
-
},
|
|
652
|
-
optimizeDeps: {
|
|
653
|
-
...config.optimizeDeps,
|
|
654
|
-
esbuildOptions: {
|
|
655
|
-
target: "es2022",
|
|
656
|
-
format: "esm"
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
};
|
|
660
|
-
},
|
|
661
|
-
generateBundle: {
|
|
662
|
-
order: "post",
|
|
663
|
-
async handler(_opts, bundle2) {
|
|
664
|
-
cleanupDummy(bundle2);
|
|
665
|
-
}
|
|
666
|
-
},
|
|
667
|
-
sharedDuringBuild: true
|
|
668
|
-
},
|
|
669
|
-
{
|
|
670
|
-
name: "vite-plugin-vercel:setup-envs:vercel_node",
|
|
671
|
-
applyToEnvironment(env) {
|
|
672
|
-
return env.name === "vercel_node";
|
|
673
|
-
},
|
|
674
|
-
configEnvironment(name, config) {
|
|
675
|
-
if (name !== "vercel_node") return;
|
|
676
|
-
return {
|
|
677
|
-
optimizeDeps: {
|
|
678
|
-
...config.optimizeDeps
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
|
-
},
|
|
682
|
-
generateBundle: {
|
|
683
|
-
order: "post",
|
|
684
|
-
async handler(_opts, bundle2) {
|
|
685
|
-
cleanupDummy(bundle2);
|
|
686
|
-
this.emitFile({
|
|
687
|
-
type: "asset",
|
|
688
|
-
fileName: "config.json",
|
|
689
|
-
source: JSON.stringify(getConfig(pluginConfig), void 0, 2)
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
},
|
|
693
|
-
sharedDuringBuild: true
|
|
694
|
-
}
|
|
695
|
-
];
|
|
696
|
-
}
|
|
697
|
-
function createVercelLogger() {
|
|
698
|
-
const logger = createLogger();
|
|
699
|
-
const loggerInfo = logger.info;
|
|
700
|
-
logger.info = (msg, options) => {
|
|
701
|
-
if (options?.environment && (msg.includes("building for production") || msg.includes("building SSR bundle for production"))) {
|
|
702
|
-
return loggerInfo(`${msg} ${options.environment}`, options);
|
|
703
|
-
}
|
|
704
|
-
if (msg.includes(".vercel/output/_tmp")) {
|
|
705
|
-
const strippedMsg = stripAnsi(msg);
|
|
706
|
-
if (strippedMsg.includes(".vercel/output/_tmp/assets") || strippedMsg.includes(".vercel/output/_tmp/chunks") || strippedMsg.includes(".vercel/output/_tmp/entries"))
|
|
707
|
-
return;
|
|
708
|
-
if (!strippedMsg.includes(".vercel/output/_tmp/functions/") && !strippedMsg.includes(".vercel/output/_tmp/config.json")) {
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
return loggerInfo(msg.replace(".vercel/output/_tmp/", ".vercel/output/"), options);
|
|
712
|
-
}
|
|
713
|
-
loggerInfo(msg, options);
|
|
714
|
-
};
|
|
715
|
-
return logger;
|
|
716
|
-
}
|
|
717
|
-
function createVercelEnvironmentOptions(overrides) {
|
|
718
|
-
return mergeConfig(
|
|
719
|
-
{
|
|
720
|
-
// The bundle generated by esbuild takes care of this
|
|
721
|
-
// resolve: {
|
|
722
|
-
// noExternal: true,
|
|
723
|
-
// },
|
|
724
|
-
dev: {
|
|
725
|
-
async createEnvironment(name, config) {
|
|
726
|
-
return createRunnableDevEnvironment(name, config);
|
|
727
|
-
}
|
|
728
|
-
},
|
|
729
|
-
build: {
|
|
730
|
-
createEnvironment(name, config) {
|
|
731
|
-
return new BuildEnvironment(name, config);
|
|
732
|
-
},
|
|
733
|
-
outDir: path4.posix.join(outDir, "_tmp"),
|
|
734
|
-
copyPublicDir: false,
|
|
735
|
-
rollupOptions: {
|
|
736
|
-
input: getDummyInput(),
|
|
737
|
-
output: {
|
|
738
|
-
sanitizeFileName: false,
|
|
739
|
-
sourcemap: false
|
|
740
|
-
}
|
|
741
|
-
},
|
|
742
|
-
target: "es2022",
|
|
743
|
-
emptyOutDir: false,
|
|
744
|
-
emitAssets: true
|
|
745
|
-
},
|
|
746
|
-
consumer: "server",
|
|
747
|
-
keepProcessEnv: true
|
|
748
|
-
},
|
|
749
|
-
overrides ?? {}
|
|
750
|
-
);
|
|
751
|
-
}
|
|
752
|
-
function getDummyInput() {
|
|
753
|
-
return { [DUMMY2]: `${virtualEntry}:${DUMMY2}` };
|
|
754
|
-
}
|
|
755
|
-
function cleanupDummy(bundle2) {
|
|
756
|
-
const dummy = Object.keys(bundle2).find((key) => key.includes(DUMMY2));
|
|
757
|
-
if (dummy) {
|
|
758
|
-
delete bundle2[dummy];
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
// src/plugins/wasm.ts
|
|
763
|
-
function wasmPlugin() {
|
|
764
|
-
return {
|
|
765
|
-
name: "vite-plugin-vercel:fix-wasm-module",
|
|
766
|
-
enforce: "pre",
|
|
767
|
-
resolveId: {
|
|
768
|
-
order: "pre",
|
|
769
|
-
async handler(id) {
|
|
770
|
-
if (!id.endsWith(".wasm?module")) return;
|
|
771
|
-
if (this.environment.name !== "vercel_edge") return;
|
|
772
|
-
return {
|
|
773
|
-
id,
|
|
774
|
-
external: true
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
};
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
// src/plugins/index.ts
|
|
782
|
-
function vercel(pluginConfig = {}) {
|
|
783
|
-
const additionalConfig = {};
|
|
784
|
-
if (pluginConfig.entries) {
|
|
785
|
-
additionalConfig.entries = pluginConfig.entries;
|
|
786
|
-
}
|
|
787
|
-
if (pluginConfig.server) {
|
|
788
|
-
additionalConfig.server = pluginConfig.server;
|
|
789
|
-
}
|
|
790
|
-
return [
|
|
791
|
-
...installPhoton("vite-plugin-vercel", {
|
|
792
|
-
...additionalConfig,
|
|
793
|
-
defaultBuildEnv: "vercel_node",
|
|
794
|
-
codeSplitting: {
|
|
795
|
-
target: true
|
|
796
|
-
},
|
|
797
|
-
devServer: {
|
|
798
|
-
env: "vercel_node"
|
|
799
|
-
},
|
|
800
|
-
resolveMiddlewares(env) {
|
|
801
|
-
if (env === "dev") {
|
|
802
|
-
return "vite-plugin-vercel/universal-middleware/dev";
|
|
803
|
-
}
|
|
804
|
-
return "vite-plugin-vercel/universal-middleware";
|
|
805
|
-
}
|
|
806
|
-
}),
|
|
807
|
-
vercelCleanupPlugin(),
|
|
808
|
-
apiPlugin(pluginConfig),
|
|
809
|
-
...setupEnvs(pluginConfig),
|
|
810
|
-
wasmPlugin(),
|
|
811
|
-
...loaderPlugin(pluginConfig),
|
|
812
|
-
...routesPlugins(),
|
|
813
|
-
...bundlePlugin(pluginConfig)
|
|
814
|
-
];
|
|
815
|
-
}
|
|
816
|
-
var plugins_default = vercel;
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { installPhotonResolver } from "@photonjs/core/vite";
|
|
3
|
+
import plugin from "@photonjs/vercel/vite";
|
|
4
|
+
var vercel = (options) => {
|
|
5
|
+
const plugins = plugin(options);
|
|
6
|
+
return [...plugins, ...installPhotonResolver("vite-plugin-vercel")];
|
|
7
|
+
};
|
|
8
|
+
var vite_default = vercel;
|
|
817
9
|
export {
|
|
818
|
-
|
|
10
|
+
vite_default as default,
|
|
819
11
|
vercel
|
|
820
12
|
};
|