({
workPath,
page,
pageExtensions,
nextVersion,
}: {
workPath: string;
page: string;
pageExtensions?: ReadonlyArray<string>;
nextVersion?: string;
})
| 1752 | } |
| 1753 | |
| 1754 | async function getSourceFilePathFromPage({ |
| 1755 | workPath, |
| 1756 | page, |
| 1757 | pageExtensions, |
| 1758 | nextVersion, |
| 1759 | }: { |
| 1760 | workPath: string; |
| 1761 | page: string; |
| 1762 | pageExtensions?: ReadonlyArray<string>; |
| 1763 | nextVersion?: string; |
| 1764 | }) { |
| 1765 | const usesSrcDir = await usesSrcDirectory(workPath); |
| 1766 | const extensionsToTry = pageExtensions || ['js', 'jsx', 'ts', 'tsx']; |
| 1767 | |
| 1768 | // In Next.js 16+, check for proxy.ts first as it's the recommended replacement for middleware.ts |
| 1769 | const isNextJs16Plus = nextVersion && semver.gte(nextVersion, '16.0.0'); |
| 1770 | const pagesToCheck = |
| 1771 | page === 'middleware' && isNextJs16Plus |
| 1772 | ? ['proxy', 'middleware'] // Check proxy.ts first in Next.js 16+ |
| 1773 | : [page]; |
| 1774 | |
| 1775 | for (const pageToCheck of pagesToCheck) { |
| 1776 | for (const pageType of [ |
| 1777 | // middleware/proxy is not nested in pages/app |
| 1778 | ...(pageToCheck === 'middleware' || pageToCheck === 'proxy' |
| 1779 | ? [''] |
| 1780 | : ['pages', 'app']), |
| 1781 | ]) { |
| 1782 | let fsPath = path.join(workPath, pageType, pageToCheck); |
| 1783 | if (usesSrcDir) { |
| 1784 | fsPath = path.join(workPath, 'src', pageType, pageToCheck); |
| 1785 | } |
| 1786 | |
| 1787 | if (fs.existsSync(fsPath)) { |
| 1788 | return path.relative(workPath, fsPath); |
| 1789 | } |
| 1790 | const extensionless = fsPath.replace(path.extname(fsPath), ''); |
| 1791 | |
| 1792 | for (const ext of extensionsToTry) { |
| 1793 | fsPath = `${extensionless}.${ext}`; |
| 1794 | // for appDir, we need to treat "index.js" as root-level "page.js" |
| 1795 | if ( |
| 1796 | pageType === 'app' && |
| 1797 | extensionless === |
| 1798 | path.join(workPath, `${usesSrcDir ? 'src/' : ''}app/index`) |
| 1799 | ) { |
| 1800 | fsPath = `${extensionless.replace(/index$/, 'page')}.${ext}`; |
| 1801 | } |
| 1802 | if (fs.existsSync(fsPath)) { |
| 1803 | return path.relative(workPath, fsPath); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | if (isDirectory(extensionless)) { |
| 1808 | if (pageType === 'pages') { |
| 1809 | for (const ext of extensionsToTry) { |
| 1810 | fsPath = path.join(extensionless, `index.${ext}`); |
| 1811 | if (fs.existsSync(fsPath)) { |
no test coverage detected