-
-
Notifications
You must be signed in to change notification settings - Fork 788
feat: add bunny preset
#4025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sandros94
wants to merge
6
commits into
nitrojs:main
Choose a base branch
from
sandros94:feat/bunny-preset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add bunny preset
#4025
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58fb03f
feat(bunny): add new preset
sandros94 aa35ab5
fix(bunny): preset entry
sandros94 e8c3aa0
test(bunny): properly kill spawned test server
sandros94 ae8ae09
fix(bunny): support `serveStatic: false`
sandros94 8006e45
chore: apply automated updates
autofix-ci[bot] 9dfded7
Merge branch 'main' into feat/bunny-preset
sandros94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { defineNitroPreset } from "../_utils/preset.ts"; | ||
| import type { Nitro } from "nitro/types"; | ||
| import { builtinModules } from "node:module"; | ||
| import { rm } from "node:fs/promises"; | ||
|
|
||
| const edgeScripting = defineNitroPreset( | ||
| { | ||
| entry: "./bunny/runtime/edge-scripting", | ||
|
|
||
| exportConditions: ["deno"], | ||
| commands: { | ||
| preview: "deno -A ./bunny-edge-scripting.mjs", | ||
| }, | ||
|
|
||
| output: { | ||
| dir: "{{ rootDir }}/.output", | ||
| serverDir: "{{ output.dir }}", | ||
| publicDir: "{{ output.dir }}/public", | ||
| }, | ||
|
|
||
| rollupConfig: { | ||
| output: { | ||
| format: "esm", | ||
| entryFileNames: "bunny-edge-scripting.mjs", | ||
| inlineDynamicImports: true, | ||
| hoistTransitiveImports: false, | ||
| }, | ||
| external: (id: string) => | ||
| id.startsWith("https://") || id.startsWith("node:") || builtinModules.includes(id), | ||
| }, | ||
|
|
||
| serveStatic: "inline", | ||
| minify: true, | ||
|
|
||
| hooks: { | ||
| "build:before": (nitro: Nitro) => { | ||
| if (nitro.options.serveStatic !== "inline" && nitro.options.serveStatic !== false) { | ||
| nitro.options.serveStatic = "inline"; | ||
| nitro.logger.warn( | ||
| "Bunny Edge Scripting preset requires `serveStatic` to be `inline` or `false`. Overriding to `inline`." | ||
| ); | ||
| } | ||
| }, | ||
| async compiled(nitro: Nitro) { | ||
| // Remove public dir when inlined, usecase is for | ||
| // managing assets directly in Bunny Storage | ||
| if (nitro.options.serveStatic === "inline") { | ||
| const publicDir = nitro.options.output.publicDir; | ||
| await rm(publicDir, { recursive: true, force: true }); | ||
| } | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| aliases: ["bunny"], | ||
| name: "bunny-edge-scripting" as const, | ||
| } | ||
| ); | ||
|
|
||
| export default [edgeScripting] as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import "#nitro/virtual/polyfills"; | ||
| import { useNitroApp } from "nitro/app"; | ||
|
|
||
| const nitroApp = useNitroApp(); | ||
|
|
||
| // @ts-expect-error | ||
| if (typeof Bunny !== "undefined") { | ||
| // @ts-expect-error | ||
| Bunny.v1.serve(nitroApp.fetch); | ||
| } else { | ||
| const _parsedPort = Number.parseInt(process.env.NITRO_PORT ?? process.env.PORT ?? ""); | ||
|
|
||
| Deno.serve( | ||
| { | ||
| port: Number.isNaN(_parsedPort) ? 3000 : _parsedPort, | ||
| hostname: process.env.NITRO_HOST || process.env.HOST, | ||
| }, | ||
| nitroApp.fetch | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { promises as fsp } from "node:fs"; | ||
| import { execa, execaCommandSync } from "execa"; | ||
| import { getRandomPort, waitForPort } from "get-port-please"; | ||
| import { resolve } from "pathe"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { setupTest, testNitro } from "../tests.ts"; | ||
|
|
||
| const hasDeno = | ||
| execaCommandSync("deno --version", { stdio: "ignore", reject: false }).exitCode === 0; | ||
|
|
||
| describe.runIf(hasDeno)("nitro:preset:bunny", async () => { | ||
| const ctx = await setupTest("bunny-edge-scripting"); | ||
|
|
||
| testNitro(ctx, async () => { | ||
| const port = await getRandomPort(); | ||
| const p = execa("deno", ["run", "-A", "./bunny-edge-scripting.mjs"], { | ||
| cwd: ctx.outDir, | ||
| stdio: "ignore", | ||
| env: { | ||
| NITRO_PORT: String(port), | ||
| NITRO_HOST: "127.0.0.1", | ||
| }, | ||
| }); | ||
| ctx.server = { | ||
| url: `http://127.0.0.1:${port}`, | ||
| close: () => p.kill(), | ||
| } as any; | ||
| await waitForPort(port, { delay: 1000, retries: 20, host: "127.0.0.1" }); | ||
| return async ({ url, ...opts }) => { | ||
| const res = await ctx.fetch(url, opts); | ||
| return res; | ||
| }; | ||
| }); | ||
|
|
||
| it("should generate the bunny-edge-scripting.mjs file", async () => { | ||
| const serverFiles = await fsp.readdir(resolve(ctx.outDir)); | ||
| expect(serverFiles).toContain("bunny-edge-scripting.mjs"); | ||
| }); | ||
|
|
||
| it("should not have a separate server directory", async () => { | ||
| const serverFiles = await fsp.readdir(resolve(ctx.outDir)); | ||
| expect(serverFiles).not.toContain("server"); | ||
| }); | ||
|
|
||
| it("should not have a public directory (assets should be inlined)", async () => { | ||
| const serverFiles = await fsp.readdir(resolve(ctx.outDir)); | ||
| if (ctx.nitro?.options.serveStatic === "inline") { | ||
| expect(serverFiles).not.toContain("public"); | ||
| } else { | ||
| expect(serverFiles).toContain("public"); | ||
| } | ||
| }); | ||
|
|
||
| it("should have minified output", async () => { | ||
| const entry = await fsp.readFile(resolve(ctx.outDir, "bunny-edge-scripting.mjs"), "utf8"); | ||
| // Check file is actually minified - should have very few newlines relative to size | ||
| const newlineCount = (entry.match(/\n/g) || []).length; | ||
| const ratio = entry.length / Math.max(1, newlineCount); | ||
| // Fixture is small, so we expect a high ratio of characters to newlines in minified code | ||
| expect(ratio).toBeGreaterThan(500); | ||
| }); | ||
|
|
||
| it("should contain the Bunny.v1.serve call", async () => { | ||
| const entry = await fsp.readFile(resolve(ctx.outDir, "bunny-edge-scripting.mjs"), "utf8"); | ||
| expect(entry).toContain("Bunny"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.