vite-plugin-vercel 10.0.0 → 11.0.0-beta.2

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 CHANGED
@@ -1,9 +1,15 @@
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 (v10). 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).
6
9
 
10
+ > [!NOTE]
11
+ > This plugin is mostly a re-export of [`@photonjs/vercel`](https://github.com/photon-js/photon/tree/main/packages/adapter-vercel)
12
+
7
13
  ## Install
8
14
 
9
15
  ```bash
@@ -25,12 +31,12 @@ bun add -D vite-plugin-vercel
25
31
  ## Features
26
32
 
27
33
  - [x] [SSG/Static files](https://vercel.com/docs/build-output-api/v3/primitives#static-files)
28
- - see [`prerender` config](/packages/vercel/src/types.ts#L37)
34
+ - see [`prerender` config](/packages/vercel/src/types.ts#L37)
29
35
  - [x] [SSR/Serverless functions](https://vercel.com/docs/build-output-api/v3/primitives#serverless-functions)
30
- - `.[jt]s` files under the `<root>/api` folder of your project are automatically bundled as Serverless functions under `.vercel/output/functions/api/*.func`
31
- - see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L62)
36
+ - `.[jt]s` files under the `<root>/api` folder of your project are automatically bundled as Serverless functions under `.vercel/output/functions/api/*.func`
37
+ - see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L62)
32
38
  - [x] [ISR/Prerender functions](https://vercel.com/docs/build-output-api/v3/primitives#prerender-functions)
33
- - see [`isr` config](/packages/vercel/src/types.ts#L89). Also see implementation of [vike](/packages/vike-integration/vike.ts) for example
39
+ - see [`isr` config](/packages/vercel/src/types.ts#L89). Also see implementation of [vike](/packages/vike-integration/vike.ts) for example
34
40
  - [x] [Edge functions](https://vercel.com/docs/build-output-api/v3/primitives#edge-functions)
35
41
  - [x] [Edge middleware](https://vercel.com/docs/functions/edge-middleware/middleware-api)
36
42
  - [ ] [Images optimization](https://vercel.com/docs/build-output-api/v3/configuration#images)
@@ -45,27 +51,32 @@ Install this package as a dev dependency and add it to your Vite config:
45
51
  // vite.config.ts
46
52
  import { defineConfig } from 'vite';
47
53
  import vercel from 'vite-plugin-vercel';
54
+ import { getVercelEntries } from "vite-plugin-vercel";
55
+
56
+ const entries = await getVercelEntries("endpoints/api", {
57
+ // Auto mapping examples:
58
+ // endpoints/api/page.ts -> /api/page
59
+ // endpoints/api/name/[name].ts -> /api/name/*
60
+ destination: "api",
61
+ });
48
62
 
49
63
  export default defineConfig({
50
- plugins: [vercel()],
51
- vercel: {
52
- // optional configuration options, see "Advanced usage" below for details
53
- },
64
+ plugins: [vercel({
65
+ entries,
66
+ })],
54
67
  });
55
68
  ```
56
69
 
57
70
  > [!NOTE]
58
- > Files under `/api` or `/_api` directory will automatically be added under `/api/*` route
59
- > Prefer using `/_api` directory, as `@vercel/build` is currently force building `/api` files,
60
- > with no way to disable it, thus avoiding double compilation and unexpected behaviour.
71
+ > `@vercel/build` currently forces the building of files in the _/api_ folder, with no way to disable this behavior.
72
+ > It's recommended to place your files in a different folder.
61
73
 
62
74
  ### Configure endpoints
63
75
 
64
- Endpoints under `/api`, `/_api` or added through `additionalEndpoints` can be configured
65
- by exporting values from the endpoint file:
76
+ Endpoints added via `getVercelEntries` can be configured by exporting values from the endpoint file:
66
77
 
67
78
  ```ts
68
- // file: _api/endpoint.ts
79
+ // file: endpoints/api/endpoint.ts
69
80
 
70
81
  // Should run on edge runtime
71
82
  export const edge = true;
@@ -90,53 +101,11 @@ export default async function handler() {
90
101
  }
91
102
  ```
92
103
 
93
- > [!NOTE]
94
- > Please create an issue if you need other per-endpoints configurations
95
-
96
104
  ### Edge middleware
97
105
 
98
106
  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
107
 
100
- ## Usage with Vike
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
108
+ ## Advanced settings
140
109
 
141
110
  ```ts
142
111
  // vite.config.ts
@@ -144,8 +113,7 @@ import { defineConfig } from 'vite';
144
113
  import vercel from 'vite-plugin-vercel';
145
114
 
146
115
  export default defineConfig({
147
- plugins: [vercel()],
148
- vercel: {
116
+ plugins: [vercel({
149
117
  // All the followings optional
150
118
 
151
119
  /**
@@ -162,14 +130,7 @@ export default defineConfig({
162
130
  * Defaults to 86400 seconds (24h).
163
131
  */
164
132
  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
- },
133
+
173
134
  /**
174
135
  * See https://vercel.com/docs/projects/project-configuration#rewrites
175
136
  */
