main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import { ensureLeadingSlash } from "./ensure-leading-slash";
import { normalizePathSep } from "./normalize-path-sep";
import path from "../isomorphic/path";
import { removePagePathTail } from "./remove-page-path-tail";
import { normalizeMetadataRoute } from "../../../lib/metadata/get-metadata-route";
/**
* Given the absolute path to the pages folder, an absolute file path for a
* page and the page extensions, this function will return the page path
* relative to the pages folder. It doesn't consider index tail. Example:
* - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz`
*
* It also handles special metadata routes mapping. Example:
* - `/Users/rick/my-project/app/sitemap.js` -> `/sitemap/route`
*
* @param filepath Absolute path to the page.
* @param opts.dir Absolute path to the pages/app folder.
* @param opts.extensions Extensions allowed for the page.
* @param opts.keepIndex When true the trailing `index` kept in the path.
* @param opts.pagesType Whether the page is in the pages or app directory.
*/ export function absolutePathToPage(pagePath, options) {
const isAppDir = options.pagesType === "app";
const page = removePagePathTail(normalizePathSep(ensureLeadingSlash(path.relative(options.dir, pagePath))), {
extensions: options.extensions,
keepIndex: options.keepIndex
});
return isAppDir ? normalizeMetadataRoute(page) : page;
}
//# sourceMappingURL=absolute-path-to-page.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/absolute-path-to-page.ts"],"names":["ensureLeadingSlash","normalizePathSep","path","removePagePathTail","normalizeMetadataRoute","absolutePathToPage","pagePath","options","isAppDir","pagesType","page","relative","dir","extensions","keepIndex"],"mappings":"AAAA,SAASA,kBAAkB,QAAQ,yBAAwB;AAC3D,SAASC,gBAAgB,QAAQ,uBAAsB;AACvD,OAAOC,UAAU,qBAAoB;AACrC,SAASC,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,sBAAsB,QAAQ,2CAA0C;AAEjF;;;;;;;;;;;;;;CAcC,GACD,OAAO,SAASC,mBACdC,QAAgB,EAChBC,OAKC;IAED,MAAMC,WAAWD,QAAQE,SAAS,KAAK;IACvC,MAAMC,OAAOP,mBACXF,iBAAiBD,mBAAmBE,KAAKS,QAAQ,CAACJ,QAAQK,GAAG,EAAEN,aAC/D;QACEO,YAAYN,QAAQM,UAAU;QAC9BC,WAAWP,QAAQO,SAAS;IAC9B;IAEF,OAAON,WAAWJ,uBAAuBM,QAAQA;AACnD"}

View File

@@ -0,0 +1,9 @@
export function denormalizeAppPagePath(page) {
// `/` is normalized to `/index`
if (page === "/index") {
return "/";
}
return page;
}
//# sourceMappingURL=denormalize-app-path.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/denormalize-app-path.ts"],"names":["denormalizeAppPagePath","page"],"mappings":"AAAA,OAAO,SAASA,uBAAuBC,IAAY;IACjD,gCAAgC;IAChC,IAAIA,SAAS,UAAU;QACrB,OAAO;IACT;IAEA,OAAOA;AACT"}

View File

@@ -0,0 +1,15 @@
import { isDynamicRoute } from "../router/utils";
import { normalizePathSep } from "./normalize-path-sep";
/**
* Performs the opposite transformation of `normalizePagePath`. Note that
* this function is not idempotent either in cases where there are multiple
* leading `/index` for the page. Examples:
* - `/index` -> `/`
* - `/index/foo` -> `/foo`
* - `/index/index` -> `/index`
*/ export function denormalizePagePath(page) {
let _page = normalizePathSep(page);
return _page.startsWith("/index/") && !isDynamicRoute(_page) ? _page.slice(6) : _page !== "/index" ? _page : "/";
}
//# sourceMappingURL=denormalize-page-path.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/denormalize-page-path.ts"],"names":["isDynamicRoute","normalizePathSep","denormalizePagePath","page","_page","startsWith","slice"],"mappings":"AAAA,SAASA,cAAc,QAAQ,kBAAiB;AAChD,SAASC,gBAAgB,QAAQ,uBAAsB;AAEvD;;;;;;;CAOC,GACD,OAAO,SAASC,oBAAoBC,IAAY;IAC9C,IAAIC,QAAQH,iBAAiBE;IAC7B,OAAOC,MAAMC,UAAU,CAAC,cAAc,CAACL,eAAeI,SAClDA,MAAME,KAAK,CAAC,KACZF,UAAU,WACVA,QACA;AACN"}

