vite-plugin-vercel 10.0.0-beta.5 → 10.0.0

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,15 +1,9 @@
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
-
6
3
  Vercel adapter for [Vite](https://vitejs.dev/).
7
4
 
8
5
  Bundle your Vite application as supported by [Vercel Output API (v3)](https://vercel.com/docs/build-output-api/v3).
9
6
 
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
-
13
7
  ## Install
14
8
 
15
9
  ```bash
@@ -51,32 +45,27 @@ Install this package as a dev dependency and add it to your Vite config:
51
45
  // vite.config.ts
52
46
  import { defineConfig } from 'vite';
53
47
  import vercel from 'vite-plugin-vercel';
54
- import { getEntriesFromFs } from "vite-plugin-vercel/utils";
55
-
56
- const entries = await getEntriesFromFs("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
- });
62
48
 
63
49
  export default defineConfig({
64
- plugins: [vercel({
65
- entries,
66
- })],
50
+ plugins: [vercel()],
51
+ vercel: {
52
+ // optional configuration options, see "Advanced usage" below for details
53
+ },
67
54
  });
68
55
  ```
69
56
 
70
57
  > [!NOTE]
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.
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.
73
61
 
74
62
  ### Configure endpoints
75
63
 
76
- Endpoints added via `getEntriesFromFs` can be configured by exporting values from the endpoint file:
64
+ Endpoints under `/api`, `/_api` or added through `additionalEndpoints` can be configured
65
+ by exporting values from the endpoint file:
77
66
 
78
67
  ```ts
79
- // file: endpoints/api/endpoint.ts
68
+ // file: _api/endpoint.ts
80
69
 
81
70
  // Should run on edge runtime
82
71
  export const edge = true;
@@ -101,11 +90,53 @@ export default async function handler() {
101
90
  }
102
91
  ```
103
92
 
93
+ > [!NOTE]
94
+ > Please create an issue if you need other per-endpoints configurations
95
+
104
96
  ### Edge middleware
105
97
 
106
98
  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).
107
99
 
108
- ## Advanced settings
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
109
140
 
110
141
  ```ts
111
142
  // vite.config.ts
@@ -113,7 +144,8 @@ import { defineConfig } from 'vite';
113
144
  import vercel from 'vite-plugin-vercel';
114
145
 
115
146
  export default defineConfig({
116
- plugins: [vercel({
147
+ plugins: [vercel()],
148
+ vercel: {
117
149
  // All the followings optional
118
150
 
119
151
  /**
@@ -130,7 +162,14 @@ export default defineConfig({
130
162
  * Defaults to 86400 seconds (24h).
131
163
  */
132
164
  expiration: 86400,
133
-
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
+ },
134
173
  /**
135
174
  * See https://vercel.com/docs/projects/project-configuration#rewrites
136
175
  */
@@ -164,16 +203,19 @@ export default defineConfig({
164
203
  */
165
204
  trailingSlash: true,
166
205
  /**
167
- * Use `getEntriesFromFs` 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
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.
169
210
  */
170
- entries: {
171
- root: {
172
- id: 'src/routes/root.ts',
173
- name: 'root',
174
- route: '/'
175
- }
176
- },
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
+ ],
177
219
  /**
178
220
  * Advanced configuration to override .vercel/output/config.json
179
221
  * See https://vercel.com/docs/build-output-api/v3/configuration#configuration
@@ -186,31 +228,32 @@ export default defineConfig({
186
228
  // cache?: string[];
187
229
  // crons?: CronsConfig;
188
230
  },
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
+ },
189
249
  /**
190
250
  * Defaults to `.vercel/output`. Mostly useful for testing purpose
191
251
  */
192
252
  outDir: '.vercel/output',
193
- })]
253
+ },
194
254
  });
195
255
  ```
196
256
 
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
-
214
257
  ## Demo
215
258
 
216
259
  https://vike-photon-demo.vercel.app/