@@ -203,19 +164,16 @@ export default defineConfig({
203
164
  */
204
165
  trailingSlash: true,
205
166
  /**
206
- * By default, all `api/*` and `_api/*` endpoints are compiled under `.vercel/output/functions/api/*.func`.
207
- * If others serverless functions need to be compiled under `.vercel/output/functions`, they should be added here.
208
- * For instance, a framework can leverage this to have a generic ssr endpoint
209
- * without requiring the user to write any code.
167
+ * Use `getVercelEntries` for mapping your filesystem routes to entries.
168
+ * If you are interfacing this plugin with a framework, entries can also be added through the Photon API
210
169
  */
211
- additionalEndpoints: [
212
- {
213
- // can also be an Object representing an esbuild StdinOptions
214
- source: '/path/to/file.ts',
215
- // URL path of the handler, will be generated to `.vercel/output/functions/api/file.func/index.js`
216
- destination: '/api/file',
217
- },
218
- ],
170
+ entries: {
171
+ root: {
172
+ id: 'src/routes/root.ts',
173
+ name: 'root',
174
+ route: '/'
175
+ }
176
+ },
219
177
  /**
220
178
  * Advanced configuration to override .vercel/output/config.json
221
179
  * See https://vercel.com/docs/build-output-api/v3/configuration#configuration
@@ -228,32 +186,31 @@ export default defineConfig({
228
186
  // cache?: string[];
229
187
  // crons?: CronsConfig;
230
188
  },
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
189
  /**
250
190
  * Defaults to `.vercel/output`. Mostly useful for testing purpose
251
191
  */
252
192
  outDir: '.vercel/output',
253
- },
193
+ })]
254
194
  });
255
195
  ```
256
196
 
197
+ ## FAQ
198
+
199
+ ### What does ISR do in dev mode?
200
+ Nothing. It's a production-only feature
201
+
202
+ ### What does `edge: true` target do in dev mode?
203
+ Nothing (yet?). If you have a use-case where an actual Edge runtime would be necessary in dev, please open a discussion
204
+
205
+ ### I don't see Vercel specific headers in dev mode
206
+ This is not yet supported. Please open an issue if you need this (PR welcome).
207
+
208
+ Related documentation: https://vercel.com/docs/edge-network/headers/request-headers
209
+
210
+ ## Migrations
211
+
212
+ - [Migration from v9 to v10](https://github.com/magne4000/vite-plugin-vercel/blob/main/MIGRATION.md)
213
+
257
214
  ## Demo
258
215
 
259
216
  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
@@ -0,0 +1,3 @@
1
+ import { n as getVercelAPI, t as createAPI } from "./api-DR2y7JVQ.js";
2
+
3
+ export { createAPI, getVercelAPI };