View File

@@ -0,0 +1,8 @@
/**
* For a given page path, this function ensures that there is a leading slash.
* If there is not a leading slash, one is added, otherwise it is noop.
*/ export function ensureLeadingSlash(path) {
return path.startsWith("/") ? path : "/" + path;
}
//# sourceMappingURL=ensure-leading-slash.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/ensure-leading-slash.ts"],"names":["ensureLeadingSlash","path","startsWith"],"mappings":"AAAA;;;CAGC,GACD,OAAO,SAASA,mBAAmBC,IAAY;IAC7C,OAAOA,KAAKC,UAAU,CAAC,OAAOD,OAAO,AAAC,MAAGA;AAC3C"}

View File

@@ -0,0 +1,40 @@
import { denormalizePagePath } from "./denormalize-page-path";
import path from "../isomorphic/path";
/**
* Calculate all possible pagePaths for a given normalized pagePath along with
* allowed extensions. This can be used to check which one of the files exists
* and to debug inspected locations.
*
* For pages, map `/route` to [`/route.[ext]`, `/route/index.[ext]`]
* For app paths, map `/route/page` to [`/route/page.[ext]`] or `/route/route`
* to [`/route/route.[ext]`]
*
* @param normalizedPagePath Normalized page path (it will denormalize).
* @param extensions Allowed extensions.
*/ export function getPagePaths(normalizedPagePath, extensions, isAppDir) {
const page = denormalizePagePath(normalizedPagePath);
let prefixes;
if (isAppDir) {
prefixes = [
page
];
} else if (normalizedPagePath.endsWith("/index")) {
prefixes = [
path.join(page, "index")
];
} else {
prefixes = [
page,
path.join(page, "index")
];
}
const paths = [];
for (const extension of extensions){
for (const prefix of prefixes){
paths.push(prefix + "." + extension);
}
}
return paths;
}
//# sourceMappingURL=get-page-paths.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/get-page-paths.ts"],"names":["denormalizePagePath","path","getPagePaths","normalizedPagePath","extensions","isAppDir","page","prefixes","endsWith","join","paths","extension","prefix","push"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,0BAAyB;AAC7D,OAAOC,UAAU,qBAAoB;AAErC;;;;;;;;;;;CAWC,GACD,OAAO,SAASC,aACdC,kBAA0B,EAC1BC,UAAoB,EACpBC,QAAiB;IAEjB,MAAMC,OAAON,oBAAoBG;IAEjC,IAAII;IACJ,IAAIF,UAAU;QACZE,WAAW;YAACD;SAAK;IACnB,OAAO,IAAIH,mBAAmBK,QAAQ,CAAC,WAAW;QAChDD,WAAW;YAACN,KAAKQ,IAAI,CAACH,MAAM;SAAS;IACvC,OAAO;QACLC,WAAW;YAACD;YAAML,KAAKQ,IAAI,CAACH,MAAM;SAAS;IAC7C;IAEA,MAAMI,QAAkB,EAAE;IAC1B,KAAK,MAAMC,aAAaP,WAAY;QAClC,KAAK,MAAMQ,UAAUL,SAAU;YAC7BG,MAAMG,IAAI,CAAC,AAAGD,SAAO,MAAGD;QAC1B;IACF;IAEA,OAAOD;AACT"}

View File

