vite-plugin-vercel 11.0.0-beta.3 → 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 +9 -0
- package/dist/vite.js +56 -40
- package/package.json +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -17,6 +17,15 @@ interface ViteVercelConfig {
|
|
|
17
17
|
* @default basic
|
|
18
18
|
*/
|
|
19
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
|
+
};
|
|
20
29
|
/**
|
|
21
30
|
* How long Functions should be allowed to run for every request, in seconds.
|
|
22
31
|
* If left empty, default value for your plan will be used.
|
package/dist/vite.js
CHANGED
|
@@ -19,6 +19,16 @@ import { getNodeVersion } from "@vercel/build-utils";
|
|
|
19
19
|
import { toPathToRegexpV6 } from "convert-route/path-to-regexp-v6";
|
|
20
20
|
import { getTransformedRoutes, mergeRoutes, normalizeRoutes } from "@vercel/routing-utils";
|
|
21
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
|
|
22
32
|
//#region src/utils/dedupeRoutes.ts
|
|
23
33
|
/**
|
|
24
34
|
* When multiple entries point to the same module, we can deploy them as a single function.
|
|
@@ -68,6 +78,7 @@ function entryDestination(root, entry, postfix) {
|
|
|
68
78
|
//#endregion
|
|
69
79
|
//#region src/plugins/api.ts
|
|
70
80
|
function apiPlugin(pluginConfig) {
|
|
81
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
71
82
|
const outfiles = [];
|
|
72
83
|
return {
|
|
73
84
|
name: "vite-plugin-vercel:api",
|
|
@@ -75,7 +86,7 @@ function apiPlugin(pluginConfig) {
|
|
|
75
86
|
return createAPI(outfiles, pluginConfig);
|
|
76
87
|
},
|
|
77
88
|
applyToEnvironment({ name }) {
|
|
78
|
-
return name ===
|
|
89
|
+
return name === envNames.edge || name === envNames.node;
|
|
79
90
|
},
|
|
80
91
|
writeBundle(_opts, bundle$2) {
|
|
81
92
|
const root = this.environment.config.root ?? process.cwd();
|
|
@@ -153,7 +164,7 @@ const reactEdgePlugin$1 = {
|
|
|
153
164
|
}
|
|
154
165
|
}
|
|
155
166
|
};
|
|
156
|
-
function basicBundlePlugin() {
|
|
167
|
+
function basicBundlePlugin(pluginConfig) {
|
|
157
168
|
const bundledAssets = /* @__PURE__ */ new Map();
|
|
158
169
|
const bundledChunks = [];
|
|
159
170
|
return [{
|
|
@@ -179,7 +190,7 @@ function basicBundlePlugin() {
|
|
|
179
190
|
closeBundle: {
|
|
180
191
|
order: "post",
|
|
181
192
|
async handler() {
|
|
182
|
-
if (!isVercelLastBuildStep(this.environment)) return;
|
|
193
|
+
if (!isVercelLastBuildStep(this.environment, pluginConfig)) return;
|
|
183
194
|
this.environment.logger.info("Creating Vercel bundles...");
|
|
184
195
|
const outfiles = getVercelAPI(this).getOutFiles();
|
|
185
196
|
const filesToKeep = [];
|
|
@@ -295,8 +306,9 @@ function joinAbsolutePosix(env_or_p0, p1, ...p) {
|
|
|
295
306
|
if (path.isAbsolute(p1)) return path.posix.join(p1, ...p);
|
|
296
307
|
return path.posix.join(typeof env_or_p0 === "string" ? env_or_p0 : env_or_p0.config.root, p1, ...p);
|
|
297
308
|
}
|
|
298
|
-
function isVercelLastBuildStep(env) {
|
|
299
|
-
|
|
309
|
+
function isVercelLastBuildStep(env, pluginConfig) {
|
|
310
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
311
|
+
return (typeof env !== "string" ? env.name : env) === envNames.node;
|
|
300
312
|
}
|
|
301
313
|
async function cleanup$1(filesToKeep, bundledChunks) {
|
|
302
314
|
const toKeep = new Set(filesToKeep);
|
|
@@ -323,7 +335,8 @@ async function cleanup$1(filesToKeep, bundledChunks) {
|
|
|
323
335
|
|
|
324
336
|
//#endregion
|
|
325
337
|
//#region src/plugins/bundle/nf3.ts
|
|
326
|
-
function nf3BundlePlugin() {
|
|
338
|
+
function nf3BundlePlugin(pluginConfig) {
|
|
339
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
327
340
|
const externalsPlugin = externals({});
|
|
328
341
|
delete externalsPlugin.buildEnd;
|
|
329
342
|
let buildOutput;
|
|
@@ -339,7 +352,7 @@ function nf3BundlePlugin() {
|
|
|
339
352
|
return env.config.consumer !== "client";
|
|
340
353
|
},
|
|
341
354
|
async writeBundle(_, output) {
|
|
342
|
-
const isEdge = this.environment.name ===
|
|
355
|
+
const isEdge = this.environment.name === envNames.edge;
|
|
343
356
|
const config = this.environment.config;
|
|
344
357
|
const outDir$1 = normalizePath(path.isAbsolute(config.build.outDir) ? config.build.outDir : path.join(config.root, config.build.outDir));
|
|
345
358
|
const entries = Object.entries(output).filter((e) => "isEntry" in e[1] && e[1].isEntry).map((e) => ({
|
|
@@ -429,12 +442,13 @@ function removeExtension(subject) {
|
|
|
429
442
|
//#region src/plugins/clean-outdir.ts
|
|
430
443
|
function vercelCleanupPlugin(pluginConfig) {
|
|
431
444
|
let alreadyRun = false;
|
|
445
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
432
446
|
return {
|
|
433
447
|
apply: "build",
|
|
434
448
|
name: "vite-plugin-vercel:cleanup",
|
|
435
449
|
enforce: "pre",
|
|
436
450
|
applyToEnvironment(env) {
|
|
437
|
-
return env.name ===
|
|
451
|
+
return env.name === envNames.client;
|
|
438
452
|
},
|
|
439
453
|
buildStart: {
|
|
440
454
|
order: "pre",
|
|
@@ -476,6 +490,7 @@ function getVcConfig(pluginConfig, filename, options) {
|
|
|
476
490
|
//#region src/plugins/loader.ts
|
|
477
491
|
const re_DUMMY = /* @__PURE__ */ new RegExp(`__DUMMY__$`);
|
|
478
492
|
function loaderPlugin(pluginConfig) {
|
|
493
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
479
494
|
let root;
|
|
480
495
|
return [
|
|
481
496
|
{
|
|
@@ -519,7 +534,7 @@ export default def;`;
|
|
|
519
534
|
name: "vite-plugin-vercel:build-functions",
|
|
520
535
|
apply: "build",
|
|
521
536
|
applyToEnvironment(env) {
|
|
522
|
-
return env.name ===
|
|
537
|
+
return env.name === envNames.node || env.name === envNames.edge;
|
|
523
538
|
},
|
|
524
539
|
config: {
|
|
525
540
|
order: "post",
|
|
@@ -530,8 +545,8 @@ export default def;`;
|
|
|
530
545
|
configEnvironment: {
|
|
531
546
|
order: "post",
|
|
532
547
|
handler(name) {
|
|
533
|
-
const isEdge = name ===
|
|
534
|
-
if (name ===
|
|
548
|
+
const isEdge = name === envNames.edge;
|
|
549
|
+
if (name === envNames.node || isEdge) {
|
|
535
550
|
const entries = dedupeRoutes().filter((e) => (e.vercel?.edge ?? false) === isEdge);
|
|
536
551
|
return { build: { rollupOptions: {
|
|
537
552
|
input: Object.fromEntries(entries.map((e) => [entryDestination(root ?? process.cwd(), e, ".func/index"), isEdge ? `${e.id}?edge` : e.id])),
|
|
@@ -541,17 +556,16 @@ export default def;`;
|
|
|
541
556
|
}
|
|
542
557
|
},
|
|
543
558
|
async buildStart() {
|
|
544
|
-
const isEdge = this.environment.name ===
|
|
559
|
+
const isEdge = this.environment.name === envNames.edge;
|
|
545
560
|
const nodeVersion = await getNodeVersion(process.cwd());
|
|
546
561
|
const entries = dedupeRoutes();
|
|
547
562
|
for (const entry of entries.filter((e) => (e.vercel?.edge ?? false) === isEdge)) {
|
|
548
|
-
const isEdge$1 = this.environment.name === "vercel_edge";
|
|
549
563
|
this.emitFile({
|
|
550
564
|
type: "asset",
|
|
551
565
|
fileName: entryDestination(root ?? process.cwd(), entry, ".func/.vc-config.json"),
|
|
552
|
-
source: JSON.stringify(getVcConfig(pluginConfig, isEdge
|
|
566
|
+
source: JSON.stringify(getVcConfig(pluginConfig, isEdge ? "index.js" : "index.mjs", {
|
|
553
567
|
nodeVersion,
|
|
554
|
-
edge: isEdge
|
|
568
|
+
edge: isEdge,
|
|
555
569
|
streaming: entry.vercel?.streaming
|
|
556
570
|
}), void 0, 2)
|
|
557
571
|
});
|
|
@@ -588,11 +602,12 @@ export default def;`;
|
|
|
588
602
|
|
|
589
603
|
//#endregion
|
|
590
604
|
//#region src/plugins/react-edge.ts
|
|
591
|
-
function reactEdgePlugin() {
|
|
605
|
+
function reactEdgePlugin(pluginConfig) {
|
|
606
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
592
607
|
return {
|
|
593
608
|
name: "vite-plugin-vercel:react-edge",
|
|
594
609
|
applyToEnvironment(env) {
|
|
595
|
-
return env.name ===
|
|
610
|
+
return env.name === envNames.edge;
|
|
596
611
|
},
|
|
597
612
|
resolveId: {
|
|
598
613
|
order: "pre",
|
|
@@ -662,6 +677,7 @@ const outDir = path.posix.join(process.cwd(), ".vercel/output");
|
|
|
662
677
|
const DUMMY = "__DUMMY__";
|
|
663
678
|
let injected = false;
|
|
664
679
|
function setupEnvs(pluginConfig) {
|
|
680
|
+
const envNames = getBuildEnvNames(pluginConfig);
|
|
665
681
|
return [
|
|
666
682
|
{
|
|
667
683
|
name: "vite-plugin-vercel:setup-envs",
|
|
@@ -669,12 +685,12 @@ function setupEnvs(pluginConfig) {
|
|
|
669
685
|
order: "post",
|
|
670
686
|
async handler(builder) {
|
|
671
687
|
try {
|
|
672
|
-
await builder.build(builder.environments.
|
|
688
|
+
await builder.build(builder.environments[envNames.client]);
|
|
673
689
|
} catch (e) {
|
|
674
690
|
if (e instanceof Error && e.message.includes(`Could not resolve entry module "index.html"`)) {} else throw e;
|
|
675
691
|
}
|
|
676
|
-
await builder.build(builder.environments.
|
|
677
|
-
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]);
|
|
678
694
|
}
|
|
679
695
|
},
|
|
680
696
|
config: {
|
|
@@ -685,19 +701,19 @@ function setupEnvs(pluginConfig) {
|
|
|
685
701
|
if (pluginConfig.entries) store.entries.push(...pluginConfig.entries);
|
|
686
702
|
}
|
|
687
703
|
const outDirOverride = pluginConfig.outDir ? { build: { outDir: pluginConfig.outDir } } : {};
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
outDir: path.join(pluginConfig.outDir ?? outDir, "static"),
|
|
695
|
-
copyPublicDir: true,
|
|
696
|
-
rollupOptions: { input: getDummyInput() }
|
|
697
|
-
},
|
|
698
|
-
consumer: "client"
|
|
699
|
-
}
|
|
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() }
|
|
700
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,
|
|
701
717
|
builder: {}
|
|
702
718
|
};
|
|
703
719
|
}
|
|
@@ -707,10 +723,10 @@ function setupEnvs(pluginConfig) {
|
|
|
707
723
|
{
|
|
708
724
|
name: "vite-plugin-vercel:setup-envs:vercel_edge",
|
|
709
725
|
applyToEnvironment(env) {
|
|
710
|
-
return env.name ===
|
|
726
|
+
return env.name === envNames.edge;
|
|
711
727
|
},
|
|
712
728
|
configEnvironment(name, config, env) {
|
|
713
|
-
if (name !==
|
|
729
|
+
if (name !== envNames.edge) return;
|
|
714
730
|
return {
|
|
715
731
|
resolve: {
|
|
716
732
|
external: edgeExternal,
|
|
@@ -746,10 +762,10 @@ function setupEnvs(pluginConfig) {
|
|
|
746
762
|
{
|
|
747
763
|
name: "vite-plugin-vercel:setup-envs:vercel_node",
|
|
748
764
|
applyToEnvironment(env) {
|
|
749
|
-
return env.name ===
|
|
765
|
+
return env.name === envNames.node;
|
|
750
766
|
},
|
|
751
767
|
configEnvironment(name, config) {
|
|
752
|
-
if (name !==
|
|
768
|
+
if (name !== envNames.node) return;
|
|
753
769
|
return { optimizeDeps: { ...config.optimizeDeps } };
|
|
754
770
|
},
|
|
755
771
|
generateBundle: {
|
|
@@ -768,7 +784,7 @@ function setupEnvs(pluginConfig) {
|
|
|
768
784
|
{
|
|
769
785
|
name: "vite-plugin-vercel:setup-envs:vercel_client",
|
|
770
786
|
applyToEnvironment(env) {
|
|
771
|
-
return env.name ===
|
|
787
|
+
return env.name === envNames.client;
|
|
772
788
|
},
|
|
773
789
|
generateBundle: { async handler(_opts, bundle$2) {
|
|
774
790
|
cleanupDummy(bundle$2);
|
|
@@ -826,12 +842,12 @@ function cleanupDummy(bundle$2) {
|
|
|
826
842
|
//#region src/plugins/index.ts
|
|
827
843
|
function vercel(pluginConfig = {}) {
|
|
828
844
|
return [
|
|
829
|
-
reactEdgePlugin(),
|
|
830
|
-
vercelCleanupPlugin(),
|
|
845
|
+
reactEdgePlugin(pluginConfig),
|
|
846
|
+
vercelCleanupPlugin(pluginConfig),
|
|
831
847
|
apiPlugin(pluginConfig),
|
|
832
848
|
...setupEnvs(pluginConfig),
|
|
833
849
|
...loaderPlugin(pluginConfig),
|
|
834
|
-
...pluginConfig?.bundleStrategy === "nf3" ? nf3BundlePlugin() : basicBundlePlugin(),
|
|
850
|
+
...pluginConfig?.bundleStrategy === "nf3" ? nf3BundlePlugin(pluginConfig) : basicBundlePlugin(pluginConfig),
|
|
835
851
|
catchAll(),
|
|
836
852
|
devServer()
|
|
837
853
|
];
|