-
Notifications
You must be signed in to change notification settings - Fork 1k
graduate next + assets template #8741
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "create-cloudflare": patch | ||
| --- | ||
|
|
||
| Graduate Next.js + Workers Assets template from experimental | ||
|
|
||
| You no longer need the `--experimental` flag to access this template. |
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
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 |
|---|---|---|
| @@ -1,223 +1,9 @@ | ||
| import { join } from "path"; | ||
| import { updateStatus, warn } from "@cloudflare/cli"; | ||
| import { brandColor, dim } from "@cloudflare/cli/colors"; | ||
| import { inputPrompt, spinner } from "@cloudflare/cli/interactive"; | ||
| import { runFrameworkGenerator } from "frameworks/index"; | ||
| import { | ||
| copyFile, | ||
| probePaths, | ||
| readFile, | ||
| readJSON, | ||
| usesEslint, | ||
| usesTypescript, | ||
| writeFile, | ||
| writeJSON, | ||
| } from "helpers/files"; | ||
| import { detectPackageManager } from "helpers/packageManagers"; | ||
| import { installPackages } from "helpers/packages"; | ||
| import { getTemplatePath } from "../../src/templates"; | ||
| import type { TemplateConfig } from "../../src/templates"; | ||
| import type { C3Context } from "types"; | ||
| import pages from "./pages/c3"; | ||
| import workers from "./workers/c3"; | ||
| import type { MultiPlatformTemplateConfig } from "../../src/templates"; | ||
|
|
||
| const { npm, npx } = detectPackageManager(); | ||
|
|
||
| const generate = async (ctx: C3Context) => { | ||
| const projectName = ctx.project.name; | ||
|
|
||
| await runFrameworkGenerator(ctx, [projectName]); | ||
|
|
||
| const wranglerConfig = readFile(join(getTemplatePath(ctx), "wrangler.jsonc")); | ||
| writeFile(join(ctx.project.path, "wrangler.jsonc"), wranglerConfig); | ||
| updateStatus("Created wrangler.jsonc file"); | ||
| }; | ||
|
|
||
| const updateNextConfig = (usesTs: boolean) => { | ||
| const s = spinner(); | ||
|
|
||
| const configFile = `next.config.${usesTs ? "ts" : "mjs"}`; | ||
| s.start(`Updating \`${configFile}\``); | ||
|
|
||
| const configContent = readFile(configFile); | ||
|
|
||
| const updatedConfigFile = | ||
| `import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev'; | ||
|
|
||
| // Here we use the @cloudflare/next-on-pages next-dev module to allow us to | ||
| // use bindings during local development (when running the application with | ||
| // \`next dev\`). This function is only necessary during development and | ||
| // has no impact outside of that. For more information see: | ||
| // https://github.com/cloudflare/next-on-pages/blob/main/internal-packages/next-dev/README.md | ||
| setupDevPlatform().catch(console.error); | ||
|
|
||
| `.replace(/\n\t*/g, "\n") + configContent; | ||
|
|
||
| writeFile(configFile, updatedConfigFile); | ||
|
|
||
| s.stop(`${brandColor(`updated`)} ${dim(`\`${configFile}\``)}`); | ||
| }; | ||
|
|
||
| const configure = async (ctx: C3Context) => { | ||
| const projectPath = ctx.project.path; | ||
|
|
||
| // Add a compatible function handler example | ||
| const path = probePaths([ | ||
| `${projectPath}/pages/api`, | ||
| `${projectPath}/src/pages/api`, | ||
| `${projectPath}/src/app/api`, | ||
| `${projectPath}/app/api`, | ||
| `${projectPath}/src/app`, | ||
| `${projectPath}/app`, | ||
| ]); | ||
|
|
||
| if (!path) { | ||
| throw new Error("Could not find the `/api` or `/app` directory"); | ||
| } | ||
|
|
||
| const usesTs = usesTypescript(ctx); | ||
|
|
||
| if (usesTs) { | ||
| copyFile( | ||
| join(getTemplatePath(ctx), "env.d.ts"), | ||
| join(projectPath, "env.d.ts"), | ||
| ); | ||
| updateStatus("Created an env.d.ts file"); | ||
| } | ||
|
|
||
| const installEslintPlugin = await shouldInstallNextOnPagesEslintPlugin(ctx); | ||
|
|
||
| if (installEslintPlugin) { | ||
| await writeEslintrc(ctx); | ||
| } | ||
|
|
||
| updateNextConfig(usesTs); | ||
|
|
||
| copyFile( | ||
| join(getTemplatePath(ctx), "README.md"), | ||
| join(projectPath, "README.md"), | ||
| ); | ||
| updateStatus("Updated the README file"); | ||
|
|
||
| await addDevDependencies(installEslintPlugin); | ||
| }; | ||
|
|
||
| export const shouldInstallNextOnPagesEslintPlugin = async ( | ||
| ctx: C3Context, | ||
| ): Promise<boolean> => { | ||
| const eslintUsage = usesEslint(ctx); | ||
|
|
||
| if (!eslintUsage.used) { | ||
| return false; | ||
| } | ||
|
|
||
| if (eslintUsage.configType !== ".eslintrc.json") { | ||
| warn( | ||
| `Expected .eslintrc.json from Next.js scaffolding but found ${eslintUsage.configType} instead`, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| return await inputPrompt({ | ||
| type: "confirm", | ||
| question: "Do you want to use the next-on-pages eslint-plugin?", | ||
| label: "eslint-plugin", | ||
| defaultValue: true, | ||
| }); | ||
| }; | ||
|
|
||
| export const writeEslintrc = async (ctx: C3Context): Promise<void> => { | ||
| const eslintConfig = readJSON(`${ctx.project.path}/.eslintrc.json`) as { | ||
| plugins: string[]; | ||
| extends: string | string[]; | ||
| }; | ||
|
|
||
| eslintConfig.plugins ??= []; | ||
| eslintConfig.plugins.push("eslint-plugin-next-on-pages"); | ||
|
|
||
| if (typeof eslintConfig.extends === "string") { | ||
| eslintConfig.extends = [eslintConfig.extends]; | ||
| } | ||
| eslintConfig.extends ??= []; | ||
| eslintConfig.extends.push("plugin:eslint-plugin-next-on-pages/recommended"); | ||
|
|
||
| writeJSON(`${ctx.project.path}/.eslintrc.json`, eslintConfig); | ||
| }; | ||
|
|
||
| const addDevDependencies = async (installEslintPlugin: boolean) => { | ||
| const packages = [ | ||
| "@cloudflare/next-on-pages@1", | ||
| "@cloudflare/workers-types", | ||
| "vercel", | ||
| ...(installEslintPlugin ? ["eslint-plugin-next-on-pages"] : []), | ||
| ]; | ||
| await installPackages(packages, { | ||
| dev: true, | ||
| startText: "Adding the Cloudflare Pages adapter", | ||
| doneText: `${brandColor(`installed`)} ${dim(packages.join(", "))}`, | ||
| }); | ||
| }; | ||
|
|
||
| export default { | ||
| configVersion: 1, | ||
| id: "next", | ||
| frameworkCli: "create-next-app", | ||
| platform: "pages", | ||
| const config: MultiPlatformTemplateConfig = { | ||
| displayName: "Next.js", | ||
| generate, | ||
| configure, | ||
| copyFiles: { | ||
| async selectVariant(ctx) { | ||
| const isApp = probePaths([ | ||
| `${ctx.project.path}/src/app`, | ||
| `${ctx.project.path}/app`, | ||
| ]); | ||
|
|
||
| const isTypescript = usesTypescript(ctx); | ||
|
|
||
| const dir = isApp ? "app" : "pages"; | ||
| return `${dir}/${isTypescript ? "ts" : "js"}`; | ||
| }, | ||
| destinationDir(ctx) { | ||
| const srcPath = probePaths([`${ctx.project.path}/src`]); | ||
| return srcPath ? "./src" : "./"; | ||
| }, | ||
| variants: { | ||
| "app/ts": { | ||
| path: "./app/ts", | ||
| }, | ||
| "app/js": { | ||
| path: "./app/js", | ||
| }, | ||
| "pages/ts": { | ||
| path: "./pages/ts", | ||
| }, | ||
| "pages/js": { | ||
| path: "./pages/js", | ||
| }, | ||
| }, | ||
| }, | ||
| transformPackageJson: async (_, ctx) => { | ||
| const isNpm = npm === "npm"; | ||
| const isBun = npm === "bun"; | ||
| const isNpmOrBun = isNpm || isBun; | ||
| const nextOnPagesScope = isNpmOrBun ? "@cloudflare/" : ""; | ||
| const nextOnPagesCommand = `${nextOnPagesScope}next-on-pages`; | ||
| const pmCommand = isNpmOrBun ? npx : npm; | ||
| const pagesBuildRunCommand = `${ | ||
| isNpm ? "npm run" : isBun ? "bun" : pmCommand | ||
| } pages:build`; | ||
| return { | ||
| scripts: { | ||
| "pages:build": `${pmCommand} ${nextOnPagesCommand}`, | ||
| preview: `${pagesBuildRunCommand} && wrangler pages dev`, | ||
| deploy: `${pagesBuildRunCommand} && wrangler pages deploy`, | ||
| ...(usesTypescript(ctx) && { | ||
| "cf-typegen": `wrangler types --env-interface CloudflareEnv env.d.ts`, | ||
| }), | ||
| }, | ||
| }; | ||
| }, | ||
| devScript: "dev", | ||
| previewScript: "preview", | ||
| deployScript: "deploy", | ||
| compatibilityFlags: ["nodejs_compat"], | ||
| } as TemplateConfig; | ||
| platformVariants: { pages, workers }, | ||
| }; | ||
| export default config; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment (and the Windows exclusion) actually valid for OpenNext?
Not for this PR but perhaps a quick follow-up? cc @vicb