@@ -0,0 +1,24 @@
import { ensureLeadingSlash } from "./ensure-leading-slash";
import { isDynamicRoute } from "../router/utils";
import { NormalizeError } from "../utils";
/**
* Takes a page and transforms it into its file counterpart ensuring that the
* output is normalized. Note this function is not idempotent because a page
* `/index` can be referencing `/index/index.js` and `/index/index` could be
* referencing `/index/index/index.js`. Examples:
* - `/` -> `/index`
* - `/index/foo` -> `/index/index/foo`
* - `/index` -> `/index/index`
*/ export function normalizePagePath(page) {
const normalized = /^\/index(\/|$)/.test(page) && !isDynamicRoute(page) ? "/index" + page : page === "/" ? "/index" : ensureLeadingSlash(page);
if (process.env.NEXT_RUNTIME !== "edge") {
const { posix } = require("path");
const resolvedPage = posix.normalize(normalized);
if (resolvedPage !== normalized) {
throw new NormalizeError("Requested and resolved page mismatch: " + normalized + " " + resolvedPage);
}
}
return normalized;
}
//# sourceMappingURL=normalize-page-path.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/normalize-page-path.ts"],"names":["ensureLeadingSlash","isDynamicRoute","NormalizeError","normalizePagePath","page","normalized","test","process","env","NEXT_RUNTIME","posix","require","resolvedPage","normalize"],"mappings":"AAAA,SAASA,kBAAkB,QAAQ,yBAAwB;AAC3D,SAASC,cAAc,QAAQ,kBAAiB;AAChD,SAASC,cAAc,QAAQ,WAAU;AAEzC;;;;;;;;CAQC,GACD,OAAO,SAASC,kBAAkBC,IAAY;IAC5C,MAAMC,aACJ,iBAAiBC,IAAI,CAACF,SAAS,CAACH,eAAeG,QAC3C,AAAC,WAAQA,OACTA,SAAS,MACT,WACAJ,mBAAmBI;IAEzB,IAAIG,QAAQC,GAAG,CAACC,YAAY,KAAK,QAAQ;QACvC,MAAM,EAAEC,KAAK,EAAE,GAAGC,QAAQ;QAC1B,MAAMC,eAAeF,MAAMG,SAAS,CAACR;QACrC,IAAIO,iBAAiBP,YAAY;YAC/B,MAAM,IAAIH,eACR,AAAC,2CAAwCG,aAAW,MAAGO;QAE3D;IACF;IAEA,OAAOP;AACT"}

View File

@@ -0,0 +1,9 @@
/**
* For a given page path, this function ensures that there is no backslash
* escaping slashes in the path. Example:
* - `foo\/bar\/baz` -> `foo/bar/baz`
*/ export function normalizePathSep(path) {
return path.replace(/\\/g, "/");
}
//# sourceMappingURL=normalize-path-sep.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/normalize-path-sep.ts"],"names":["normalizePathSep","path","replace"],"mappings":"AAAA;;;;CAIC,GACD,OAAO,SAASA,iBAAiBC,IAAY;IAC3C,OAAOA,KAAKC,OAAO,CAAC,OAAO;AAC7B"}

View File

@@ -0,0 +1,20 @@
import { normalizePathSep } from "./normalize-path-sep";
/**
* Removes the file extension for a page and the trailing `index` if it exists
* making sure to not return an empty string. The page head is not touched
* and returned as it is passed. Examples:
* - `/foo/bar/baz/index.js` -> `/foo/bar/baz`
* - `/foo/bar/baz.js` -> `/foo/bar/baz`
*
* @param pagePath A page to a page file (absolute or relative)
* @param options.extensions Extensions allowed for the page.
* @param options.keepIndex When true the trailing `index` is _not_ removed.
*/ export function removePagePathTail(pagePath, options) {
pagePath = normalizePathSep(pagePath).replace(new RegExp("\\.+(?:" + options.extensions.join("|") + ")$"), "");
if (options.keepIndex !== true) {
pagePath = pagePath.replace(/\/index$/, "") || "/";
}
return pagePath;
}
//# sourceMappingURL=remove-page-path-tail.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../../src/shared/lib/page-path/remove-page-path-tail.ts"],"names":["normalizePathSep","removePagePathTail","pagePath","options","replace","RegExp","extensions","join","keepIndex"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,uBAAsB;AAEvD;;;;;;;;;;CAUC,GACD,OAAO,SAASC,mBACdC,QAAgB,EAChBC,OAGC;IAEDD,WAAWF,iBAAiBE,UAAUE,OAAO,CAC3C,IAAIC,OAAO,AAAC,YAASF,QAAQG,UAAU,CAACC,IAAI,CAAC,OAAK,OAClD;IAGF,IAAIJ,QAAQK,SAAS,KAAK,MAAM;QAC9BN,WAAWA,SAASE,OAAO,CAAC,YAAY,OAAO;IACjD;IAEA,OAAOF;AACT"}