diff --git a/packages/cli/package.json b/packages/cli/package.json index 23436472d9..f49ae06b22 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -41,6 +41,7 @@ "@types/mock-fs": "^4.13.1", "@types/node": "16", "@types/node-fetch": "^2.6.2", + "@types/semver": "^7.3.13", "rimraf": "^3.0.2", "tsup": "^6.5.0", "type-fest": "^3.6.0", @@ -80,6 +81,7 @@ "path-to-regexp": "^6.2.1", "posthog-node": "^3.1.1", "proxy-agent": "^6.3.0", + "semver": "^7.5.0", "simple-git": "^3.19.0", "terminal-link": "^3.0.0", "tsconfck": "^2.1.2", diff --git a/packages/cli/src/frameworks/nextjs/index.ts b/packages/cli/src/frameworks/nextjs/index.ts index d407107531..af879ab9ef 100644 --- a/packages/cli/src/frameworks/nextjs/index.ts +++ b/packages/cli/src/frameworks/nextjs/index.ts @@ -12,6 +12,9 @@ import { readPackageJson } from "../../utils/readPackageJson"; import { standardWatchFilePaths } from "../watchConfig"; import { telemetryClient } from "../../telemetry/telemetry"; import { detectMiddlewareUsage } from "./middleware"; +import semver from "semver"; +import boxen from "boxen"; +import { createRequire } from "node:module"; export class NextJs implements Framework { id = "nextjs"; @@ -89,6 +92,58 @@ export class NextJs implements Framework { break; } } + + const nextJsDir = await detectPagesOrAppDir(path); + const nextConfigExists = await detectNextConfigFile(path); + + const require = createRequire(import.meta.url); + const nextPath = require.resolve("next"); + const reactPath = require.resolve("@trigger.dev/react"); + + if (nextConfigExists && nextJsDir === "pages" && typeof reactPath !== "undefined") { + const nextPackageJsonPath = pathModule.join(nextPath, "../../../", "package.json"); + const nextPackageJsonData = JSON.parse(await fs.readFile(nextPackageJsonPath, "utf8")); + + const nextVersion = nextPackageJsonData.version; + + if (semver.gte(nextVersion, "13.1.0")) { + logger.warn( + `⚠️ We've detected you're using Next.js Pages, please add the following to your next.config` + ); + logger.info( + boxen( + ` +const nextConfig = { +// existing code +transpilePackages: ["@trigger.dev/react"], +}; +module.exports = nextConfig; +`, + { padding: 1, borderStyle: "double", borderColor: "magenta" } + ) + ); + } + + if (semver.lt(nextVersion, "13.1.0")) { + logger.warn( + `⚠️ We've detected you're using Next.js Pages, please add the following to your next.config` + ); + logger.info( + boxen( + ` +Add the following to your next.config file: + +const withTM = require('next-transpile-modules')(['@trigger.dev/react']); + +module.exports = withTM({ +//...existing config object +}); +`, + { margin: 1, borderStyle: "double", borderColor: "magenta" } + ) + ); + } + } } defaultHostnames = ["localhost"];