Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 1 addition & 55 deletions src/presets/zeabur/preset.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
import fsp from "node:fs/promises";
import { writeFile } from "../_utils/fs.ts";
import { defineNitroPreset } from "../_utils/preset.ts";
import type { Nitro } from "nitro/types";
import { dirname, relative, resolve } from "pathe";

// https://zeabur.com/docs/advanced/serverless-output-format

const zeabur = defineNitroPreset(
{
entry: "./zeabur/runtime/zeabur",
output: {
dir: "{{ rootDir }}/.zeabur/output",
serverDir: "{{ output.dir }}/functions/__nitro.func",
publicDir: "{{ output.dir }}/static",
},
hooks: {
async compiled(nitro: Nitro) {
const buildConfigPath = resolve(
nitro.options.output.dir,
"config.json"
);
const cfg = {
containerized: false,
routes: [{ src: ".*", dest: "/__nitro" }],
};
await writeFile(buildConfigPath, JSON.stringify(cfg, null, 2));

// Write ISR functions
for (const [key, value] of Object.entries(nitro.options.routeRules)) {
if (!value.isr) {
continue;
}
const funcPrefix = resolve(
nitro.options.output.serverDir,
".." + key
);
await fsp.mkdir(dirname(funcPrefix), { recursive: true });
await fsp.symlink(
"./" +
relative(dirname(funcPrefix), nitro.options.output.serverDir),
funcPrefix + ".func",
"junction"
);
await writeFile(
funcPrefix + ".prerender-config.json",
JSON.stringify({ type: "Prerender" })
);
}
},
},
extends: "node-server",
},
{
name: "zeabur" as const,
Expand All @@ -59,17 +13,9 @@ const zeabur = defineNitroPreset(
const zeaburStatic = defineNitroPreset(
{
extends: "static",
output: {
dir: "{{ rootDir }}/.zeabur/output",
publicDir: "{{ output.dir }}/static",
},
commands: {
preview: "npx serve ./static",
},
},
{
name: "zeabur-static" as const,

static: true,
}
);
Expand Down
35 changes: 35 additions & 0 deletions test/presets/zeabur.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { existsSync } from "node:fs";
import fsp from "node:fs/promises";
import { resolve } from "pathe";
import { describe, expect, it } from "vitest";
import { setupTest } from "../tests.ts";

describe("nitro:preset:zeabur-static", async () => {
const ctx = await setupTest("zeabur-static");

it("should not generate a server folder", async () => {
const contents = await fsp.readdir(resolve(ctx.outDir));
expect(contents).toMatchInlineSnapshot(`
[
"nitro.json",
"public",
]
`);
});

it("output has public directory", async () => {
// make output directory aligned with `nuxt generate`'s
// output directory, which is ".output/public"
expect(existsSync(resolve(ctx.outDir, "public", "favicon.ico"))).toBe(true);
});
});

describe("nitro:preset:zeabur", async () => {
const ctx = await setupTest("zeabur");

it("output directory is .output/server and has index.mjs", async () => {
// Zeabur sets ".output/server/index.mjs" as the entry point
// https://github.com/zeabur/zbpack/blob/b8d76b5758fed32203cbdbf0456a9bc5c948dfc0/internal/nodejs/plan.go#L883
expect(existsSync(resolve(ctx.outDir, "server", "index.mjs"))).toBe(true);
});
});
Loading