vite-plugin-vercel 10.0.0 → 11.0.0-beta.10
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 +52 -98
- package/dist/api-DR2y7JVQ.js +70 -0
- package/dist/api.d.ts +53 -0
- package/dist/api.js +3 -0
- package/dist/index.d.ts +35 -691
- package/dist/index.js +57 -789
- package/dist/path-B4ThGm96.js +10 -0
- package/dist/types.d.ts +117 -0
- package/dist/types.js +1 -0
- package/dist/vite.d.ts +9 -0
- package/dist/vite.js +949 -0
- package/meta.d.ts +7 -0
- package/package.json +32 -35
- package/dist/index.cjs +0 -829
- package/dist/index.d.cts +0 -694
- package/index.d.ts +0 -10
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# vite-plugin-vercel
|
|
2
2
|
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> You are on the [Vite Environment API](https://vite.dev/guide/api-environment.html#environment-configuration) beta branch (v11). Check out [v9 branch](https://github.com/magne4000/vite-plugin-vercel/tree/v9) for current stable version.
|
|
5
|
+
|
|
3
6
|
Vercel adapter for [Vite](https://vitejs.dev/).
|
|
4
7
|
|
|
5
8
|
Bundle your Vite application as supported by [Vercel Output API (v3)](https://vercel.com/docs/build-output-api/v3).
|
|
@@ -25,12 +28,12 @@ bun add -D vite-plugin-vercel
|
|
|
25
28
|
## Features
|
|
26
29
|
|
|
27
30
|
- [x] [SSG/Static files](https://vercel.com/docs/build-output-api/v3/primitives#static-files)
|
|
28
|
-
|
|
31
|
+
- see [`prerender` config](/packages/vercel/src/types.ts#L37)
|
|
29
32
|
- [x] [SSR/Serverless functions](https://vercel.com/docs/build-output-api/v3/primitives#serverless-functions)
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
- `.[jt]s` files under the `<root>/api` folder of your project are automatically bundled as Serverless functions under `.vercel/output/functions/api/*.func`
|
|
34
|
+
- see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L62)
|
|
32
35
|
- [x] [ISR/Prerender functions](https://vercel.com/docs/build-output-api/v3/primitives#prerender-functions)
|
|
33
|
-
|
|
36
|
+
- see [`isr` config](/packages/vercel/src/types.ts#L89). Also see implementation of [vike](/packages/vike-integration/vike.ts) for example
|
|
34
37
|
- [x] [Edge functions](https://vercel.com/docs/build-output-api/v3/primitives#edge-functions)
|
|
35
38
|
- [x] [Edge middleware](https://vercel.com/docs/functions/edge-middleware/middleware-api)
|
|
36
39
|
- [ ] [Images optimization](https://vercel.com/docs/build-output-api/v3/configuration#images)
|
|
@@ -45,27 +48,32 @@ Install this package as a dev dependency and add it to your Vite config:
|
|
|
45
48
|
// vite.config.ts
|
|
46
49
|
import { defineConfig } from 'vite';
|
|
47
50
|
import vercel from 'vite-plugin-vercel';
|
|
51
|
+
import { getVercelEntries } from "vite-plugin-vercel";
|
|
52
|
+
|
|
53
|
+
const entries = await getVercelEntries("endpoints/api", {
|
|
54
|
+
// Auto mapping examples:
|
|
55
|
+
// endpoints/api/page.ts -> /api/page
|
|
56
|
+
// endpoints/api/name/[name].ts -> /api/name/*
|
|
57
|
+
destination: "api",
|
|
58
|
+
});
|
|
48
59
|
|
|
49
60
|
export default defineConfig({
|
|
50
|
-
plugins: [vercel(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
61
|
+
plugins: [vercel({
|
|
62
|
+
entries,
|
|
63
|
+
})],
|
|
54
64
|
});
|
|
55
65
|
```
|
|
56
66
|
|
|
57
67
|
> [!NOTE]
|
|
58
|
-
>
|
|
59
|
-
>
|
|
60
|
-
> with no way to disable it, thus avoiding double compilation and unexpected behaviour.
|
|
68
|
+
> `@vercel/build` forces the building of files in the _/api_ folder, with no way to disable this behavior.
|
|
69
|
+
> It's recommended to place your files in a different folder.
|
|
61
70
|
|
|
62
71
|
### Configure endpoints
|
|
63
72
|
|
|
64
|
-
Endpoints
|
|
65
|
-
by exporting values from the endpoint file:
|
|
73
|
+
Endpoints added via `getVercelEntries` can be configured by exporting values from the endpoint file:
|
|
66
74
|
|
|
67
75
|
```ts
|
|
68
|
-
// file:
|
|
76
|
+
// file: endpoints/api/endpoint.ts
|
|
69
77
|
|
|
70
78
|
// Should run on edge runtime
|
|
71
79
|
export const edge = true;
|
|
@@ -90,53 +98,11 @@ export default async function handler() {
|
|
|
90
98
|
}
|
|
91
99
|
```
|
|
92
100
|
|
|
93
|
-
> [!NOTE]
|
|
94
|
-
> Please create an issue if you need other per-endpoints configurations
|
|
95
|
-
|
|
96
101
|
### Edge middleware
|
|
97
102
|
|
|
98
103
|
You can use [Edge middleware as describe in the official documentation](https://vercel.com/docs/functions/edge-middleware/middleware-api) (i.e. with a `middleware.ts` file at the root of your project).
|
|
99
104
|
|
|
100
|
-
##
|
|
101
|
-
|
|
102
|
-
[Vike](https://vike.dev/) is supported through [@vite-plugin-vercel/vike](/packages/vike-integration/README.md) plugin.
|
|
103
|
-
|
|
104
|
-
You only need to install `@vite-plugin-vercel/vike`, the Vite config stays the same as above.
|
|
105
|
-
|
|
106
|
-
You can then leverage [config files](https://vike.dev/config) to customize your endpoints:
|
|
107
|
-
|
|
108
|
-
```ts
|
|
109
|
-
// /pages/product/+config.ts
|
|
110
|
-
|
|
111
|
-
import Page from './Page';
|
|
112
|
-
import type { Config } from 'vike/types';
|
|
113
|
-
|
|
114
|
-
export default {
|
|
115
|
-
// Customize ISR config for this page
|
|
116
|
-
isr: { expiration: 15 },
|
|
117
|
-
// Target Edge instead of Serverless
|
|
118
|
-
edge: true,
|
|
119
|
-
// append headers to all responses
|
|
120
|
-
headers: {
|
|
121
|
-
'X-Header': 'value'
|
|
122
|
-
}
|
|
123
|
-
} satisfies Config;
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
You will also need to extend the [renderer config](https://vike.dev/config#renderer) so that `vike` is aware of the new parameter:
|
|
127
|
-
|
|
128
|
-
```ts
|
|
129
|
-
// /renderer/+config.ts
|
|
130
|
-
|
|
131
|
-
import config from '@vite-plugin-vercel/vike/config';
|
|
132
|
-
import type { Config } from 'vike/types';
|
|
133
|
-
|
|
134
|
-
export default {
|
|
135
|
-
extends: config,
|
|
136
|
-
} satisfies Config;
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Advanced usage
|
|
105
|
+
## Advanced settings
|
|
140
106
|
|
|
141
107
|
```ts
|
|
142
108
|
// vite.config.ts
|
|
@@ -144,8 +110,7 @@ import { defineConfig } from 'vite';
|
|
|
144
110
|
import vercel from 'vite-plugin-vercel';
|
|
145
111
|
|
|
146
112
|
export default defineConfig({
|
|
147
|
-
plugins: [vercel(
|
|
148
|
-
vercel: {
|
|
113
|
+
plugins: [vercel({
|
|
149
114
|
// All the followings optional
|
|
150
115
|
|
|
151
116
|
/**
|
|
@@ -162,14 +127,7 @@ export default defineConfig({
|
|
|
162
127
|
* Defaults to 86400 seconds (24h).
|
|
163
128
|
*/
|
|
164
129
|
expiration: 86400,
|
|
165
|
-
|
|
166
|
-
* Also known as Server Side Generation, or SSG.
|
|
167
|
-
* If present, this function is responsible to create static files in `.vercel/output/static`.
|
|
168
|
-
* Defaults to `false`, which disables prerendering.
|
|
169
|
-
*/
|
|
170
|
-
prerender(resolvedConfig) {
|
|
171
|
-
// Check `/packages/vike/vike.ts` `prerender` for an example
|
|
172
|
-
},
|
|
130
|
+
|
|
173
131
|
/**
|
|
174
132
|
* See https://vercel.com/docs/projects/project-configuration#rewrites
|
|
175
133
|
*/
|
|
@@ -203,19 +161,16 @@ export default defineConfig({
|
|
|
203
161
|
*/
|
|
204
162
|
trailingSlash: true,
|
|
205
163
|
/**
|
|
206
|
-
*
|
|
207
|
-
* If
|
|
208
|
-
* For instance, a framework can leverage this to have a generic ssr endpoint
|
|
209
|
-
* without requiring the user to write any code.
|
|
164
|
+
* Use `getVercelEntries` for mapping your filesystem routes to entries.
|
|
165
|
+
* If you are interfacing this plugin with a framework, entries can also be added through the {@link https://github.com/photon-js/universal-deploy | universal-deploy} API
|
|
210
166
|
*/
|
|
211
|
-
|
|
212
|
-
{
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
],
|
|
167
|
+
entries: {
|
|
168
|
+
root: {
|
|
169
|
+
id: 'src/routes/root.ts',
|
|
170
|
+
name: 'root',
|
|
171
|
+
route: '/'
|
|
172
|
+
}
|
|
173
|
+
},
|
|
219
174
|
/**
|
|
220
175
|
* Advanced configuration to override .vercel/output/config.json
|
|
221
176
|
* See https://vercel.com/docs/build-output-api/v3/configuration#configuration
|
|
@@ -228,32 +183,31 @@ export default defineConfig({
|
|
|
228
183
|
// cache?: string[];
|
|
229
184
|
// crons?: CronsConfig;
|
|
230
185
|
},
|
|
231
|
-
/**
|
|
232
|
-
* ISR and SSG pages are mutually exclusive. If a page is found in both, ISR prevails.
|
|
233
|
-
* Keys are path relative to .vercel/output/functions directory, either without extension,
|
|
234
|
-
* or with `.prerender-config.json` extension.
|
|
235
|
-
* If you have multiple isr configurations pointing to the same underlying function, you can leverage the `symlink`
|
|
236
|
-
* property.
|
|
237
|
-
*
|
|
238
|
-
* Can be an object or a function returning an object (or a Promise of an object).
|
|
239
|
-
*
|
|
240
|
-
* Check `/packages/vike/vike.ts` `vitePluginVercelVpsIsrPlugin` for advanced usage.
|
|
241
|
-
*/
|
|
242
|
-
isr: {
|
|
243
|
-
// `symlink: 'ssr_'` means that a function is available under `.vercel/output/functions/ssr_.func`
|
|
244
|
-
'/pages/a': { expiration: 15, symlink: 'ssr_', route: '^/a/.*$' },
|
|
245
|
-
'/pages/b/c': { expiration: 15, symlink: 'ssr_', route: '^/b/c/.*$' },
|
|
246
|
-
'/pages/d': { expiration: 15, symlink: 'ssr_', route: '^/d$' },
|
|
247
|
-
'/pages/e': { expiration: 25 },
|
|
248
|
-
},
|
|
249
186
|
/**
|
|
250
187
|
* Defaults to `.vercel/output`. Mostly useful for testing purpose
|
|
251
188
|
*/
|
|
252
189
|
outDir: '.vercel/output',
|
|
253
|
-
}
|
|
190
|
+
})]
|
|
254
191
|
});
|
|
255
192
|
```
|
|
256
193
|
|
|
194
|
+
## FAQ
|
|
195
|
+
|
|
196
|
+
### What does ISR do in dev mode?
|
|
197
|
+
Nothing. It's a production-only feature
|
|
198
|
+
|
|
199
|
+
### What does `edge: true` target do in dev mode?
|
|
200
|
+
Nothing (yet?). If you have a use-case where an actual Edge runtime would be necessary in dev, please open a discussion
|
|
201
|
+
|
|
202
|
+
### I don't see Vercel specific headers in dev mode
|
|
203
|
+
This is not yet supported. Please open an issue if you need this (PR welcome).
|
|
204
|
+
|
|
205
|
+
Related documentation: https://vercel.com/docs/edge-network/headers/request-headers
|
|
206
|
+
|
|
207
|
+
## Migrations
|
|
208
|
+
|
|
209
|
+
- [Migration from v9 to v11](https://github.com/magne4000/vite-plugin-vercel/blob/main/MIGRATION.md)
|
|
210
|
+
|
|
257
211
|
## Demo
|
|
258
212
|
|
|
259
213
|
https://vike-photon-demo.vercel.app/
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//#region src/utils/assert.ts
|
|
2
|
+
function assert(condition, errorMessage) {
|
|
3
|
+
if (condition) return;
|
|
4
|
+
throw new Error(`[vite-plugin-vercel] ${errorMessage}`);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/api.ts
|
|
9
|
+
function createAPI(outfiles, pluginConfig) {
|
|
10
|
+
return {
|
|
11
|
+
getOutFiles() {
|
|
12
|
+
return outfiles;
|
|
13
|
+
},
|
|
14
|
+
get config() {
|
|
15
|
+
pluginConfig.config ??= {};
|
|
16
|
+
return pluginConfig.config;
|
|
17
|
+
},
|
|
18
|
+
get defaultMaxDuration() {
|
|
19
|
+
return pluginConfig.defaultMaxDuration;
|
|
20
|
+
},
|
|
21
|
+
set defaultMaxDuration(value) {
|
|
22
|
+
pluginConfig.defaultMaxDuration = value;
|
|
23
|
+
},
|
|
24
|
+
get expiration() {
|
|
25
|
+
return pluginConfig.expiration;
|
|
26
|
+
},
|
|
27
|
+
set expiration(value) {
|
|
28
|
+
pluginConfig.expiration = value;
|
|
29
|
+
},
|
|
30
|
+
get rewrites() {
|
|
31
|
+
pluginConfig.rewrites ??= [];
|
|
32
|
+
return pluginConfig.rewrites;
|
|
33
|
+
},
|
|
34
|
+
get headers() {
|
|
35
|
+
pluginConfig.headers ??= [];
|
|
36
|
+
return pluginConfig.headers;
|
|
37
|
+
},
|
|
38
|
+
get redirects() {
|
|
39
|
+
pluginConfig.redirects ??= [];
|
|
40
|
+
return pluginConfig.redirects;
|
|
41
|
+
},
|
|
42
|
+
get cleanUrls() {
|
|
43
|
+
return pluginConfig.cleanUrls;
|
|
44
|
+
},
|
|
45
|
+
set cleanUrls(value) {
|
|
46
|
+
pluginConfig.cleanUrls = value;
|
|
47
|
+
},
|
|
48
|
+
get trailingSlash() {
|
|
49
|
+
return pluginConfig.trailingSlash;
|
|
50
|
+
},
|
|
51
|
+
set trailingSlash(value) {
|
|
52
|
+
pluginConfig.trailingSlash = value;
|
|
53
|
+
},
|
|
54
|
+
get defaultSupportsResponseStreaming() {
|
|
55
|
+
return pluginConfig.defaultSupportsResponseStreaming;
|
|
56
|
+
},
|
|
57
|
+
set defaultSupportsResponseStreaming(value) {
|
|
58
|
+
pluginConfig.defaultSupportsResponseStreaming = value;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function getVercelAPI(pluginContextOrServer) {
|
|
63
|
+
const vpv = ("environment" in pluginContextOrServer ? pluginContextOrServer.environment.config : pluginContextOrServer.config).plugins.find((p) => p.name === "vite-plugin-vercel:api");
|
|
64
|
+
assert(vpv, "Could not find vite-plugin-vercel:api plugin");
|
|
65
|
+
assert(vpv.api, "Missing `api`. Make sure vite-plugin-vercel is up-to-date");
|
|
66
|
+
return vpv.api("environment" in pluginContextOrServer ? pluginContextOrServer : void 0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { getVercelAPI as n, assert as r, createAPI as t };
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { PluginContext, ViteVercelConfig, ViteVercelRedirect, ViteVercelRewrite } from "./types.js";
|
|
2
|
+
import { VercelOutputConfig } from "@vite-plugin-vercel/schemas";
|
|
3
|
+
import { ViteDevServer } from "vite";
|
|
4
|
+
import { EntryMeta } from "@universal-deploy/store";
|
|
5
|
+
import * as _vercel_routing_utils0 from "@vercel/routing-utils";
|
|
6
|
+
|
|
7
|
+
//#region src/api.d.ts
|
|
8
|
+
declare function createAPI(outfiles: ViteVercelOutFile[], pluginConfig: ViteVercelConfig): {
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
getOutFiles(): ViteVercelOutFile[];
|
|
13
|
+
readonly config: Partial<Omit<VercelOutputConfig, "version">>;
|
|
14
|
+
defaultMaxDuration: number | undefined;
|
|
15
|
+
expiration: number | undefined;
|
|
16
|
+
readonly rewrites: ViteVercelRewrite[];
|
|
17
|
+
readonly headers: _vercel_routing_utils0.Header[];
|
|
18
|
+
readonly redirects: ViteVercelRedirect[];
|
|
19
|
+
cleanUrls: boolean | undefined;
|
|
20
|
+
trailingSlash: boolean | undefined;
|
|
21
|
+
defaultSupportsResponseStreaming: boolean | undefined;
|
|
22
|
+
};
|
|
23
|
+
declare function getVercelAPI(pluginContextOrServer: Pick<PluginContext, "environment"> | ViteDevServer): {
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
getOutFiles(): ViteVercelOutFile[];
|
|
28
|
+
readonly config: Partial<Omit<VercelOutputConfig, "version">>;
|
|
29
|
+
defaultMaxDuration: number | undefined;
|
|
30
|
+
expiration: number | undefined;
|
|
31
|
+
readonly rewrites: ViteVercelRewrite[];
|
|
32
|
+
readonly headers: _vercel_routing_utils0.Header[];
|
|
33
|
+
readonly redirects: ViteVercelRedirect[];
|
|
34
|
+
cleanUrls: boolean | undefined;
|
|
35
|
+
trailingSlash: boolean | undefined;
|
|
36
|
+
defaultSupportsResponseStreaming: boolean | undefined;
|
|
37
|
+
};
|
|
38
|
+
type ViteVercelApi = ReturnType<typeof createAPI>;
|
|
39
|
+
type ViteVercelOutFile = ViteVercelOutFileChunk | ViteVercelOutFileAsset;
|
|
40
|
+
interface ViteVercelOutFileCommon {
|
|
41
|
+
filepath: string;
|
|
42
|
+
root: string;
|
|
43
|
+
outdir: string;
|
|
44
|
+
}
|
|
45
|
+
interface ViteVercelOutFileChunk extends ViteVercelOutFileCommon {
|
|
46
|
+
type: "chunk";
|
|
47
|
+
relatedEntry: EntryMeta;
|
|
48
|
+
}
|
|
49
|
+
interface ViteVercelOutFileAsset extends ViteVercelOutFileCommon {
|
|
50
|
+
type: "asset";
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { ViteVercelApi, ViteVercelOutFile, ViteVercelOutFileAsset, ViteVercelOutFileChunk, createAPI, getVercelAPI };
|
package/dist/api.js
ADDED