|
| 1 | +import type { RunnerName } from "env-runner"; |
| 2 | +import type { Nitro } from "nitro/types"; |
| 3 | + |
| 4 | +import { pathToFileURL } from "node:url"; |
| 5 | +import { resolveModulePath } from "exsolve"; |
| 6 | +import { ensureDep } from "../utils/dep.ts"; |
| 7 | + |
| 8 | +export interface MiniflareRunnerDeps { |
| 9 | + miniflare?: URL; |
| 10 | + wranglerModule?: URL; |
| 11 | + [key: string]: unknown; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Resolve the platform packages a dev runner needs from the user project. |
| 16 | + * |
| 17 | + * Runners do not import these packages themselves: each one is passed as an |
| 18 | + * explicit option so the version installed next to the app is the version that |
| 19 | + * runs. Unresolved entries are left out and the runner falls back to its own |
| 20 | + * optional import (or a degraded mode). |
| 21 | + */ |
| 22 | +export async function resolveRunnerDeps( |
| 23 | + nitro: Nitro, |
| 24 | + runner: RunnerName |
| 25 | +): Promise<Record<string, unknown>> { |
| 26 | + switch (runner) { |
| 27 | + case "miniflare": { |
| 28 | + return resolveMiniflareDeps(nitro); |
| 29 | + } |
| 30 | + case "netlify": { |
| 31 | + // `@netlify/runtime` is instantiated inside the worker thread, so it can |
| 32 | + // only be handed over as a specifier, never as an imported module. |
| 33 | + return { netlifyRuntime: _resolve("@netlify/runtime", nitro.options.rootDir) }; |
| 34 | + } |
| 35 | + default: { |
| 36 | + return {}; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export async function resolveMiniflareDeps(nitro: Nitro): Promise<MiniflareRunnerDeps> { |
| 42 | + const miniflare = await ensureDep({ |
| 43 | + id: "miniflare", |
| 44 | + dir: nitro.options.rootDir, |
| 45 | + reason: "the `miniflare` dev runner", |
| 46 | + version: "^4", |
| 47 | + }); |
| 48 | + return { |
| 49 | + miniflare: miniflare ? pathToFileURL(miniflare) : undefined, |
| 50 | + // Optional: without it, a built-in minimal reader handles plain JSON |
| 51 | + // wrangler configs and inline objects. |
| 52 | + wranglerModule: _resolve("wrangler", nitro.options.rootDir), |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function _resolve(id: string, dir: string): URL | undefined { |
| 57 | + const path = resolveModulePath(id, { from: [dir, import.meta.url], try: true }); |
| 58 | + return path ? pathToFileURL(path) : undefined; |
| 59 | +} |
0 commit comments