vite-plugin-vercel 0.2.1 → 0.3.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,68 +1,152 @@
1
1
  # vite-plugin-vercel
2
2
 
3
- This is a **Work In Progress** Vercel adapter for [`vite`](https://vitejs.dev/).
3
+ Vercel adapter for [`vite`](https://vitejs.dev/).
4
4
 
5
5
  Its purpose is to help you bundle your application in `.vercel` folder as supported by
6
6
  [Vercel API v3](https://vercel.com/docs/build-output-api/v3).
7
7
 
8
8
  ## Features
9
9
 
10
- - [x] [SSG/Static files support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/static-files)
11
- - see [`prerender` config](/packages/vercel/src/types.ts#L33)
12
- - [x] [SSR/Serverless functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions)
10
+ - [x] [SSG/Static files support](https://vercel.com/docs/build-output-api/v3/primitives#static-files)
11
+ - see [`prerender` config](/packages/vercel/src/types.ts#L37)
12
+ - [x] [SSR/Serverless functions support](https://vercel.com/docs/build-output-api/v3/primitives#serverless-functions)
13
13
  - `.[jt]s` files under the `<root>/api` folder of your project are automatically bundled as Serverless functions under `.vercel/output/functions/api/*.func`
14
- - see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L54)
15
- - [x] [ISR/Prerender functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/prerender-functions)
16
- - see [`isr` config](/packages/vercel/src/types.ts#L81). Also see implementation of [vite-plugin-ssr](/packages/vite-plugin-ssr/vite-plugin-ssr.ts) for example
17
- - [x] [Edge functions support](https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions)
18
- - [ ] [Images optimization support](https://vercel.com/docs/build-output-api/v3#build-output-configuration/supported-properties/images)
19
- - [ ] [Preview mode support](https://vercel.com/docs/build-output-api/v3#features/preview-mode)
20
- - [x] [Advanced config override](/packages/vercel/src/types.ts#L15)
14
+ - see [`additionalEndpoints` config](/packages/vercel/src/types.ts#L62)
15
+ - [x] [ISR/Prerender functions support](https://vercel.com/docs/build-output-api/v3/primitives#prerender-functions)
16
+ - see [`isr` config](/packages/vercel/src/types.ts#L89). Also see implementation of [vite-plugin-ssr](/packages/vike-integration/vite-plugin-ssr.ts) for example
17
+ - [x] [Edge functions support](https://vercel.com/docs/build-output-api/v3/primitives#edge-functions)
18
+ - [ ] [Images optimization support](https://vercel.com/docs/build-output-api/v3/configuration#images)
19
+ - [ ] [Preview mode support](https://vercel.com/docs/build-output-api/v3/features#preview-mode)
20
+ - [x] [Advanced config override](/packages/vercel/src/types.ts#L19)
21
21
  - [ ] Complete config override
22
22
 
23
- ## Usage
23
+ ## Simple usage
24
24
 
25
- First, make sure `ENABLE_VC_BUILD=1` is declared as an Environment Variable in your deployment configuration.
26
-
27
- Then, install this package as a dev dependency and add it to your Vite config like this:
25
+ Then, install this package as a dev dependency and add it to your Vite config:
28
26
 
29
27
  ```ts
28
+ // vite.config.ts
30
29
  import { defineConfig } from 'vite';
31
30
  import vercel from 'vite-plugin-vercel';
32
- import ssr from 'vite-plugin-ssr/plugin';
33
31
 
34
32
  export default defineConfig({
35
- plugins: [ssr(), vercel()],
33
+ plugins: [vercel()],
34
+ vercel: {
35
+ // optional configuration options, see below for details
36
+ },
36
37
  });
37
38
  ```
38
39
 
39
- ### Usage with vite-plugin-ssr
40
+ ## Usage with vite-plugin-ssr
41
+
42
+ [vite-plugin-ssr](https://vite-plugin-ssr.com/) is supported through [@vite-plugin-vercel/vike](/packages/vike-integration/README.md) plugin.
40
43
 
41
- [vite-plugin-ssr](https://vite-plugin-ssr.com/) is supported through [@magne4000/vite-plugin-vercel-ssr](/packages/vite-plugin-ssr/README.md) plugin.
44
+ You only need to install `@vite-plugin-vercel/vike`, the config stays the same as above.
42
45
 
43
- Install `@magne4000/vite-plugin-vercel-ssr` package, and update your vite config:
46
+ > [!IMPORTANT]
47
+ > `@vite-plugin-vercel/vike` supersedes the old `@magne4000/vite-plugin-vercel-ssr` package.
48
+ > As such, you should remove `@magne4000/vite-plugin-vercel-ssr` from your package.json and vite config file.
49
+
50
+ ## Advanced usage
44
51
 
45
52
  ```ts
46
53
  // vite.config.ts
47
54
  import { defineConfig } from 'vite';
48
- import ssr from 'vite-plugin-ssr/plugin';
49
55
  import vercel from 'vite-plugin-vercel';
50
- import vercelSsr from '@magne4000/vite-plugin-vercel-ssr';
51
56
 
52
- export default defineConfig(async ({ command, mode }) => {
53
- return {
54
- plugins: [ssr(), vercel(), vercelSsr()],
55
- vercel: {
56
- // Tweak what you need, check TS definition for details
57
+ export default defineConfig({
58
+ plugins: [vercel()],
59
+ vercel: {
60
+ // All the followings optional
61
+
62
+ /**
63
+ * How long Functions should be allowed to run for every request, in seconds.
64
+ * If left empty, default value for your plan will be used.
65
+ */
66
+ defaultMaxDuration: 30,
67
+ /**
68
+ * Default expiration time (in seconds) for prerender functions.
69
+ * Defaults to 86400 seconds (24h).
70
+ */
71
+ expiration: 86400,
72
+ /**
73
+ * Also known as Server Side Generation, or SSG.
74
+ * If present, this function is responsible to create static files in `.vercel/output/static`.
75
+ * Defaults to `false`, which disables prerendering.
76
+ */
77
+ prerender(resolvedConfig) {
78
+ // Check `/packages/vite-plugin-ssr/vite-plugin-ssr.ts` `prerender` for an example
57
79
  },
58
- };
80
+ /**
81
+ * See https://vercel.com/docs/projects/project-configuration#rewrites
82
+ */
83
+ rewrites: [{ source: '/about', destination: '/about-our-company.html' }],
84
+ /**
85
+ * See https://vercel.com/docs/projects/project-configuration#redirects
86
+ */
87
+ redirects: [
88
+ { source: '/me', destination: '/profile.html', permanent: false },
89
+ ],
90
+ /**
91
+ * See https://vercel.com/docs/projects/project-configuration#cleanurls
92
+ */
93
+ cleanUrls: true,
94
+ /**
95
+ * See https://vercel.com/docs/projects/project-configuration#trailingslash
96
+ */
97
+ trailingSlash: true,
98
+ /**
99
+ * By default, all `api/*` endpoints are compiled under `.vercel/output/functions/api/*.func`.
100
+ * If others serverless functions need to be compiled under `.vercel/output/functions`, they should be added here.
101
+ * For instance, a framework can leverage this to have a generic ssr endpoint
102
+ * without requiring the user to write any code.
103
+ */
104
+ additionalEndpoints: [
105
+ {
106
+ // can also be an Object representing an esbuild StdinOptions
107
+ source: '/path/to/file.ts',
108
+ // URL path of the handler, will be generated to `.vercel/output/functions/api/file.func/index.js`
109
+ destination: '/api/file',
110
+ },
111
+ ],
112
+ /**
113
+ * Advanced configuration to override .vercel/output/config.json
114
+ * See https://vercel.com/docs/build-output-api/v3/configuration#configuration
115
+ */
116
+ config: {
117
+ // routes?: Route[];
118
+ // images?: ImagesConfig;
119
+ // wildcard?: WildcardConfig;
120
+ // overrides?: OverrideConfig;
121
+ // cache?: string[];
122
+ // crons?: CronsConfig;
123
+ },
124
+ /**
125
+ * ISR and SSG pages are mutually exclusive. If a page is found in both, ISR prevails.
126
+ * Keys are path relative to .vercel/output/functions directory, either without extension,
127
+ * or with `.prerender-config.json` extension.
128
+ * If you have multiple isr configurations pointing to the same underlying function, you can leverage the `symlink`
129
+ * property.
130
+ *
131
+ * Can be an object or a function returning an object (or a Promise of an object).
132
+ *
133
+ * Check `/packages/vite-plugin-ssr/vite-plugin-ssr.ts` `vitePluginVercelVpsIsrPlugin` for advanced usage.
134
+ */
135
+ isr: {
136
+ // `symlink: 'ssr_'` means that a function is available under `.vercel/output/functions/ssr_.func`
137
+ '/pages/a': { expiration: 15, symlink: 'ssr_', route: '^/a/.*$' },
138
+ '/pages/b/c': { expiration: 15, symlink: 'ssr_', route: '^/b/c/.*$' },
139
+ '/pages/d': { expiration: 15, symlink: 'ssr_', route: '^/d$' },
140
+ '/pages/e': { expiration: 25 },
141
+ },
142
+ /**
143
+ * Defaults to `.vercel/output`. Mostly useful for testing purpose
144
+ */
145
+ outDir: '.vercel/output',
146
+ },
59
147
  });
60
148
  ```
61
149
 
62
- ### Config
63
-
64
- See [TS types](/packages/vercel/src/types.ts#L15) for details.
65
-
66
150
  ## Demo
67
151
 
68
152
  https://test-vite-vercel-plugin.vercel.app/
package/dist/index.cjs CHANGED
@@ -487,22 +487,22 @@ async function getIsrConfig(resolvedConfig) {
487
487
  var import_path5 = __toESM(require("path"), 1);
488
488
  function vercelPlugin() {
489
489
  let resolvedConfig;
490
- let vpsFound = false;
490
+ let vikeFound = false;
491
491
  return {
492
492
  apply: "build",
493
493
  name: "vite-plugin-vercel",
494
494
  enforce: "post",
495
495
  configResolved(config) {
496
496
  resolvedConfig = config;
497
- vpsFound = resolvedConfig.plugins.some(
498
- (p) => p.name.startsWith("vite-plugin-ssr:")
497
+ vikeFound = resolvedConfig.plugins.some(
498
+ (p) => p.name.match("^vite-plugin-ssr:|^vike:")
499
499
  );
500
500
  },
501
501
  async writeBundle() {
502
502
  var _a;
503
503
  if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr)) {
504
504
  await cleanOutputDirectory(resolvedConfig);
505
- if (vpsFound) {
505
+ if (vikeFound) {
506
506
  return;
507
507
  }
508
508
  }
@@ -555,6 +555,15 @@ async function getStaticHtmlFiles(src) {
555
555
  }
556
556
  return htmlFiles;
557
557
  }
558
- function allPlugins() {
559
- return [vercelPlugin()];
558
+ async function tryImportVpvv() {
559
+ try {
560
+ await import("vite-plugin-ssr/plugin");
561
+ const vpvv = await import("@vite-plugin-vercel/vike");
562
+ return vpvv.default();
563
+ } catch (e) {
564
+ return null;
565
+ }
566
+ }
567
+ function allPlugins(options = {}) {
568
+ return [vercelPlugin(), options.smart !== false ? tryImportVpvv() : null];
560
569
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ResolvedConfig, Plugin } from 'vite';
1
+ import { ResolvedConfig, PluginOption } from 'vite';
2
2
  import { StdinOptions, BuildOptions } from 'esbuild';
3
3
  import { z } from 'zod';
4
4
  import { Rewrite, Redirect } from '@vercel/routing-utils';
@@ -521,8 +521,8 @@ type ViteVercelRedirect = Redirect & {
521
521
  };
522
522
  interface ViteVercelConfig {
523
523
  /**
524
- * How long Functions should be allowed to run for every request in seconds.
525
- * If left empty, default value for your plan will is used.
524
+ * How long Functions should be allowed to run for every request, in seconds.
525
+ * If left empty, default value for your plan will be used.
526
526
  */
527
527
  defaultMaxDuration?: number;
528
528
  /**
@@ -534,10 +534,26 @@ interface ViteVercelConfig {
534
534
  expiration?: number;
535
535
  /**
536
536
  * Also known as Server Side Generation, or SSG.
537
- * If present, must build static files in `.vercel/output/static`.
538
- * Can be set to `false` to disable prerendering completely.
537
+ * If present, this function is responsible to create static files in `.vercel/output/static`.
538
+ * Defaults to `false`, which disables prerendering.
539
539
  */
540
540
  prerender?: ViteVercelPrerenderFn | false;
541
+ /**
542
+ * @see {@link https://vercel.com/docs/projects/project-configuration#rewrites}
543
+ */
544
+ rewrites?: ViteVercelRewrite[];
545
+ /**
546
+ * @see {@link https://vercel.com/docs/projects/project-configuration#redirects}
547
+ */
548
+ redirects?: ViteVercelRedirect[];
549
+ /**
550
+ * @see {@link https://vercel.com/docs/projects/project-configuration#cleanurls}
551
+ */
552
+ cleanUrls?: boolean;
553
+ /**
554
+ * @see {@link https://vercel.com/docs/projects/project-configuration#trailingslash}
555
+ */
556
+ trailingSlash?: boolean;
541
557
  /**
542
558
  * By default, all `api/*` endpoints are compiled under `.vercel/output/functions/api/*.func`.
543
559
  * If others serverless functions need to be compiled under `.vercel/output/functions`, they should be added here.
@@ -558,14 +574,10 @@ interface ViteVercelConfig {
558
574
  * }
559
575
  * ```
560
576
  */
561
- rewrites?: ViteVercelRewrite[];
562
- redirects?: ViteVercelRedirect[];
563
- cleanUrls?: boolean;
564
- trailingSlash?: boolean;
565
577
  additionalEndpoints?: ViteVercelApiEntry[];
566
578
  /**
567
579
  * Advanced configuration to override .vercel/output/config.json
568
- * @see {@link https://vercel.com/docs/build-output-api/v3#build-output-configuration}
580
+ * @see {@link https://vercel.com/docs/build-output-api/v3/configuration#configuration}
569
581
  * @protected
570
582
  */
571
583
  config?: Partial<Omit<VercelOutputConfig, 'version'>>;
@@ -629,6 +641,8 @@ interface ViteVercelApiEntry {
629
641
  edge?: boolean;
630
642
  }
631
643
 
632
- declare function allPlugins(): Plugin[];
644
+ declare function allPlugins(options?: {
645
+ smart?: boolean;
646
+ }): PluginOption[];
633
647
 
634
648
  export { VercelOutputConfig, VercelOutputIsr, VercelOutputPrerenderConfig, VercelOutputVcConfig, ViteVercelApiEntry, ViteVercelConfig, ViteVercelPrerenderFn, ViteVercelPrerenderRoute, ViteVercelRedirect, ViteVercelRewrite, allPlugins as default };
package/dist/index.js CHANGED
@@ -453,22 +453,22 @@ async function getIsrConfig(resolvedConfig) {
453
453
  import path5 from "path";
454
454
  function vercelPlugin() {
455
455
  let resolvedConfig;
456
- let vpsFound = false;
456
+ let vikeFound = false;
457
457
  return {
458
458
  apply: "build",
459
459
  name: "vite-plugin-vercel",
460
460
  enforce: "post",
461
461
  configResolved(config) {
462
462
  resolvedConfig = config;
463
- vpsFound = resolvedConfig.plugins.some(
464
- (p) => p.name.startsWith("vite-plugin-ssr:")
463
+ vikeFound = resolvedConfig.plugins.some(
464
+ (p) => p.name.match("^vite-plugin-ssr:|^vike:")
465
465
  );
466
466
  },
467
467
  async writeBundle() {
468
468
  var _a;
469
469
  if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr)) {
470
470
  await cleanOutputDirectory(resolvedConfig);
471
- if (vpsFound) {
471
+ if (vikeFound) {
472
472
  return;
473
473
  }
474
474
  }
@@ -521,8 +521,17 @@ async function getStaticHtmlFiles(src) {
521
521
  }
522
522
  return htmlFiles;
523
523
  }
524
- function allPlugins() {
525
- return [vercelPlugin()];
524
+ async function tryImportVpvv() {
525
+ try {
526
+ await import("vite-plugin-ssr/plugin");
527
+ const vpvv = await import("@vite-plugin-vercel/vike");
528
+ return vpvv.default();
529
+ } catch (e) {
530
+ return null;
531
+ }
532
+ }
533
+ function allPlugins(options = {}) {
534
+ return [vercelPlugin(), options.smart !== false ? tryImportVpvv() : null];
526
535
  }
527
536
  export {
528
537
  allPlugins as default
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -21,23 +21,33 @@
21
21
  "repository": "https://github.com/magne4000/vite-plugin-vercel",
22
22
  "license": "MIT",
23
23
  "peerDependencies": {
24
- "vite": "^4.2.0"
24
+ "vite": "^4.2.0",
25
+ "vite-plugin-ssr": "*",
26
+ "@vite-plugin-vercel/vike": "0.3.1"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@vite-plugin-vercel/vike": {
30
+ "optional": true
31
+ },
32
+ "vite-plugin-ssr": {
33
+ "optional": true
34
+ }
25
35
  },
26
36
  "devDependencies": {
27
- "@types/node": "^16.18.37",
28
- "@typescript-eslint/eslint-plugin": "^5.60.1",
29
- "@typescript-eslint/parser": "^5.60.1",
30
- "eslint": "^8.43.0",
37
+ "@types/node": "^16.18.48",
38
+ "@typescript-eslint/eslint-plugin": "^5.62.0",
39
+ "@typescript-eslint/parser": "^5.62.0",
40
+ "eslint": "^8.48.0",
31
41
  "tsup": "^6.7.0",
32
- "typescript": "^5.1.6",
33
- "vite": "^4.3.9"
42
+ "typescript": "^5.2.2",
43
+ "vite": "^4.4.9"
34
44
  },
35
45
  "dependencies": {
36
46
  "@brillout/libassert": "^0.5.8",
37
47
  "@vercel/routing-utils": "^2.2.1",
38
48
  "esbuild": "^0.17.19",
39
- "fast-glob": "^3.3.0",
40
- "zod": "^3.21.4"
49
+ "fast-glob": "^3.3.1",
50
+ "zod": "^3.22.2"
41
51
  },
42
52
  "scripts": {
43
53
  "build": "tsup",