vite-plugin-vercel 0.0.7 → 0.1.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
@@ -2,9 +2,29 @@
2
2
 
3
3
  This is a **Work In Progress** Vercel adapter for [`vite`](https://vitejs.dev/).
4
4
 
5
+ Its purpose is to help you bundle your application in `.vercel` folder as supported by
6
+ [Vercel API v3](https://vercel.com/docs/build-output-api/v3).
7
+
8
+ ## Features
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)
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
+ - [ ] [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)
21
+ - [ ] Complete config override
22
+
5
23
  ## Usage
6
24
 
7
- Install as a dev dependency and add it to your Vite config like this:
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:
8
28
 
9
29
  ```ts
10
30
  import { defineConfig } from 'vite';
@@ -21,30 +41,18 @@ export default defineConfig({
21
41
  [vite-plugin-ssr](https://vite-plugin-ssr.com/) will support this plugin when stable.
22
42
  In the meantime, you can add experimental support yourself.
23
43
 
24
- First copy [prerender](../../prerender) folder to the root of your project.
25
- Then, update your vercel config:
44
+ Install `@magne4000/vite-plugin-vercel-ssr` package, and update your vite config:
26
45
 
27
46
  ```ts
28
- // vercel.config.ts
29
- // A TS config is prefered if your project is of { type: "module" }
30
-
31
- import module from 'module';
47
+ // vite.config.ts
32
48
  import { defineConfig } from 'vite';
33
49
  import ssr from 'vite-plugin-ssr/plugin';
34
50
  import vercel from 'vite-plugin-vercel';
35
-
36
- // FIX esbuild bug https://github.com/evanw/esbuild/pull/2067
37
- // probably not necessary when `./prerender/vite-plugin-ssr` will be included in `vite-plugin-ssr`
38
- // eslint-disable-next-line no-undef
39
- globalThis.require = module.createRequire(import.meta.url);
51
+ import vercelSsr from '@magne4000/vite-plugin-vercel-ssr';
40
52
 
41
53
  export default defineConfig(async ({ command, mode }) => {
42
- // Dynamic import to bypass esbuild compilation issue.
43
- // If you are not using ESM, could me move as a top synchronous import
44
- const vitePluginSsrVercelPlugin = await import('./prerender/vite-plugin-ssr');
45
-
46
54
  return {
47
- plugins: [ssr(), vercel(), vitePluginSsrVercelPlugin.default()],
55
+ plugins: [ssr(), vercel(), vercelSsr()],
48
56
  build: {
49
57
  polyfillDynamicImport: false,
50
58
  },
@@ -54,3 +62,11 @@ export default defineConfig(async ({ command, mode }) => {
54
62
  };
55
63
  });
56
64
  ```
65
+
66
+ ### Config
67
+
68
+ See [TS types](/packages/vercel/src/types.ts#L15) for details.
69
+
70
+ ## Demo
71
+
72
+ https://test-vite-vercel-plugin.vercel.app/
package/dist/index.cjs CHANGED
@@ -254,7 +254,7 @@ function getEntries(resolvedConfig) {
254
254
  const parsed = import_path3.default.parse(outFilePath);
255
255
  const entry = {
256
256
  source: filePath,
257
- destination: `api/${import_path3.default.join(parsed.dir, parsed.name)}.func`
257
+ destination: `api/${import_path3.default.posix.join(parsed.dir, parsed.name)}.func`
258
258
  };
259
259
  entryPoints.push(entry);
260
260
  return entryPoints;
@@ -375,6 +375,7 @@ async function getIsrConfig(resolvedConfig) {
375
375
  }
376
376
 
377
377
  // src/index.ts
378
+ var import_path5 = __toESM(require("path"), 1);
378
379
  function vercelPlugin() {
379
380
  let resolvedConfig;
380
381
  return {
@@ -398,12 +399,13 @@ function vercelPlugin() {
398
399
  var _a;
399
400
  if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr))
400
401
  return;
402
+ const userOverrides = await computeStaticHtmlOverrides(resolvedConfig);
401
403
  const overrides = await execPrerender(resolvedConfig);
402
404
  await buildEndpoints(resolvedConfig);
403
405
  const rewrites = await buildPrerenderConfigs(resolvedConfig);
404
406
  await writeConfig(resolvedConfig, {
405
407
  routes: [{ handle: "filesystem" }, ...rewrites],
406
- overrides
408
+ overrides: __spreadValues(__spreadValues({}, userOverrides), overrides)
407
409
  });
408
410
  }
409
411
  };
@@ -417,6 +419,28 @@ async function cleanOutputDirectory(resolvedConfig) {
417
419
  force: true
418
420
  });
419
421
  }
422
+ async function computeStaticHtmlOverrides(resolvedConfig) {
423
+ const staticAbsolutePath = getOutput(resolvedConfig, "static");
424
+ const files = await getStaticHtmlFiles(resolvedConfig, staticAbsolutePath);
425
+ return files.reduce((acc, curr) => {
426
+ const relPath = import_path5.default.relative(staticAbsolutePath, curr);
427
+ const parsed = import_path5.default.parse(relPath);
428
+ const pathJoined = import_path5.default.join(parsed.dir, parsed.name);
429
+ acc[relPath] = {
430
+ path: pathJoined
431
+ };
432
+ return acc;
433
+ }, {});
434
+ }
435
+ async function getStaticHtmlFiles(resolvedConfig, src) {
436
+ const entries = await import_promises5.default.readdir(src, { withFileTypes: true });
437
+ const htmlFiles = [];
438
+ for (const entry of entries) {
439
+ const srcPath = import_path5.default.join(src, entry.name);
440
+ entry.isDirectory() ? htmlFiles.push(...await getStaticHtmlFiles(resolvedConfig, srcPath)) : srcPath.endsWith(".html") ? htmlFiles.push(srcPath) : void 0;
441
+ }
442
+ return htmlFiles;
443
+ }
420
444
  function allPlugins() {
421
445
  return [vercelPlugin()];
422
446
  }
package/dist/index.js CHANGED
@@ -231,7 +231,7 @@ function getEntries(resolvedConfig) {
231
231
  const parsed = path3.parse(outFilePath);
232
232
  const entry = {
233
233
  source: filePath,
234
- destination: `api/${path3.join(parsed.dir, parsed.name)}.func`
234
+ destination: `api/${path3.posix.join(parsed.dir, parsed.name)}.func`
235
235
  };
236
236
  entryPoints.push(entry);
237
237
  return entryPoints;
@@ -352,6 +352,7 @@ async function getIsrConfig(resolvedConfig) {
352
352
  }
353
353
 
354
354
  // src/index.ts
355
+ import path5 from "path";
355
356
  function vercelPlugin() {
356
357
  let resolvedConfig;
357
358
  return {
@@ -375,12 +376,13 @@ function vercelPlugin() {
375
376
  var _a;
376
377
  if (!((_a = resolvedConfig.build) == null ? void 0 : _a.ssr))
377
378
  return;
379
+ const userOverrides = await computeStaticHtmlOverrides(resolvedConfig);
378
380
  const overrides = await execPrerender(resolvedConfig);
379
381
  await buildEndpoints(resolvedConfig);
380
382
  const rewrites = await buildPrerenderConfigs(resolvedConfig);
381
383
  await writeConfig(resolvedConfig, {
382
384
  routes: [{ handle: "filesystem" }, ...rewrites],
383
- overrides
385
+ overrides: __spreadValues(__spreadValues({}, userOverrides), overrides)
384
386
  });
385
387
  }
386
388
  };
@@ -394,6 +396,28 @@ async function cleanOutputDirectory(resolvedConfig) {
394
396
  force: true
395
397
  });
396
398
  }
399
+ async function computeStaticHtmlOverrides(resolvedConfig) {
400
+ const staticAbsolutePath = getOutput(resolvedConfig, "static");
401
+ const files = await getStaticHtmlFiles(resolvedConfig, staticAbsolutePath);
402
+ return files.reduce((acc, curr) => {
403
+ const relPath = path5.relative(staticAbsolutePath, curr);
404
+ const parsed = path5.parse(relPath);
405
+ const pathJoined = path5.join(parsed.dir, parsed.name);
406
+ acc[relPath] = {
407
+ path: pathJoined
408
+ };
409
+ return acc;
410
+ }, {});
411
+ }
412
+ async function getStaticHtmlFiles(resolvedConfig, src) {
413
+ const entries = await fs5.readdir(src, { withFileTypes: true });
414
+ const htmlFiles = [];
415
+ for (const entry of entries) {
416
+ const srcPath = path5.join(src, entry.name);
417
+ entry.isDirectory() ? htmlFiles.push(...await getStaticHtmlFiles(resolvedConfig, srcPath)) : srcPath.endsWith(".html") ? htmlFiles.push(srcPath) : void 0;
418
+ }
419
+ return htmlFiles;
420
+ }
397
421
  function allPlugins() {
398
422
  return [vercelPlugin()];
399
423
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vercel",
3
- "version": "0.0.7",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -23,20 +23,20 @@
23
23
  "vite": "^2.9.9"
24
24
  },
25
25
  "devDependencies": {
26
- "@types/node": "^17.0.34",
27
- "@typescript-eslint/eslint-plugin": "^5.24.0",
28
- "@typescript-eslint/parser": "^5.24.0",
29
- "eslint": "^8.15.0",
30
- "eslint-plugin-solid": "^0.4.7",
31
- "tsup": "^5.12.8",
32
- "typescript": "^4.6.4"
26
+ "@types/node": "^17.0.36",
27
+ "@typescript-eslint/eslint-plugin": "^5.26.0",
28
+ "@typescript-eslint/parser": "^5.26.0",
29
+ "eslint": "^8.16.0",
30
+ "tsup": "^6.0.1",
31
+ "typescript": "^4.7.2",
32
+ "vite": "^2.9.9"
33
33
  },
34
34
  "dependencies": {
35
35
  "@brillout/libassert": "^0.5.6",
36
- "@vercel/routing-utils": "^1.13.2",
37
- "esbuild": "^0.14.39",
36
+ "@vercel/routing-utils": "^1.13.3",
37
+ "esbuild": "^0.14.42",
38
38
  "fast-glob": "^3.2.11",
39
- "zod": "^3.16.0"
39
+ "zod": "^3.17.3"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsup",