-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathwithPayload.utils.js
More file actions
183 lines (160 loc) · 5.25 KB
/
withPayload.utils.js
File metadata and controls
183 lines (160 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* This was taken and modified from https://github.com/getsentry/sentry-javascript/blob/15256034ee8150a5b7dcb97d23eca1a5486f0cae/packages/nextjs/src/config/util.ts
*
* MIT License
*
* Copyright (c) 2012 Functional Software, Inc. dba Sentry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
/**
* @param {string | undefined} input
* @returns {number}
*/
function _parseInt(input) {
return parseInt(input || '', 10)
}
/**
* Represents Semantic Versioning object
* @typedef {Object} SemVer
* @property {string} [buildmetadata]
* @property {number} [canaryVersion] - undefined if not a canary version
* @property {number} [major]
* @property {number} [minor]
* @property {number} [patch]
* @property {string} [prerelease]
*/
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const SEMVER_REGEXP =
/^(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
/**
* Parses input into a SemVer interface
* @param {string} input - string representation of a semver version
* @returns {SemVer}
*/
export function parseSemver(input) {
const match = input.match(SEMVER_REGEXP) || []
const major = _parseInt(match[1])
const minor = _parseInt(match[2])
const patch = _parseInt(match[3])
const prerelease = match[4]
const canaryVersion = prerelease?.startsWith('canary.')
? parseInt(prerelease.split('.')[1] || '0', 10)
: undefined
return {
buildmetadata: match[5],
canaryVersion,
major: isNaN(major) ? undefined : major,
minor: isNaN(minor) ? undefined : minor,
patch: isNaN(patch) ? undefined : patch,
prerelease: match[4],
}
}
/**
* Returns the version of Next.js installed in the project, or undefined if it cannot be determined.
* @returns {SemVer | undefined}
*/
export function getNextjsVersion() {
try {
/** @type {string} */
let pkgPath
// Check if we're in ESM or CJS environment
if (typeof import.meta?.resolve === 'function') {
// ESM environment - use import.meta.resolve
const pkgUrl = import.meta.resolve('next/package.json')
// Use fileURLToPath for proper cross-platform path handling (Windows, macOS, Linux)
// new URL().pathname returns '/C:/path' on Windows which causes path resolution issues
pkgPath = fileURLToPath(pkgUrl)
} else {
// CJS environment - use require.resolve
pkgPath = require.resolve('next/package.json')
}
const pkgJson = JSON.parse(readFileSync(pkgPath, 'utf8'))
return parseSemver(pkgJson.version)
} catch (e) {
console.error('Payload: Error getting Next.js version', e)
return undefined
}
}
/**
* Checks if the current Next.js version supports the `experimental.serverFastRefresh` option.
* This was introduced in Next.js v16.2.2
* @param {SemVer | undefined} version
* @returns {boolean}
*/
export function supportsServerFastRefreshConfig(version) {
if (!version) {
return false
}
const { major, minor, patch } = version
if (major === undefined || minor === undefined || patch === undefined) {
return false
}
if (major > 16) {
return true
}
if (major === 16) {
if (minor > 2) {
return true
}
if (minor === 2) {
return patch >= 2
}
}
return false
}
/**
* Checks if the current Next.js version supports Turbopack externalize transitive dependencies.
* This was introduced in Next.js v16.1.0-canary.3
* @param {SemVer | undefined} version
* @returns {boolean}
*/
export function supportsTurbopackExternalizeTransitiveDependencies(version) {
if (!version) {
return false
}
const { canaryVersion, major, minor, patch } = version
if (major === undefined || minor === undefined) {
return false
}
if (major > 16) {
return true
}
if (major === 16) {
if (minor > 1) {
return true
}
if (minor === 1) {
// 16.1.1+ and canaries support this feature
if (patch > 0) {
return true
}
if (canaryVersion !== undefined) {
// 16.1.0-canary.3+
return canaryVersion >= 3
} else {
// Next.js 16.1.0
return true
}
}
}
return false
}