@kiln-js/adapter-vercel 0.1.0 → 0.1.4
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 +43 -0
- package/dist/index.js +43 -25
- package/package.json +10 -2
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @kiln-js/adapter-vercel
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@kiln-js/adapter-vercel)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
Part of the Kiln framework.
|
|
7
|
+
|
|
8
|
+
Part of [Kiln](https://github.com/kiln-js/kiln), a file-based server-side rendering framework for React and Vite.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bun add -d @kiln-js/adapter-vercel
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { adaptVercel } from "@kiln-js/adapter-vercel";
|
|
20
|
+
|
|
21
|
+
await adaptVercel({
|
|
22
|
+
root: process.cwd(),
|
|
23
|
+
clientOut: "dist/client",
|
|
24
|
+
serverOut: "dist/server",
|
|
25
|
+
clientRel: "app/client.tsx",
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
Bun at build time, and the Edge runtime only. Edge functions cannot use Node built-ins such as `node:fs`.
|
|
32
|
+
|
|
33
|
+
## Documentation
|
|
34
|
+
|
|
35
|
+
See the [Kiln repository](https://github.com/kiln-js/kiln) for the full API reference and guides.
|
|
36
|
+
|
|
37
|
+
## Development
|
|
38
|
+
|
|
39
|
+
This repository is generated from the Kiln monorepo. Make changes there, then run `scripts/publish-repo.ps1` to publish them here. Report issues in this repository, and send pull requests to the monorepo.
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
// packages/adapter-vercel/src/index.ts
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { build } from "esbuild";
|
|
5
|
+
function readVercelJson(root) {
|
|
6
|
+
const file = path.join(root, "vercel.json");
|
|
7
|
+
if (!fs.existsSync(file)) return {};
|
|
8
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
9
|
+
}
|
|
5
10
|
async function adaptVercel(opts) {
|
|
6
11
|
const outRoot = path.join(opts.root, ".vercel", "output");
|
|
7
12
|
const staticDir = path.join(outRoot, "static");
|
|
@@ -12,42 +17,55 @@ async function adaptVercel(opts) {
|
|
|
12
17
|
const manifestPath = path.join(opts.clientOut, ".vite", "manifest.json");
|
|
13
18
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
14
19
|
const entry = manifest[opts.clientRel];
|
|
15
|
-
if (!entry)
|
|
16
|
-
throw new Error(`No manifest entry for ${opts.clientRel}`);
|
|
20
|
+
if (!entry) throw new Error(`No manifest entry for ${opts.clientRel}`);
|
|
17
21
|
const assets = {
|
|
18
|
-
head: (entry.css ?? []).map((href) => `<link rel="stylesheet" href="/${href}">`).join(
|
|
19
|
-
`),
|
|
22
|
+
head: (entry.css ?? []).map((href) => `<link rel="stylesheet" href="/${href}">`).join("\n"),
|
|
20
23
|
entry: `/${entry.file}`
|
|
21
24
|
};
|
|
22
25
|
const entryFile = path.join(opts.root, ".kiln", "vercel-entry.ts");
|
|
23
26
|
fs.mkdirSync(path.dirname(entryFile), { recursive: true });
|
|
24
|
-
fs.writeFileSync(
|
|
27
|
+
fs.writeFileSync(
|
|
28
|
+
entryFile,
|
|
29
|
+
`import { createHandler } from ${JSON.stringify(path.join(opts.serverOut, "server.js"))};
|
|
25
30
|
|
|
26
31
|
const handler = createHandler(${JSON.stringify(assets)});
|
|
27
32
|
|
|
28
33
|
export default function edge(request: Request): Promise<Response> {
|
|
29
34
|
return handler(request);
|
|
30
35
|
}
|
|
31
|
-
`
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
`
|
|
37
|
+
);
|
|
38
|
+
const result = await build({
|
|
39
|
+
entryPoints: [entryFile],
|
|
40
|
+
bundle: true,
|
|
41
|
+
write: false,
|
|
42
|
+
platform: "browser",
|
|
35
43
|
format: "esm",
|
|
44
|
+
target: "es2022",
|
|
45
|
+
jsx: "automatic",
|
|
36
46
|
minify: true,
|
|
37
|
-
define: { "process.env.NODE_ENV":
|
|
47
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
48
|
+
logLevel: "silent"
|
|
38
49
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
fs.writeFileSync(path.join(funcDir, ".vc-config.json"), JSON.stringify({ runtime: "edge", entrypoint: "index.js" }, null, 2));
|
|
47
|
-
fs.writeFileSync(path.join(outRoot, "config.json"), JSON.stringify({
|
|
50
|
+
fs.writeFileSync(path.join(funcDir, "index.js"), result.outputFiles[0].contents);
|
|
51
|
+
fs.writeFileSync(
|
|
52
|
+
path.join(funcDir, ".vc-config.json"),
|
|
53
|
+
JSON.stringify({ runtime: "edge", entrypoint: "index.js" }, null, 2)
|
|
54
|
+
);
|
|
55
|
+
const custom = readVercelJson(opts.root);
|
|
56
|
+
const config = {
|
|
48
57
|
version: 3,
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
headers: custom.headers ?? [],
|
|
59
|
+
redirects: custom.redirects ?? [],
|
|
60
|
+
routes: [
|
|
61
|
+
{ handle: "filesystem" },
|
|
62
|
+
...(custom.rewrites ?? []).map((r) => ({ src: r.source, dest: r.destination })),
|
|
63
|
+
{ src: "/(.*)", dest: "/index" }
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
if (custom.cleanUrls !== void 0) config.cleanUrls = custom.cleanUrls;
|
|
67
|
+
if (custom.trailingSlash !== void 0) config.trailingSlash = custom.trailingSlash;
|
|
68
|
+
fs.writeFileSync(path.join(outRoot, "config.json"), JSON.stringify(config, null, 2));
|
|
51
69
|
}
|
|
52
70
|
export {
|
|
53
71
|
adaptVercel
|
package/package.json
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiln-js/adapter-vercel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "Vercel Edge output for the Kiln framework",
|
|
5
|
+
"license": "MIT",
|
|
4
6
|
"type": "module",
|
|
5
7
|
"exports": {
|
|
6
8
|
".": {
|
|
@@ -11,10 +13,16 @@
|
|
|
11
13
|
"files": [
|
|
12
14
|
"dist"
|
|
13
15
|
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": "\u003e=18"
|
|
18
|
+
},
|
|
14
19
|
"scripts": {
|
|
15
|
-
"build": "
|
|
20
|
+
"build": "node ../../scripts/build.mjs adapter-vercel",
|
|
16
21
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
17
22
|
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"esbuild": "^0.21.5"
|
|
25
|
+
},
|
|
18
26
|
"devDependencies": {
|
|
19
27
|
"@types/node": "^22.0.0"
|
|
20
28
|
}
|