Skip to content

Commit 44af1ad

Browse files
authored
chore: refactor withPayload back to js (#15159)
withPayload file has been migrated to ts however this breaks being able to use `pnpm dev` in our templates Fixes #14994
1 parent ec658a4 commit 44af1ad

4 files changed

Lines changed: 39 additions & 36 deletions

File tree

packages/next/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"default": "./src/index.js"
3333
},
3434
"./withPayload": {
35-
"import": "./src/withPayload/withPayload.ts",
36-
"default": "./src/withPayload/withPayload.ts"
35+
"import": "./src/withPayload/withPayload.js",
36+
"default": "./src/withPayload/withPayload.js"
3737
},
3838
"./layouts": {
3939
"import": "./src/exports/layouts.ts",

packages/next/src/withPayload/withPayload.ts renamed to packages/next/src/withPayload/withPayload.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* eslint-disable no-console */
2-
/* eslint-disable no-restricted-exports */
3-
import type { NextConfig } from 'next'
4-
1+
/**
2+
* These files must remain as plain JavaScript (.js) rather than TypeScript (.ts) because they are
3+
* imported directly in next.config.mjs files. Since next.config files run before the build process,
4+
* TypeScript compilation is not available. This ensures compatibility with all templates and
5+
* user projects regardless of their TypeScript setup.
6+
*/
57
import {
68
getNextjsVersion,
79
supportsTurbopackExternalizeTransitiveDependencies,
@@ -18,10 +20,7 @@ const poweredByHeader = {
1820
* @param {Object} [options] - Optional configuration options
1921
* @param {boolean} [options.devBundleServerPackages] - Whether to bundle server packages in development mode. @default false
2022
* */
21-
export const withPayload = (
22-
nextConfig: NextConfig = {},
23-
options: { devBundleServerPackages?: boolean } = {},
24-
): NextConfig => {
23+
export const withPayload = (nextConfig = {}, options = {}) => {
2524
const nextjsVersion = getNextjsVersion()
2625

2726
const supportsTurbopackBuild = supportsTurbopackExternalizeTransitiveDependencies(nextjsVersion)
@@ -35,7 +34,8 @@ export const withPayload = (
3534
env.NEXT_PUBLIC_ENABLE_ROUTER_CACHE_REFRESH = 'true'
3635
}
3736

38-
const baseConfig: NextConfig = {
37+
/** @type {import('next').NextConfig} */
38+
const baseConfig = {
3939
...nextConfig,
4040
env,
4141
outputFileTracingExcludes: {

packages/next/src/withPayload/withPayload.utils.ts renamed to packages/next/src/withPayload/withPayload.utils.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-console */
1+
22
/**
33
* This was taken and modified from https://github.com/getsentry/sentry-javascript/blob/15256034ee8150a5b7dcb97d23eca1a5486f0cae/packages/nextjs/src/config/util.ts
44
*
@@ -28,34 +28,35 @@
2828
import { readFileSync } from 'fs'
2929
import { fileURLToPath } from 'url'
3030

31-
function _parseInt(input: string | undefined): number {
31+
/**
32+
* @param {string | undefined} input
33+
* @returns {number}
34+
*/
35+
function _parseInt(input) {
3236
return parseInt(input || '', 10)
3337
}
3438

3539
/**
3640
* Represents Semantic Versioning object
41+
* @typedef {Object} SemVer
42+
* @property {string} [buildmetadata]
43+
* @property {number} [canaryVersion] - undefined if not a canary version
44+
* @property {number} [major]
45+
* @property {number} [minor]
46+
* @property {number} [patch]
47+
* @property {string} [prerelease]
3748
*/
38-
type SemVer = {
39-
buildmetadata?: string
40-
/**
41-
* undefined if not a canary version
42-
*/
43-
canaryVersion?: number
44-
major?: number
45-
minor?: number
46-
patch?: number
47-
prerelease?: string
48-
}
4949

5050
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
5151
const SEMVER_REGEXP =
5252
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-z-][0-9a-z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-z-][0-9a-z-]*))*))?(?:\+([0-9a-z-]+(?:\.[0-9a-z-]+)*))?$/i
5353

5454
/**
5555
* Parses input into a SemVer interface
56-
* @param input string representation of a semver version
56+
* @param {string} input - string representation of a semver version
57+
* @returns {SemVer}
5758
*/
58-
export function parseSemver(input: string): SemVer {
59+
export function parseSemver(input) {
5960
const match = input.match(SEMVER_REGEXP) || []
6061
const major = _parseInt(match[1])
6162
const minor = _parseInt(match[2])
@@ -78,10 +79,12 @@ export function parseSemver(input: string): SemVer {
7879

7980
/**
8081
* Returns the version of Next.js installed in the project, or undefined if it cannot be determined.
82+
* @returns {SemVer | undefined}
8183
*/
82-
export function getNextjsVersion(): SemVer | undefined {
84+
export function getNextjsVersion() {
8385
try {
84-
let pkgPath: string
86+
/** @type {string} */
87+
let pkgPath
8588

8689
// Check if we're in ESM or CJS environment
8790
if (typeof import.meta?.resolve === 'function') {
@@ -106,10 +109,10 @@ export function getNextjsVersion(): SemVer | undefined {
106109
/**
107110
* Checks if the current Next.js version supports Turbopack externalize transitive dependencies.
108111
* This was introduced in Next.js v16.1.0-canary.3
112+
* @param {SemVer | undefined} version
113+
* @returns {boolean}
109114
*/
110-
export function supportsTurbopackExternalizeTransitiveDependencies(
111-
version: SemVer | undefined,
112-
): boolean {
115+
export function supportsTurbopackExternalizeTransitiveDependencies(version) {
113116
if (!version) {
114117
return false
115118
}

packages/next/src/withPayload/withPayloadLegacy.ts renamed to packages/next/src/withPayload/withPayloadLegacy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/* eslint-disable no-console */
2-
import type { NextConfig } from 'next'
3-
41
/**
52
* Applies config options required to support Next.js versions before 16.1.0 and 16.1.0-canary.3.
3+
* @param {import('next').NextConfig} nextConfig
4+
* @returns {import('next').NextConfig}
65
*/
7-
export const withPayloadLegacy = (nextConfig: NextConfig = {}): NextConfig => {
6+
export const withPayloadLegacy = (nextConfig = {}) => {
87
if (process.env.PAYLOAD_PATCH_TURBOPACK_WARNINGS !== 'false') {
98
// TODO: This warning is thrown because we cannot externalize the entry-point package for client-s3, so we patch the warning to not show it.
109
// We can remove this once Next.js implements https://github.com/vercel/next.js/discussions/76991
@@ -52,7 +51,8 @@ export const withPayloadLegacy = (nextConfig: NextConfig = {}): NextConfig => {
5251
)
5352
}
5453

55-
const toReturn: NextConfig = {
54+
/** @type {import('next').NextConfig} */
55+
const toReturn = {
5656
...nextConfig,
5757
serverExternalPackages: [
5858
// serverExternalPackages = webpack.externals, but with turbopack support and an additional check

0 commit comments

Comments
 (0)