vite-plugin-vercel 11.0.0-beta.2 → 11.0.0-beta.4
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/types.d.ts +14 -0
- package/dist/vite.js +303 -74
- package/package.json +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -12,6 +12,20 @@ type ViteVercelRedirect = Redirect & {
|
|
|
12
12
|
};
|
|
13
13
|
type PluginContext = ThisParameterType<Extract<Plugin["resolveId"], (...args: never) => any>>;
|
|
14
14
|
interface ViteVercelConfig {
|
|
15
|
+
/**
|
|
16
|
+
* @experimental
|
|
17
|
+
* @default basic
|
|
18
|
+
*/
|
|
19
|
+
bundleStrategy?: "basic" | "nf3";
|
|
20
|
+
/**
|
|
21
|
+
* Override vite build environments
|
|
22
|
+
* @experimental
|
|
23
|
+
*/
|
|
24
|
+
viteEnvNames?: {
|
|
25
|
+
client?: string;
|
|
26
|
+
edge?: string | false;
|
|
27
|
+
node?: string;
|
|
28
|
+
};
|
|
15
29
|
/**
|
|
16
30
|
* How long Functions should be allowed to run for every request, in seconds.
|
|
17
31
|
* If left empty, default value for your plan will be used.
|
package/dist/vite.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import { t as pathRelativeTo$1 } from "./path-B4ThGm96.js";
|
|
2
|
-
import { r as assert, t as createAPI } from "./api-DR2y7JVQ.js";
|
|
2
|
+
import { n as getVercelAPI, r as assert, t as createAPI } from "./api-DR2y7JVQ.js";
|
|
3
|
+
import { builtinModules } from "node:module";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { vercelOutputConfigSchema, vercelOutputPrerenderConfigSchema, vercelOutputVcConfigSchema } from "@vite-plugin-vercel/schemas";
|
|
5
6
|
import { fromRou3 } from "convert-route/rou3";
|
|
6
7
|
import { BuildEnvironment, createRunnableDevEnvironment, mergeConfig, normalizePath } from "vite";
|
|
7
8
|
import { catchAll, devServer } from "@universal-deploy/store/vite";
|
|
8
9
|
import { store } from "@universal-deploy/store";
|
|
9
|
-
import fs from "node:fs";
|
|
10
|
+
import fs, { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { copyFile, cp, rmdir, unlink } from "node:fs/promises";
|
|
12
|
+
import { findRoot } from "@manypkg/find-root";
|
|
13
|
+
import { nodeFileTrace, resolve } from "@vercel/nft";
|
|
14
|
+
import { build } from "rolldown";
|
|
10
15
|
import { cpus } from "node:os";
|
|
11
|
-
import { resolve } from "@vercel/nft";
|
|
12
16
|
import { externals } from "nf3/plugin";
|
|
13
17
|
import pLimit from "p-limit";
|
|
14
|
-
import { build } from "rolldown";
|
|
15
18
|
import { getNodeVersion } from "@vercel/build-utils";
|
|
16
19
|
import { toPathToRegexpV6 } from "convert-route/path-to-regexp-v6";
|
|
17
|
-
import { cp } from "node:fs/promises";
|
|
18
20
|
import { getTransformedRoutes, mergeRoutes, normalizeRoutes } from "@vercel/routing-utils";
|
|
19
21
|
|
|
22
|
+
//#region src/utils/buildEnvs.ts
|
|
23
|
+
function getBuildEnvNames(pluginConfig) {
|
|
24
|
+
return {
|
|
25
|
+
client: pluginConfig.viteEnvNames?.client ?? "vercel_client",
|
|
26
|
+
edge: pluginConfig.viteEnvNames?.edge ?? "vercel_edge",
|
|
27
|
+
node: pluginConfig.viteEnvNames?.node ?? "vercel_node"
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
20
32
|
//#region src/utils/dedupeRoutes.ts
|
|
21
33
|
/**
|
|
22
34
|
* When multiple entries point to the same module, we can deploy them as a single function.
|
|
@@ -66,6 +78,7 @@ function entryDestination(root, entry, postfix) {
|
|
|
66
78
|
//#endregion
|
|
67
79
|
//#region src/plugins/api.ts
|
|
68
80
|
function apiPlugin(pluginConfig) {
|
|
81
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
69
82
|
const outfiles = [];
|
|
70
83
|
return {
|
|
71
84
|
name: "vite-plugin-vercel:api",
|
|
@@ -73,12 +86,12 @@ function apiPlugin(pluginConfig) {
|
|
|
73
86
|
return createAPI(outfiles, pluginConfig);
|
|
74
87
|
},
|
|
75
88
|
applyToEnvironment({ name }) {
|
|
76
|
-
return name ===
|
|
89
|
+
return name === envNames.edge || name === envNames.node;
|
|
77
90
|
},
|
|
78
|
-
writeBundle(_opts, bundle$
|
|
91
|
+
writeBundle(_opts, bundle$2) {
|
|
79
92
|
const root = this.environment.config.root ?? process.cwd();
|
|
80
93
|
const entryMapByDestination = new Map(dedupeRoutes().map((e) => [entryDestination(root, e, ".func/index"), e]));
|
|
81
|
-
for (const [key, value] of Object.entries(bundle$
|
|
94
|
+
for (const [key, value] of Object.entries(bundle$2)) if (value.type === "chunk" && entryMapByDestination.has(removeExtension$1(key))) outfiles.push({
|
|
82
95
|
type: "chunk",
|
|
83
96
|
root: this.environment.config.root,
|
|
84
97
|
outdir: this.environment.config.build.outDir,
|
|
@@ -96,6 +109,17 @@ function apiPlugin(pluginConfig) {
|
|
|
96
109
|
};
|
|
97
110
|
}
|
|
98
111
|
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/utils/edge.ts
|
|
114
|
+
const edgeConditions = [
|
|
115
|
+
"edge-light",
|
|
116
|
+
"worker",
|
|
117
|
+
"browser",
|
|
118
|
+
"module",
|
|
119
|
+
"import",
|
|
120
|
+
"default"
|
|
121
|
+
];
|
|
122
|
+
|
|
99
123
|
//#endregion
|
|
100
124
|
//#region src/utils/external.ts
|
|
101
125
|
const _external = [
|
|
@@ -108,8 +132,211 @@ const _external = [
|
|
|
108
132
|
const edgeExternal = [..._external, ..._external.map((e) => `node:${e}`)];
|
|
109
133
|
|
|
110
134
|
//#endregion
|
|
111
|
-
//#region src/plugins/bundle.ts
|
|
112
|
-
|
|
135
|
+
//#region src/plugins/bundle/basic.ts
|
|
136
|
+
const builtIns = new Set(builtinModules.flatMap((m) => [m, `node:${m}`]));
|
|
137
|
+
const edgeWasmPlugin = {
|
|
138
|
+
name: "edge-wasm-vercel",
|
|
139
|
+
resolveId: {
|
|
140
|
+
filter: { id: [/\.wasm\?module$/] },
|
|
141
|
+
handler(id) {
|
|
142
|
+
return {
|
|
143
|
+
id: id.replace(/\.wasm\?module$/, ".wasm"),
|
|
144
|
+
external: true
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const dynamicNativeImportPlugin = {
|
|
150
|
+
name: "edge-dynamic-import-native",
|
|
151
|
+
resolveDynamicImport(specifier) {
|
|
152
|
+
if (typeof specifier === "string" && builtIns.has(specifier)) return {
|
|
153
|
+
id: specifier,
|
|
154
|
+
external: true
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const reactEdgePlugin$1 = {
|
|
159
|
+
name: "react-edge-plugin",
|
|
160
|
+
resolveId: {
|
|
161
|
+
filter: { id: [/react-dom\/server/] },
|
|
162
|
+
async handler(_id, importer, options) {
|
|
163
|
+
return this.resolve("react-dom/server.edge", importer, options);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
function basicBundlePlugin(pluginConfig) {
|
|
168
|
+
const bundledAssets = /* @__PURE__ */ new Map();
|
|
169
|
+
const bundledChunks = [];
|
|
170
|
+
return [{
|
|
171
|
+
name: "vite-plugin-vercel:bundle",
|
|
172
|
+
enforce: "post",
|
|
173
|
+
apply: "build",
|
|
174
|
+
generateBundle(_opts, bundle$2) {
|
|
175
|
+
for (const b of Object.values(bundle$2)) {
|
|
176
|
+
const outFile = joinAbsolute(this.environment, this.environment.config.build.outDir, b.fileName);
|
|
177
|
+
if (b.type === "asset") {
|
|
178
|
+
const originalFileNames = b.originalFileNames.map((relativePath) => path.resolve(this.environment.config.root, relativePath));
|
|
179
|
+
const asset = {
|
|
180
|
+
env: this.environment.name,
|
|
181
|
+
root: this.environment.config.root,
|
|
182
|
+
outDir: this.environment.config.build.outDir,
|
|
183
|
+
outFile,
|
|
184
|
+
fileName: b.fileName
|
|
185
|
+
};
|
|
186
|
+
for (const originalFileName of originalFileNames) bundledAssets.set(originalFileName, asset);
|
|
187
|
+
} else bundledChunks.push(outFile);
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
closeBundle: {
|
|
191
|
+
order: "post",
|
|
192
|
+
async handler() {
|
|
193
|
+
if (!isVercelLastBuildStep(this.environment, pluginConfig)) return;
|
|
194
|
+
this.environment.logger.info("Creating Vercel bundles...");
|
|
195
|
+
const outfiles = getVercelAPI(this).getOutFiles();
|
|
196
|
+
const filesToKeep = [];
|
|
197
|
+
for (const outfile of outfiles) if (outfile.type === "chunk") filesToKeep.push(...await bundle$1(this, bundledAssets, outfile));
|
|
198
|
+
await cleanup$1(filesToKeep, bundledChunks);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
sharedDuringBuild: true
|
|
202
|
+
}];
|
|
203
|
+
}
|
|
204
|
+
function getAbsoluteOutFile(outfile) {
|
|
205
|
+
const source = joinAbsolutePosix(outfile.root, outfile.outdir, outfile.filepath);
|
|
206
|
+
return {
|
|
207
|
+
source,
|
|
208
|
+
destination: source.replace(outfile.outdir, outfile.outdir)
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
async function bundle$1(pluginContext, bundledAssets, outfile) {
|
|
212
|
+
const output = [];
|
|
213
|
+
const { source, destination } = getAbsoluteOutFile(outfile);
|
|
214
|
+
const isEdge = Boolean(outfile.relatedEntry.vercel?.edge);
|
|
215
|
+
const { environment } = pluginContext;
|
|
216
|
+
const buildOptions = {};
|
|
217
|
+
buildOptions.output = {
|
|
218
|
+
format: "esm",
|
|
219
|
+
legalComments: "none"
|
|
220
|
+
};
|
|
221
|
+
buildOptions.checks = { pluginTimings: false };
|
|
222
|
+
buildOptions.input = [source];
|
|
223
|
+
buildOptions.treeshake = true;
|
|
224
|
+
buildOptions.resolve ??= {};
|
|
225
|
+
if (isEdge) {
|
|
226
|
+
buildOptions.platform = "browser";
|
|
227
|
+
buildOptions.external = edgeExternal;
|
|
228
|
+
buildOptions.resolve.conditionNames = edgeConditions;
|
|
229
|
+
buildOptions.transform = { define: { "process.env.NODE_ENV": JSON.stringify("production") } };
|
|
230
|
+
buildOptions.output.file = destination.replace(/\.mjs$/, ".js");
|
|
231
|
+
buildOptions.plugins = [
|
|
232
|
+
edgeWasmPlugin,
|
|
233
|
+
dynamicNativeImportPlugin,
|
|
234
|
+
reactEdgePlugin$1
|
|
235
|
+
];
|
|
236
|
+
} else {
|
|
237
|
+
buildOptions.platform = "node";
|
|
238
|
+
buildOptions.output.file = destination.replace(/\.js$/, ".mjs");
|
|
239
|
+
buildOptions.output.banner = `import { createRequire as topLevelCreateRequire } from 'node:module';
|
|
240
|
+
import { dirname as topLevelDirname } from 'node:path';
|
|
241
|
+
import { fileURLToPath as topLevelFileURLToPath } from 'node:url';
|
|
242
|
+
var require = topLevelCreateRequire(import.meta.url);
|
|
243
|
+
var __filename = topLevelFileURLToPath(import.meta.url);
|
|
244
|
+
var __dirname = topLevelDirname(__filename);
|
|
245
|
+
`;
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
await build(buildOptions);
|
|
249
|
+
output.push(buildOptions.output.file);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
throw new Error(`Error while bundling ${destination}`, { cause: e });
|
|
252
|
+
}
|
|
253
|
+
let base = environment.config.root;
|
|
254
|
+
try {
|
|
255
|
+
base = (await findRoot(environment.config.root)).rootDir;
|
|
256
|
+
} catch (_e) {}
|
|
257
|
+
const resolvedEntryP = pluginContext.resolve(outfile.relatedEntry.id);
|
|
258
|
+
const entryFilePath = existsSync(outfile.relatedEntry.id) ? outfile.relatedEntry.id : (await resolvedEntryP)?.id && existsSync((await resolvedEntryP).id) ? (await resolvedEntryP).id : null;
|
|
259
|
+
if (entryFilePath === null) return [];
|
|
260
|
+
const { fileList, reasons } = await nodeFileTrace([entryFilePath], {
|
|
261
|
+
base,
|
|
262
|
+
processCwd: environment.config.root,
|
|
263
|
+
mixedModules: true,
|
|
264
|
+
ignore: [
|
|
265
|
+
"**/node_modules/react{,-dom,-dom-server-turbopack}/**/*.development.js",
|
|
266
|
+
"**/*.d.ts",
|
|
267
|
+
"**/*.map",
|
|
268
|
+
"**/node_modules/webpack5/**/*"
|
|
269
|
+
],
|
|
270
|
+
async readFile(filepath) {
|
|
271
|
+
if (filepath.endsWith(".ts") || filepath.endsWith(".tsx")) return (await build({
|
|
272
|
+
output: {
|
|
273
|
+
format: "esm",
|
|
274
|
+
minify: false
|
|
275
|
+
},
|
|
276
|
+
external: /.*/,
|
|
277
|
+
platform: "node",
|
|
278
|
+
logLevel: "info",
|
|
279
|
+
plugins: [],
|
|
280
|
+
transform: { define: {
|
|
281
|
+
"process.env.NODE_ENV": "\"production\"",
|
|
282
|
+
"import.meta.env.NODE_ENV": "\"production\""
|
|
283
|
+
} },
|
|
284
|
+
input: [entryFilePath],
|
|
285
|
+
write: false
|
|
286
|
+
})).output[0].code;
|
|
287
|
+
return readFileSync(filepath, "utf-8");
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
for (const file of fileList) if (reasons.has(file) && reasons.get(file)?.type.includes("asset") && !file.endsWith(".js") && !file.endsWith(".cjs") && !file.endsWith(".mjs") && !file.endsWith("package.json")) {
|
|
291
|
+
const absolutePath = path.join(base, file);
|
|
292
|
+
const assetRenamedByVite = bundledAssets.get(absolutePath);
|
|
293
|
+
const basename = path.basename(assetRenamedByVite ? assetRenamedByVite.fileName : absolutePath);
|
|
294
|
+
if (assetRenamedByVite) writeFileSync(destination, readFileSync(destination, "utf-8").replaceAll(`/${assetRenamedByVite.fileName}`, `./${path.basename(assetRenamedByVite.fileName)}`));
|
|
295
|
+
const to = path.join(path.dirname(destination), basename);
|
|
296
|
+
output.push(to);
|
|
297
|
+
await copyFile(absolutePath, to);
|
|
298
|
+
}
|
|
299
|
+
return output;
|
|
300
|
+
}
|
|
301
|
+
function joinAbsolute(env_or_p0, p1, ...p) {
|
|
302
|
+
if (path.isAbsolute(p1)) return path.join(p1, ...p);
|
|
303
|
+
return path.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
|
|
304
|
+
}
|
|
305
|
+
function joinAbsolutePosix(env_or_p0, p1, ...p) {
|
|
306
|
+
if (path.isAbsolute(p1)) return path.posix.join(p1, ...p);
|
|
307
|
+
return path.posix.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
|
|
308
|
+
}
|
|
309
|
+
function isVercelLastBuildStep(env, pluginConfig) {
|
|
310
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
311
|
+
return (typeof env !== "string" ? env.name : env) === envNames.node;
|
|
312
|
+
}
|
|
313
|
+
async function cleanup$1(filesToKeep, bundledChunks) {
|
|
314
|
+
const toKeep = new Set(filesToKeep);
|
|
315
|
+
const removedFiles = [];
|
|
316
|
+
await Promise.all(bundledChunks.map(async (file) => {
|
|
317
|
+
if (!toKeep.has(file)) try {
|
|
318
|
+
await unlink(file);
|
|
319
|
+
removedFiles.push(file);
|
|
320
|
+
} catch {}
|
|
321
|
+
}));
|
|
322
|
+
const dirsToRemove = /* @__PURE__ */ new Set();
|
|
323
|
+
for (const file of removedFiles) {
|
|
324
|
+
let dir = path.dirname(file);
|
|
325
|
+
while (dir && dir !== "." && dir !== "/" && !toKeep.has(dir)) {
|
|
326
|
+
dirsToRemove.add(dir);
|
|
327
|
+
dir = path.dirname(dir);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const sortedDirs = Array.from(dirsToRemove).sort((a, b) => b.length - a.length);
|
|
331
|
+
for (const dir of sortedDirs) try {
|
|
332
|
+
await rmdir(dir);
|
|
333
|
+
} catch {}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/plugins/bundle/nf3.ts
|
|
338
|
+
function nf3BundlePlugin(pluginConfig) {
|
|
339
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
113
340
|
const externalsPlugin = externals({});
|
|
114
341
|
delete externalsPlugin.buildEnd;
|
|
115
342
|
let buildOutput;
|
|
@@ -125,7 +352,7 @@ function bundlePlugin() {
|
|
|
125
352
|
return env.config.consumer !== "client";
|
|
126
353
|
},
|
|
127
354
|
async writeBundle(_, output) {
|
|
128
|
-
const isEdge = this.environment.name ===
|
|
355
|
+
const isEdge = this.environment.name === envNames.edge;
|
|
129
356
|
const config = this.environment.config;
|
|
130
357
|
const outDir$1 = normalizePath(path.isAbsolute(config.build.outDir) ? config.build.outDir : path.join(config.root, config.build.outDir));
|
|
131
358
|
const entries = Object.entries(output).filter((e) => "isEntry" in e[1] && e[1].isEntry).map((e) => ({
|
|
@@ -133,13 +360,20 @@ function bundlePlugin() {
|
|
|
133
360
|
fileName: e[1].fileName,
|
|
134
361
|
outPath: path.join(outDir$1, e[1].fileName)
|
|
135
362
|
}));
|
|
136
|
-
|
|
363
|
+
if (entries.length === 0) return;
|
|
137
364
|
const outPaths = entries.map((entry) => entry.outPath);
|
|
138
365
|
const input = Object.fromEntries(outPaths.map((e) => [removeExtension(pathRelativeTo(e, outDir$1)), e]));
|
|
139
366
|
const limit = pLimit(Math.max(1, Math.ceil(cpus().length / 2)));
|
|
367
|
+
const nonVitePlugins = this.environment.config.plugins.filter((p) => {
|
|
368
|
+
return !p.name.startsWith("vite:") && p.name !== "alias" && p.name !== "commonjs" && p.name !== "nitro:externals";
|
|
369
|
+
}).map((x) => {
|
|
370
|
+
const { buildStart, buildEnd, writeBundle, generateBundle, ...rest } = x;
|
|
371
|
+
return rest;
|
|
372
|
+
});
|
|
140
373
|
const localOutput = (await Promise.all(Object.values(input).map((entryPath) => limit(async () => {
|
|
141
374
|
const outDir$2 = path.dirname(entryPath);
|
|
142
375
|
return { output: (await bundle({
|
|
376
|
+
plugins: nonVitePlugins,
|
|
143
377
|
isEdge,
|
|
144
378
|
input: { index: entryPath },
|
|
145
379
|
outDir: outDir$2,
|
|
@@ -169,7 +403,7 @@ function bundle(options) {
|
|
|
169
403
|
platform: options.isEdge ? "browser" : "node",
|
|
170
404
|
external: options.isEdge ? edgeExternal : [],
|
|
171
405
|
write: true,
|
|
172
|
-
plugins: [externals(options.externals)],
|
|
406
|
+
plugins: [...options.plugins, externals(options.externals)],
|
|
173
407
|
input: options.input,
|
|
174
408
|
resolve: { conditionNames: options.externals.conditions },
|
|
175
409
|
output: {
|
|
@@ -208,12 +442,13 @@ function removeExtension(subject) {
|
|
|
208
442
|
//#region src/plugins/clean-outdir.ts
|
|
209
443
|
function vercelCleanupPlugin(pluginConfig) {
|
|
210
444
|
let alreadyRun = false;
|
|
445
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
211
446
|
return {
|
|
212
447
|
apply: "build",
|
|
213
448
|
name: "vite-plugin-vercel:cleanup",
|
|
214
449
|
enforce: "pre",
|
|
215
450
|
applyToEnvironment(env) {
|
|
216
|
-
return env.name ===
|
|
451
|
+
return env.name === envNames.client;
|
|
217
452
|
},
|
|
218
453
|
buildStart: {
|
|
219
454
|
order: "pre",
|
|
@@ -255,6 +490,7 @@ function getVcConfig(pluginConfig, filename, options) {
|
|
|
255
490
|
//#region src/plugins/loader.ts
|
|
256
491
|
const re_DUMMY = /* @__PURE__ */ new RegExp(`__DUMMY__$`);
|
|
257
492
|
function loaderPlugin(pluginConfig) {
|
|
493
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
258
494
|
let root;
|
|
259
495
|
return [
|
|
260
496
|
{
|
|
@@ -298,7 +534,7 @@ export default def;`;
|
|
|
298
534
|
name: "vite-plugin-vercel:build-functions",
|
|
299
535
|
apply: "build",
|
|
300
536
|
applyToEnvironment(env) {
|
|
301
|
-
return env.name ===
|
|
537
|
+
return env.name === envNames.node || env.name === envNames.edge;
|
|
302
538
|
},
|
|
303
539
|
config: {
|
|
304
540
|
order: "post",
|
|
@@ -309,8 +545,8 @@ export default def;`;
|
|
|
309
545
|
configEnvironment: {
|
|
310
546
|
order: "post",
|
|
311
547
|
handler(name) {
|
|
312
|
-
const isEdge = name ===
|
|
313
|
-
if (name ===
|
|
548
|
+
const isEdge = name === envNames.edge;
|
|
549
|
+
if (name === envNames.node || isEdge) {
|
|
314
550
|
const entries = dedupeRoutes().filter((e) => (e.vercel?.edge ?? false) === isEdge);
|
|
315
551
|
return { build: { rollupOptions: {
|
|
316
552
|
input: Object.fromEntries(entries.map((e) => [entryDestination(root ?? process.cwd(), e, ".func/index"), isEdge ? `${e.id}?edge` : e.id])),
|
|
@@ -320,17 +556,16 @@ export default def;`;
|
|
|
320
556
|
}
|
|
321
557
|
},
|
|
322
558
|
async buildStart() {
|
|
323
|
-
const isEdge = this.environment.name ===
|
|
559
|
+
const isEdge = this.environment.name === envNames.edge;
|
|
324
560
|
const nodeVersion = await getNodeVersion(process.cwd());
|
|
325
561
|
const entries = dedupeRoutes();
|
|
326
562
|
for (const entry of entries.filter((e) => (e.vercel?.edge ?? false) === isEdge)) {
|
|
327
|
-
const isEdge$1 = this.environment.name === "vercel_edge";
|
|
328
563
|
this.emitFile({
|
|
329
564
|
type: "asset",
|
|
330
565
|
fileName: entryDestination(root ?? process.cwd(), entry, ".func/.vc-config.json"),
|
|
331
|
-
source: JSON.stringify(getVcConfig(pluginConfig, isEdge
|
|
566
|
+
source: JSON.stringify(getVcConfig(pluginConfig, isEdge ? "index.js" : "index.mjs", {
|
|
332
567
|
nodeVersion,
|
|
333
|
-
edge: isEdge
|
|
568
|
+
edge: isEdge,
|
|
334
569
|
streaming: entry.vercel?.streaming
|
|
335
570
|
}), void 0, 2)
|
|
336
571
|
});
|
|
@@ -367,11 +602,12 @@ export default def;`;
|
|
|
367
602
|
|
|
368
603
|
//#endregion
|
|
369
604
|
//#region src/plugins/react-edge.ts
|
|
370
|
-
function reactEdgePlugin() {
|
|
605
|
+
function reactEdgePlugin(pluginConfig) {
|
|
606
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
371
607
|
return {
|
|
372
608
|
name: "vite-plugin-vercel:react-edge",
|
|
373
609
|
applyToEnvironment(env) {
|
|
374
|
-
return env.name ===
|
|
610
|
+
return env.name === envNames.edge;
|
|
375
611
|
},
|
|
376
612
|
resolveId: {
|
|
377
613
|
order: "pre",
|
|
@@ -435,23 +671,13 @@ function getConfig(pluginConfig) {
|
|
|
435
671
|
//#region src/utils/const.ts
|
|
436
672
|
const virtualEntry = "virtual:vite-plugin-vercel:entry";
|
|
437
673
|
|
|
438
|
-
//#endregion
|
|
439
|
-
//#region src/utils/edge.ts
|
|
440
|
-
const edgeConditions = [
|
|
441
|
-
"edge-light",
|
|
442
|
-
"worker",
|
|
443
|
-
"browser",
|
|
444
|
-
"module",
|
|
445
|
-
"import",
|
|
446
|
-
"default"
|
|
447
|
-
];
|
|
448
|
-
|
|
449
674
|
//#endregion
|
|
450
675
|
//#region src/plugins/setupEnvs.ts
|
|
451
676
|
const outDir = path.posix.join(process.cwd(), ".vercel/output");
|
|
452
677
|
const DUMMY = "__DUMMY__";
|
|
453
678
|
let injected = false;
|
|
454
679
|
function setupEnvs(pluginConfig) {
|
|
680
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
455
681
|
return [
|
|
456
682
|
{
|
|
457
683
|
name: "vite-plugin-vercel:setup-envs",
|
|
@@ -459,45 +685,48 @@ function setupEnvs(pluginConfig) {
|
|
|
459
685
|
order: "post",
|
|
460
686
|
async handler(builder) {
|
|
461
687
|
try {
|
|
462
|
-
await builder.build(builder.environments.
|
|
688
|
+
await builder.build(builder.environments[envNames.client]);
|
|
463
689
|
} catch (e) {
|
|
464
690
|
if (e instanceof Error && e.message.includes(`Could not resolve entry module "index.html"`)) {} else throw e;
|
|
465
691
|
}
|
|
466
|
-
await builder.build(builder.environments.
|
|
467
|
-
await builder.build(builder.environments.
|
|
692
|
+
if (envNames.edge !== false) await builder.build(builder.environments[envNames.edge]);
|
|
693
|
+
await builder.build(builder.environments[envNames.node]);
|
|
468
694
|
}
|
|
469
695
|
},
|
|
470
|
-
config
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
if (
|
|
696
|
+
config: {
|
|
697
|
+
order: "post",
|
|
698
|
+
handler() {
|
|
699
|
+
if (!injected) {
|
|
700
|
+
injected = true;
|
|
701
|
+
if (pluginConfig.entries) store.entries.push(...pluginConfig.entries);
|
|
702
|
+
}
|
|
703
|
+
const outDirOverride = pluginConfig.outDir ? { build: { outDir: pluginConfig.outDir } } : {};
|
|
704
|
+
const environments = {};
|
|
705
|
+
if (envNames.client) environments[envNames.client] = {
|
|
706
|
+
build: {
|
|
707
|
+
outDir: path.join(pluginConfig.outDir ?? outDir, "static"),
|
|
708
|
+
copyPublicDir: true,
|
|
709
|
+
rollupOptions: { input: getDummyInput() }
|
|
710
|
+
},
|
|
711
|
+
consumer: "client"
|
|
712
|
+
};
|
|
713
|
+
if (envNames.edge) environments[envNames.edge] = createVercelEnvironmentOptions(outDirOverride);
|
|
714
|
+
if (envNames.node) environments[envNames.node] = createVercelEnvironmentOptions(outDirOverride);
|
|
715
|
+
return {
|
|
716
|
+
environments,
|
|
717
|
+
builder: {}
|
|
718
|
+
};
|
|
474
719
|
}
|
|
475
|
-
const outDirOverride = pluginConfig.outDir ? { build: { outDir: pluginConfig.outDir } } : {};
|
|
476
|
-
return {
|
|
477
|
-
environments: {
|
|
478
|
-
vercel_edge: createVercelEnvironmentOptions(outDirOverride),
|
|
479
|
-
vercel_node: createVercelEnvironmentOptions(outDirOverride),
|
|
480
|
-
vercel_client: {
|
|
481
|
-
build: {
|
|
482
|
-
outDir: path.join(pluginConfig.outDir ?? outDir, "static"),
|
|
483
|
-
copyPublicDir: true,
|
|
484
|
-
rollupOptions: { input: getDummyInput() }
|
|
485
|
-
},
|
|
486
|
-
consumer: "client"
|
|
487
|
-
}
|
|
488
|
-
},
|
|
489
|
-
builder: {}
|
|
490
|
-
};
|
|
491
720
|
},
|
|
492
721
|
sharedDuringBuild: true
|
|
493
722
|
},
|
|
494
723
|
{
|
|
495
724
|
name: "vite-plugin-vercel:setup-envs:vercel_edge",
|
|
496
725
|
applyToEnvironment(env) {
|
|
497
|
-
return env.name ===
|
|
726
|
+
return env.name === envNames.edge;
|
|
498
727
|
},
|
|
499
728
|
configEnvironment(name, config, env) {
|
|
500
|
-
if (name !==
|
|
729
|
+
if (name !== envNames.edge) return;
|
|
501
730
|
return {
|
|
502
731
|
resolve: {
|
|
503
732
|
external: edgeExternal,
|
|
@@ -524,8 +753,8 @@ function setupEnvs(pluginConfig) {
|
|
|
524
753
|
},
|
|
525
754
|
generateBundle: {
|
|
526
755
|
order: "post",
|
|
527
|
-
async handler(_opts, bundle$
|
|
528
|
-
cleanupDummy(bundle$
|
|
756
|
+
async handler(_opts, bundle$2) {
|
|
757
|
+
cleanupDummy(bundle$2);
|
|
529
758
|
}
|
|
530
759
|
},
|
|
531
760
|
sharedDuringBuild: true
|
|
@@ -533,16 +762,16 @@ function setupEnvs(pluginConfig) {
|
|
|
533
762
|
{
|
|
534
763
|
name: "vite-plugin-vercel:setup-envs:vercel_node",
|
|
535
764
|
applyToEnvironment(env) {
|
|
536
|
-
return env.name ===
|
|
765
|
+
return env.name === envNames.node;
|
|
537
766
|
},
|
|
538
767
|
configEnvironment(name, config) {
|
|
539
|
-
if (name !==
|
|
768
|
+
if (name !== envNames.node) return;
|
|
540
769
|
return { optimizeDeps: { ...config.optimizeDeps } };
|
|
541
770
|
},
|
|
542
771
|
generateBundle: {
|
|
543
772
|
order: "post",
|
|
544
|
-
async handler(_opts, bundle$
|
|
545
|
-
cleanupDummy(bundle$
|
|
773
|
+
async handler(_opts, bundle$2) {
|
|
774
|
+
cleanupDummy(bundle$2);
|
|
546
775
|
this.emitFile({
|
|
547
776
|
type: "asset",
|
|
548
777
|
fileName: "config.json",
|
|
@@ -555,10 +784,10 @@ function setupEnvs(pluginConfig) {
|
|
|
555
784
|
{
|
|
556
785
|
name: "vite-plugin-vercel:setup-envs:vercel_client",
|
|
557
786
|
applyToEnvironment(env) {
|
|
558
|
-
return env.name ===
|
|
787
|
+
return env.name === envNames.client;
|
|
559
788
|
},
|
|
560
|
-
generateBundle: { async handler(_opts, bundle$
|
|
561
|
-
cleanupDummy(bundle$
|
|
789
|
+
generateBundle: { async handler(_opts, bundle$2) {
|
|
790
|
+
cleanupDummy(bundle$2);
|
|
562
791
|
const topLevelConfig = this.environment.getTopLevelConfig();
|
|
563
792
|
const clientEnv = topLevelConfig.environments.client;
|
|
564
793
|
if (clientEnv) try {
|
|
@@ -604,21 +833,21 @@ function createVercelEnvironmentOptions(overrides) {
|
|
|
604
833
|
function getDummyInput() {
|
|
605
834
|
return { [DUMMY]: `${virtualEntry}:${DUMMY}` };
|
|
606
835
|
}
|
|
607
|
-
function cleanupDummy(bundle$
|
|
608
|
-
const dummy = Object.keys(bundle$
|
|
609
|
-
if (dummy) delete bundle$
|
|
836
|
+
function cleanupDummy(bundle$2) {
|
|
837
|
+
const dummy = Object.keys(bundle$2).find((key) => key.includes("_DUMMY_"));
|
|
838
|
+
if (dummy) delete bundle$2[dummy];
|
|
610
839
|
}
|
|
611
840
|
|
|
612
841
|
//#endregion
|
|
613
842
|
//#region src/plugins/index.ts
|
|
614
843
|
function vercel(pluginConfig = {}) {
|
|
615
844
|
return [
|
|
616
|
-
reactEdgePlugin(),
|
|
617
|
-
vercelCleanupPlugin(),
|
|
845
|
+
reactEdgePlugin(pluginConfig),
|
|
846
|
+
vercelCleanupPlugin(pluginConfig),
|
|
618
847
|
apiPlugin(pluginConfig),
|
|
619
848
|
...setupEnvs(pluginConfig),
|
|
620
849
|
...loaderPlugin(pluginConfig),
|
|
621
|
-
...
|
|
850
|
+
...pluginConfig?.bundleStrategy === "nf3" ? nf3BundlePlugin(pluginConfig) : basicBundlePlugin(pluginConfig),
|
|
622
851
|
catchAll(),
|
|
623
852
|
devServer()
|
|
624
853
|
];
|