|
| 1 | +import { statSync } from "node:fs"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | +import { brandColor, dim } from "@cloudflare/cli/colors"; |
| 4 | +import { spinner } from "@cloudflare/cli/interactive"; |
| 5 | +import { dedent } from "../../utils/dedent"; |
| 6 | +import { installPackages } from "../c3-vendor/packages"; |
| 7 | +import { AutoConfigFrameworkConfigurationError } from "../errors"; |
| 8 | +import { appendToGitIgnore } from "../git"; |
| 9 | +import { usesTypescript } from "../uses-typescript"; |
| 10 | +import { Framework } from "."; |
| 11 | +import type { ConfigurationOptions } from "."; |
| 12 | +import type { RawConfig } from "@cloudflare/workers-utils"; |
| 13 | + |
| 14 | +export class NextJs extends Framework { |
| 15 | + name = "next.js"; |
| 16 | + |
| 17 | + preview = "opennextjs-cloudflare build && opennextjs-cloudflare preview"; |
| 18 | + deploy = "opennextjs-cloudflare build && opennextjs-cloudflare deploy"; |
| 19 | + |
| 20 | + async configure({ |
| 21 | + dryRun, |
| 22 | + projectPath, |
| 23 | + }: ConfigurationOptions): Promise<RawConfig> { |
| 24 | + const usesTs = usesTypescript(projectPath); |
| 25 | + |
| 26 | + const nextConfigPath = probeForNextConfigPath(usesTs); |
| 27 | + if (!nextConfigPath) { |
| 28 | + throw new AutoConfigFrameworkConfigurationError( |
| 29 | + "No Next.js configuration file could be detected. Note: only next.config.ts and next.config.mjs files are supported." |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + if (!dryRun) { |
| 34 | + await installPackages(["@opennextjs/cloudflare@^1.3.0"], { |
| 35 | + startText: "Installing @opennextjs/cloudflare adapter", |
| 36 | + doneText: `${brandColor("installed")}`, |
| 37 | + }); |
| 38 | + |
| 39 | + await updateNextConfig(nextConfigPath); |
| 40 | + |
| 41 | + await createOpenNextConfigFile(projectPath); |
| 42 | + |
| 43 | + await appendToGitIgnore( |
| 44 | + projectPath, |
| 45 | + dedent` |
| 46 | + # OpenNext |
| 47 | + .open-next |
| 48 | + `, |
| 49 | + { |
| 50 | + startText: "Adding open-next section to .gitignore file", |
| 51 | + doneText: `${brandColor(`added`)} open-next section to .gitignore file`, |
| 52 | + } |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return { |
| 57 | + main: ".open-next/worker.js", |
| 58 | + compatibility_flags: ["nodejs_compat"], |
| 59 | + assets: { |
| 60 | + binding: "ASSETS", |
| 61 | + directory: ".open-next/assets", |
| 62 | + }, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + configurationDescription = "Configuring project for Next.js with OpenNext"; |
| 67 | +} |
| 68 | + |
| 69 | +function probeForNextConfigPath(usesTs: boolean): string | undefined { |
| 70 | + const pathsToProbe = [ |
| 71 | + ...(usesTs ? ["next.config.ts"] : []), |
| 72 | + "next.config.mjs", |
| 73 | + ]; |
| 74 | + |
| 75 | + for (const path of pathsToProbe) { |
| 76 | + const stats = statSync(`next.config.${usesTs ? "ts" : "mjs"}`, { |
| 77 | + throwIfNoEntry: false, |
| 78 | + }); |
| 79 | + if (stats?.isFile()) { |
| 80 | + return path; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async function updateNextConfig(nextConfigPath: string) { |
| 86 | + const s = spinner(); |
| 87 | + |
| 88 | + s.start(`Updating \`${nextConfigPath}\``); |
| 89 | + |
| 90 | + const configContent = await readFile(nextConfigPath); |
| 91 | + |
| 92 | + const updatedConfigFile = |
| 93 | + configContent + |
| 94 | + ` |
| 95 | + // added by create cloudflare to enable calling \`getCloudflareContext()\` in \`next dev\` |
| 96 | + import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'; |
| 97 | + initOpenNextCloudflareForDev(); |
| 98 | + `.replace(/\n\t*/g, "\n"); |
| 99 | + |
| 100 | + await writeFile(nextConfigPath, updatedConfigFile); |
| 101 | + |
| 102 | + s.stop(`${brandColor(`updated`)} ${dim(`\`${nextConfigPath}\``)}`); |
| 103 | +} |
| 104 | + |
| 105 | +async function createOpenNextConfigFile(projectPath: string) { |
| 106 | + const s = spinner(); |
| 107 | + |
| 108 | + s.start("Creating open-next.config.ts file"); |
| 109 | + |
| 110 | + await writeFile( |
| 111 | + // TODO: this always saves the file as open-next.config.ts, is a js alternative also supported? |
| 112 | + // (since the project might not be using TypeScript) |
| 113 | + `${projectPath}/open-next.config.ts`, |
| 114 | + dedent`import { defineCloudflareConfig } from "@opennextjs/cloudflare"; |
| 115 | +
|
| 116 | + export default defineCloudflareConfig({ |
| 117 | + // Uncomment to enable R2 cache, |
| 118 | + // It should be imported as: |
| 119 | + // \`import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";\` |
| 120 | + // See https://opennext.js.org/cloudflare/caching for more details |
| 121 | + // incrementalCache: r2IncrementalCache, |
| 122 | + }); |
| 123 | + ` |
| 124 | + ); |
| 125 | + |
| 126 | + s.stop(`${brandColor("created")} open-next.config.ts file`); |
| 127 | +} |
0 commit comments