UNPKG

abulo-vite-plugin-pages

Version:
1 lines 77.2 kB
{"version":3,"sources":["../src/constants.ts","../src/context.ts","../node_modules/.pnpm/@antfu+utils@0.7.2/node_modules/@antfu/utils/dist/index.mjs","../src/options.ts","../src/files.ts","../src/utils.ts","../src/resolvers/vue.ts","../src/stringify.ts","../src/customBlock.ts","../src/resolvers/react.ts","../src/resolvers/solid.ts","../src/index.ts"],"sourcesContent":["export const MODULE_IDS = [\n '~pages',\n '~react-pages',\n '~solid-pages',\n 'pages-generated',\n 'virtual:generated-pages',\n 'virtual:generated-pages-react',\n]\n\nexport const MODULE_ID_VIRTUAL = '/@abulo-vite-plugin-pages/generated-pages'\nexport const ROUTE_BLOCK_ID_VIRTUAL = '/@abulo-vite-plugin-pages/route-block'\nexport const ROUTE_IMPORT_NAME = '__pages_import_$1__'\n\nexport const routeBlockQueryRE = /\\?vue&type=route/\n\nexport const dynamicRouteRE = /^\\[(.+)\\]$/\nexport const cacheAllRouteRE = /^\\[\\.{3}(.*)\\]$/\nexport const replaceDynamicRouteRE = /^\\[(?:\\.{3})?(.*)\\]$/\n\nexport const nuxtDynamicRouteRE = /^_(.*)$/\nexport const nuxtCacheAllRouteRE = /^_$/\n\nexport const countSlashRE = /\\//g\n\nexport const replaceIndexRE = /\\/?index$/\n","import { extname, join, resolve } from 'path'\nimport { slash, toArray } from '@antfu/utils'\nimport { resolveOptions } from './options'\nimport { getPageFiles } from './files'\nimport { debug, invalidatePagesModule, isTarget } from './utils'\n\nimport type { FSWatcher } from 'fs'\nimport type { Logger, ViteDevServer } from 'vite'\nimport type { PageOptions, ResolvedOptions, UserOptions } from './types'\n\nexport interface PageRoute {\n path: string\n route: string\n}\n\nexport class PageContext {\n private _server: ViteDevServer | undefined\n private _pageRouteMap = new Map<string, PageRoute>()\n\n rawOptions: UserOptions\n root: string\n options: ResolvedOptions\n logger?: Logger\n\n constructor(userOptions: UserOptions, viteRoot: string = process.cwd()) {\n this.rawOptions = userOptions\n this.root = slash(viteRoot)\n debug.env('root', this.root)\n this.options = resolveOptions(userOptions, this.root)\n debug.options(this.options)\n }\n\n setLogger(logger: Logger) {\n this.logger = logger\n }\n\n setupViteServer(server: ViteDevServer) {\n if (this._server === server)\n return\n\n this._server = server\n this.setupWatcher(server.watcher)\n }\n\n setupWatcher(watcher: FSWatcher) {\n watcher\n .on('unlink', async(path) => {\n path = slash(path)\n if (!isTarget(path, this.options))\n return\n await this.removePage(path)\n this.onUpdate()\n })\n watcher\n .on('add', async(path) => {\n path = slash(path)\n if (!isTarget(path, this.options))\n return\n const page = this.options.dirs.find(i => path.startsWith(slash(resolve(this.root, i.dir))))!\n await this.addPage(path, page)\n this.onUpdate()\n })\n\n watcher\n .on('change', async(path) => {\n path = slash(path)\n if (!isTarget(path, this.options))\n return\n const page = this._pageRouteMap.get(path)\n if (page)\n await this.options.resolver.hmr?.changed?.(this, path)\n })\n }\n\n async addPage(path: string | string[], pageDir: PageOptions) {\n debug.pages('add', path)\n for (const p of toArray(path)) {\n const pageDirPath = slash(resolve(this.root, pageDir.dir))\n const route = slash(join(pageDir.baseRoute, p.replace(`${pageDirPath}/`, '').replace(extname(p), '')))\n this._pageRouteMap.set(p, {\n path: p,\n route,\n })\n await this.options.resolver.hmr?.added?.(this, p)\n }\n }\n\n async removePage(path: string) {\n debug.pages('remove', path)\n this._pageRouteMap.delete(path)\n await this.options.resolver.hmr?.removed?.(this, path)\n }\n\n onUpdate() {\n if (!this._server)\n return\n\n invalidatePagesModule(this._server)\n debug.hmr('Reload generated pages.')\n this._server.ws.send({\n type: 'full-reload',\n })\n }\n\n async resolveRoutes() {\n return this.options.resolver.resolveRoutes(this)\n }\n\n async searchGlob() {\n const pageDirFiles = this.options.dirs.map((page) => {\n const pagesDirPath = slash(resolve(this.options.root, page.dir))\n const files = getPageFiles(pagesDirPath, this.options, page)\n debug.search(page.dir, files)\n return {\n ...page,\n files: files.map(file => slash(file)),\n }\n })\n\n for (const page of pageDirFiles)\n await this.addPage(page.files, page)\n\n debug.cache(this.pageRouteMap)\n }\n\n get debug() {\n return debug\n }\n\n get pageRouteMap() {\n return this._pageRouteMap\n }\n}\n","function clamp(n, min, max) {\n return Math.min(max, Math.max(min, n));\n}\nfunction sum(...args) {\n return flattenArrayable(args).reduce((a, b) => a + b, 0);\n}\n\nfunction toArray(array) {\n array = array ?? [];\n return Array.isArray(array) ? array : [array];\n}\nfunction flattenArrayable(array) {\n return toArray(array).flat(1);\n}\nfunction mergeArrayable(...args) {\n return args.flatMap((i) => toArray(i));\n}\nfunction partition(array, ...filters) {\n const result = new Array(filters.length + 1).fill(null).map(() => []);\n array.forEach((e, idx, arr) => {\n let i = 0;\n for (const filter of filters) {\n if (filter(e, idx, arr)) {\n result[i].push(e);\n return;\n }\n i += 1;\n }\n result[i].push(e);\n });\n return result;\n}\nfunction uniq(array) {\n return Array.from(new Set(array));\n}\nfunction uniqueBy(array, equalFn) {\n return array.reduce((acc, cur) => {\n const index = acc.findIndex((item) => equalFn(cur, item));\n if (index === -1)\n acc.push(cur);\n return acc;\n }, []);\n}\nfunction last(array) {\n return at(array, -1);\n}\nfunction remove(array, value) {\n if (!array)\n return false;\n const index = array.indexOf(value);\n if (index >= 0) {\n array.splice(index, 1);\n return true;\n }\n return false;\n}\nfunction at(array, index) {\n const len = array.length;\n if (!len)\n return void 0;\n if (index < 0)\n index += len;\n return array[index];\n}\nfunction range(...args) {\n let start, stop, step;\n if (args.length === 1) {\n start = 0;\n step = 1;\n [stop] = args;\n } else {\n [start, stop, step = 1] = args;\n }\n const arr = [];\n let current = start;\n while (current < stop) {\n arr.push(current);\n current += step || 1;\n }\n return arr;\n}\nfunction move(arr, from, to) {\n arr.splice(to, 0, arr.splice(from, 1)[0]);\n return arr;\n}\nfunction clampArrayRange(n, arr) {\n return clamp(n, 0, arr.length - 1);\n}\nfunction sample(arr, count) {\n return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);\n}\nfunction shuffle(array) {\n for (let i = array.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [array[i], array[j]] = [array[j], array[i]];\n }\n return array;\n}\n\nconst assert = (condition, message) => {\n if (!condition)\n throw new Error(message);\n};\nconst toString = (v) => Object.prototype.toString.call(v);\nconst getTypeName = (v) => {\n if (v === null)\n return \"null\";\n const type = toString(v).slice(8, -1).toLowerCase();\n return typeof v === \"object\" || typeof v === \"function\" ? type : typeof v;\n};\nconst noop = () => {\n};\n\nfunction notNullish(v) {\n return v != null;\n}\nfunction noNull(v) {\n return v !== null;\n}\nfunction notUndefined(v) {\n return v !== void 0;\n}\nfunction isTruthy(v) {\n return Boolean(v);\n}\n\nconst isDef = (val) => typeof val !== \"undefined\";\nconst isBoolean = (val) => typeof val === \"boolean\";\nconst isFunction = (val) => typeof val === \"function\";\nconst isNumber = (val) => typeof val === \"number\";\nconst isString = (val) => typeof val === \"string\";\nconst isObject = (val) => toString(val) === \"[object Object]\";\nconst isUndefined = (val) => toString(val) === \"[object Undefined]\";\nconst isNull = (val) => toString(val) === \"[object Null]\";\nconst isRegExp = (val) => toString(val) === \"[object RegExp]\";\nconst isDate = (val) => toString(val) === \"[object Date]\";\nconst isWindow = (val) => typeof window !== \"undefined\" && toString(val) === \"[object Window]\";\nconst isBrowser = typeof window !== \"undefined\";\n\nfunction slash(str) {\n return str.replace(/\\\\/g, \"/\");\n}\nfunction ensurePrefix(prefix, str) {\n if (!str.startsWith(prefix))\n return prefix + str;\n return str;\n}\nfunction ensureSuffix(suffix, str) {\n if (!str.endsWith(suffix))\n return str + suffix;\n return str;\n}\nfunction template(str, ...args) {\n return str.replace(/{(\\d+)}/g, (match, key) => {\n const index = Number(key);\n if (Number.isNaN(index))\n return match;\n return args[index];\n });\n}\nconst urlAlphabet = \"useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict\";\nfunction randomStr(size = 16, dict = urlAlphabet) {\n let id = \"\";\n let i = size;\n const len = dict.length;\n while (i--)\n id += dict[Math.random() * len | 0];\n return id;\n}\nfunction capitalize(str) {\n return str[0].toUpperCase() + str.slice(1).toLowerCase();\n}\n\nconst timestamp = () => +Date.now();\n\nfunction batchInvoke(functions) {\n functions.forEach((fn) => fn && fn());\n}\nfunction invoke(fn) {\n return fn();\n}\nfunction tap(value, callback) {\n callback(value);\n return value;\n}\n\nfunction objectMap(obj, fn) {\n return Object.fromEntries(\n Object.entries(obj).map(([k, v]) => fn(k, v)).filter(notNullish)\n );\n}\nfunction isKeyOf(obj, k) {\n return k in obj;\n}\nfunction objectKeys(obj) {\n return Object.keys(obj);\n}\nfunction objectEntries(obj) {\n return Object.entries(obj);\n}\nfunction deepMerge(target, ...sources) {\n if (!sources.length)\n return target;\n const source = sources.shift();\n if (source === void 0)\n return target;\n if (isMergableObject(target) && isMergableObject(source)) {\n objectKeys(source).forEach((key) => {\n if (isMergableObject(source[key])) {\n if (!target[key])\n target[key] = {};\n deepMerge(target[key], source[key]);\n } else {\n target[key] = source[key];\n }\n });\n }\n return deepMerge(target, ...sources);\n}\nfunction isMergableObject(item) {\n return isObject(item) && !Array.isArray(item);\n}\nfunction objectPick(obj, keys, omitUndefined = false) {\n return keys.reduce((n, k) => {\n if (k in obj) {\n if (!omitUndefined || obj[k] !== void 0)\n n[k] = obj[k];\n }\n return n;\n }, {});\n}\nfunction clearUndefined(obj) {\n Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {});\n return obj;\n}\nfunction hasOwnProperty(obj, v) {\n if (obj == null)\n return false;\n return Object.prototype.hasOwnProperty.call(obj, v);\n}\n\nfunction createSingletonPromise(fn) {\n let _promise;\n function wrapper() {\n if (!_promise)\n _promise = fn();\n return _promise;\n }\n wrapper.reset = async () => {\n const _prev = _promise;\n _promise = void 0;\n if (_prev)\n await _prev;\n };\n return wrapper;\n}\nfunction sleep(ms, callback) {\n return new Promise(\n (resolve) => setTimeout(async () => {\n await (callback == null ? void 0 : callback());\n resolve();\n }, ms)\n );\n}\nfunction createPromiseLock() {\n const locks = [];\n return {\n async run(fn) {\n const p = fn();\n locks.push(p);\n try {\n return await p;\n } finally {\n remove(locks, p);\n }\n },\n async wait() {\n await Promise.allSettled(locks);\n },\n isWaiting() {\n return Boolean(locks.length);\n },\n clear() {\n locks.length = 0;\n }\n };\n}\nfunction createControlledPromise() {\n let resolve, reject;\n const promise = new Promise((_resolve, _reject) => {\n resolve = _resolve;\n reject = _reject;\n });\n promise.resolve = resolve;\n promise.reject = reject;\n return promise;\n}\n\n/* eslint-disable no-undefined,no-param-reassign,no-shadow */\n\n/**\n * Throttle execution of a function. Especially useful for rate limiting\n * execution of handlers on events like resize and scroll.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)\n * are most useful.\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,\n * as-is, to `callback` when the throttled-function is executed.\n * @param {object} [options] - An object to configure options.\n * @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds\n * while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed\n * one final time after the last throttled-function call. (After the throttled-function has not been called for\n * `delay` milliseconds, the internal counter is reset).\n * @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback\n * immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that\n * callback will never executed if both noLeading = true and noTrailing = true.\n * @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is\n * false (at end), schedule `callback` to execute after `delay` ms.\n *\n * @returns {Function} A new, throttled, function.\n */\nfunction throttle (delay, callback, options) {\n var _ref = options || {},\n _ref$noTrailing = _ref.noTrailing,\n noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,\n _ref$noLeading = _ref.noLeading,\n noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,\n _ref$debounceMode = _ref.debounceMode,\n debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;\n /*\n * After wrapper has stopped being called, this timeout ensures that\n * `callback` is executed at the proper times in `throttle` and `end`\n * debounce modes.\n */\n\n\n var timeoutID;\n var cancelled = false; // Keep track of the last time `callback` was executed.\n\n var lastExec = 0; // Function to clear existing timeout\n\n function clearExistingTimeout() {\n if (timeoutID) {\n clearTimeout(timeoutID);\n }\n } // Function to cancel next exec\n\n\n function cancel(options) {\n var _ref2 = options || {},\n _ref2$upcomingOnly = _ref2.upcomingOnly,\n upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;\n\n clearExistingTimeout();\n cancelled = !upcomingOnly;\n }\n /*\n * The `wrapper` function encapsulates all of the throttling / debouncing\n * functionality and when executed will limit the rate at which `callback`\n * is executed.\n */\n\n\n function wrapper() {\n for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {\n arguments_[_key] = arguments[_key];\n }\n\n var self = this;\n var elapsed = Date.now() - lastExec;\n\n if (cancelled) {\n return;\n } // Execute `callback` and update the `lastExec` timestamp.\n\n\n function exec() {\n lastExec = Date.now();\n callback.apply(self, arguments_);\n }\n /*\n * If `debounceMode` is true (at begin) this is used to clear the flag\n * to allow future `callback` executions.\n */\n\n\n function clear() {\n timeoutID = undefined;\n }\n\n if (!noLeading && debounceMode && !timeoutID) {\n /*\n * Since `wrapper` is being called for the first time and\n * `debounceMode` is true (at begin), execute `callback`\n * and noLeading != true.\n */\n exec();\n }\n\n clearExistingTimeout();\n\n if (debounceMode === undefined && elapsed > delay) {\n if (noLeading) {\n /*\n * In throttle mode with noLeading, if `delay` time has\n * been exceeded, update `lastExec` and schedule `callback`\n * to execute after `delay` ms.\n */\n lastExec = Date.now();\n\n if (!noTrailing) {\n timeoutID = setTimeout(debounceMode ? clear : exec, delay);\n }\n } else {\n /*\n * In throttle mode without noLeading, if `delay` time has been exceeded, execute\n * `callback`.\n */\n exec();\n }\n } else if (noTrailing !== true) {\n /*\n * In trailing throttle mode, since `delay` time has not been\n * exceeded, schedule `callback` to execute `delay` ms after most\n * recent execution.\n *\n * If `debounceMode` is true (at begin), schedule `clear` to execute\n * after `delay` ms.\n *\n * If `debounceMode` is false (at end), schedule `callback` to\n * execute after `delay` ms.\n */\n timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);\n }\n }\n\n wrapper.cancel = cancel; // Return the wrapper function.\n\n return wrapper;\n}\n\n/* eslint-disable no-undefined */\n/**\n * Debounce execution of a function. Debouncing, unlike throttling,\n * guarantees that a function is only executed a single time, either at the\n * very beginning of a series of calls, or at the very end.\n *\n * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.\n * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,\n * to `callback` when the debounced-function is executed.\n * @param {object} [options] - An object to configure options.\n * @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds\n * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.\n * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).\n *\n * @returns {Function} A new, debounced function.\n */\n\nfunction debounce (delay, callback, options) {\n var _ref = options || {},\n _ref$atBegin = _ref.atBegin,\n atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;\n\n return throttle(delay, callback, {\n debounceMode: atBegin !== false\n });\n}\n\n/*\nHow it works:\n`this.#head` is an instance of `Node` which keeps track of its current value and nests another instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, iterating through every single item is slow. This problem is solved by saving a reference to the last value as `this.#tail` so that it can reference it to add a new value.\n*/\n\nclass Node {\n\tvalue;\n\tnext;\n\n\tconstructor(value) {\n\t\tthis.value = value;\n\t}\n}\n\nclass Queue {\n\t#head;\n\t#tail;\n\t#size;\n\n\tconstructor() {\n\t\tthis.clear();\n\t}\n\n\tenqueue(value) {\n\t\tconst node = new Node(value);\n\n\t\tif (this.#head) {\n\t\t\tthis.#tail.next = node;\n\t\t\tthis.#tail = node;\n\t\t} else {\n\t\t\tthis.#head = node;\n\t\t\tthis.#tail = node;\n\t\t}\n\n\t\tthis.#size++;\n\t}\n\n\tdequeue() {\n\t\tconst current = this.#head;\n\t\tif (!current) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#head = this.#head.next;\n\t\tthis.#size--;\n\t\treturn current.value;\n\t}\n\n\tclear() {\n\t\tthis.#head = undefined;\n\t\tthis.#tail = undefined;\n\t\tthis.#size = 0;\n\t}\n\n\tget size() {\n\t\treturn this.#size;\n\t}\n\n\t* [Symbol.iterator]() {\n\t\tlet current = this.#head;\n\n\t\twhile (current) {\n\t\t\tyield current.value;\n\t\t\tcurrent = current.next;\n\t\t}\n\t}\n}\n\nfunction pLimit(concurrency) {\n\tif (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {\n\t\tthrow new TypeError('Expected `concurrency` to be a number from 1 and up');\n\t}\n\n\tconst queue = new Queue();\n\tlet activeCount = 0;\n\n\tconst next = () => {\n\t\tactiveCount--;\n\n\t\tif (queue.size > 0) {\n\t\t\tqueue.dequeue()();\n\t\t}\n\t};\n\n\tconst run = async (fn, resolve, args) => {\n\t\tactiveCount++;\n\n\t\tconst result = (async () => fn(...args))();\n\n\t\tresolve(result);\n\n\t\ttry {\n\t\t\tawait result;\n\t\t} catch {}\n\n\t\tnext();\n\t};\n\n\tconst enqueue = (fn, resolve, args) => {\n\t\tqueue.enqueue(run.bind(undefined, fn, resolve, args));\n\n\t\t(async () => {\n\t\t\t// This function needs to wait until the next microtask before comparing\n\t\t\t// `activeCount` to `concurrency`, because `activeCount` is updated asynchronously\n\t\t\t// when the run function is dequeued and called. The comparison in the if-statement\n\t\t\t// needs to happen asynchronously as well to get an up-to-date value for `activeCount`.\n\t\t\tawait Promise.resolve();\n\n\t\t\tif (activeCount < concurrency && queue.size > 0) {\n\t\t\t\tqueue.dequeue()();\n\t\t\t}\n\t\t})();\n\t};\n\n\tconst generator = (fn, ...args) => new Promise(resolve => {\n\t\tenqueue(fn, resolve, args);\n\t});\n\n\tObject.defineProperties(generator, {\n\t\tactiveCount: {\n\t\t\tget: () => activeCount,\n\t\t},\n\t\tpendingCount: {\n\t\t\tget: () => queue.size,\n\t\t},\n\t\tclearQueue: {\n\t\t\tvalue: () => {\n\t\t\t\tqueue.clear();\n\t\t\t},\n\t\t},\n\t});\n\n\treturn generator;\n}\n\nconst VOID = Symbol(\"p-void\");\nclass PInstance extends Promise {\n constructor(items = [], options) {\n super(() => {\n });\n this.items = items;\n this.options = options;\n this.promises = /* @__PURE__ */ new Set();\n }\n get promise() {\n var _a;\n let batch;\n const items = [...Array.from(this.items), ...Array.from(this.promises)];\n if ((_a = this.options) == null ? void 0 : _a.concurrency) {\n const limit = pLimit(this.options.concurrency);\n batch = Promise.all(items.map((p2) => limit(() => p2)));\n } else {\n batch = Promise.all(items);\n }\n return batch.then((l) => l.filter((i) => i !== VOID));\n }\n add(...args) {\n args.forEach((i) => {\n this.promises.add(i);\n });\n }\n map(fn) {\n return new PInstance(\n Array.from(this.items).map(async (i, idx) => {\n const v = await i;\n if (v === VOID)\n return VOID;\n return fn(v, idx);\n }),\n this.options\n );\n }\n filter(fn) {\n return new PInstance(\n Array.from(this.items).map(async (i, idx) => {\n const v = await i;\n const r = await fn(v, idx);\n if (!r)\n return VOID;\n return v;\n }),\n this.options\n );\n }\n forEach(fn) {\n return this.map(fn).then();\n }\n reduce(fn, initialValue) {\n return this.promise.then((array) => array.reduce(fn, initialValue));\n }\n clear() {\n this.promises.clear();\n }\n then(fn) {\n const p2 = this.promise;\n if (fn)\n return p2.then(fn);\n else\n return p2;\n }\n catch(fn) {\n return this.promise.catch(fn);\n }\n finally(fn) {\n return this.promise.finally(fn);\n }\n}\nfunction p(items, options) {\n return new PInstance(items, options);\n}\n\nexport { assert, at, batchInvoke, capitalize, clamp, clampArrayRange, clearUndefined, createControlledPromise, createPromiseLock, createSingletonPromise, debounce, deepMerge, ensurePrefix, ensureSuffix, flattenArrayable, getTypeName, hasOwnProperty, invoke, isBoolean, isBrowser, isDate, isDef, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isString, isTruthy, isUndefined, isWindow, last, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, p, partition, randomStr, range, remove, sample, shuffle, slash, sleep, sum, tap, template, throttle, timestamp, toArray, toString, uniq, uniqueBy };\n","import { resolve } from 'path'\nimport { slash, toArray } from '@antfu/utils'\nimport { getPageDirs } from './files'\n\nimport { reactResolver, solidResolver, vueResolver } from './resolvers'\nimport { MODULE_IDS } from './constants'\nimport type { ImportModeResolver, ResolvedOptions, UserOptions } from './types'\n\nfunction resolvePageDirs(dirs: UserOptions['dirs'], root: string, exclude: string[]) {\n dirs = toArray(dirs)\n return dirs.flatMap((dir) => {\n const option = typeof dir === 'string'\n ? { dir, baseRoute: '' }\n : dir\n\n option.dir = slash(resolve(root, option.dir)).replace(`${root}/`, '')\n option.baseRoute = option.baseRoute.replace(/^\\//, '').replace(/\\/$/, '')\n\n return getPageDirs(option, root, exclude)\n })\n}\n\nexport const syncIndexResolver: ImportModeResolver = (filepath, options) => {\n for (const page of options.dirs) {\n if (page.baseRoute === '' && filepath.startsWith(`/${page.dir}/index`))\n return 'sync'\n }\n return 'async'\n}\n\nconst getResolver = (originalResolver: UserOptions['resolver']) => {\n let resolver = originalResolver || 'vue'\n\n if (typeof resolver !== 'string')\n return resolver\n\n switch (resolver) {\n case 'vue':\n resolver = vueResolver()\n break\n case 'react':\n resolver = reactResolver()\n break\n case 'solid':\n resolver = solidResolver()\n break\n default:\n throw new Error(`Unsupported resolver: ${resolver}`)\n }\n return resolver\n}\n\nexport function resolveOptions(userOptions: UserOptions, viteRoot?: string): ResolvedOptions {\n const {\n dirs = userOptions.pagesDir || ['src/pages'],\n routeBlockLang = 'json5',\n exclude = ['node_modules', '.git', '**/__*__/**'],\n caseSensitive = false,\n syncIndex = true,\n routeNameSeparator = '-',\n extendRoute,\n onRoutesGenerated,\n onClientGenerated,\n } = userOptions\n\n const root = viteRoot || slash(process.cwd())\n\n const importMode = userOptions.importMode || (syncIndex ? syncIndexResolver : 'async')\n\n const resolver = getResolver(userOptions.resolver)\n\n const extensions = userOptions.extensions || resolver.resolveExtensions()\n\n const extensionsRE = new RegExp(`\\\\.(${extensions.join('|')})$`)\n\n const resolvedDirs = resolvePageDirs(dirs, root, exclude)\n\n const routeStyle = userOptions.nuxtStyle ? 'nuxt' : userOptions.routeStyle || 'next'\n\n const moduleIds = userOptions.moduleId\n ? [userOptions.moduleId]\n : resolver.resolveModuleIds?.() || MODULE_IDS\n\n const resolvedOptions: ResolvedOptions = {\n dirs: resolvedDirs,\n routeStyle,\n routeBlockLang,\n moduleIds,\n root,\n extensions,\n importMode,\n exclude,\n caseSensitive,\n resolver,\n extensionsRE,\n extendRoute,\n onRoutesGenerated,\n onClientGenerated,\n routeNameSeparator,\n }\n\n return resolvedOptions\n}\n","import { join } from 'path'\nimport { slash } from '@antfu/utils'\nimport fg from 'fast-glob'\nimport { extsToGlob } from './utils'\n\nimport type { PageOptions, ResolvedOptions } from './types'\n\n/**\n * Resolves the page dirs for its for its given globs\n */\nexport function getPageDirs(PageOptions: PageOptions, root: string, exclude: string[]): PageOptions[] {\n const dirs = fg.sync(slash(PageOptions.dir), {\n ignore: exclude,\n onlyDirectories: true,\n dot: true,\n unique: true,\n cwd: root,\n })\n\n const pageDirs = dirs.map(dir => ({\n ...PageOptions,\n dir,\n }))\n\n return pageDirs\n}\n\n/**\n * Resolves the files that are valid pages for the given context.\n */\nexport function getPageFiles(path: string, options: ResolvedOptions, pageOptions?: PageOptions): string[] {\n const {\n exclude,\n extensions,\n } = options\n\n const ext = extsToGlob(extensions)\n const pattern = pageOptions?.filePatern ?? `**/*.${ext}`\n\n const files = fg.sync(pattern, {\n ignore: exclude,\n onlyFiles: true,\n cwd: path,\n }).map(p => slash(join(path, p)))\n\n return files\n}\n","import { resolve, win32 } from 'path'\nimport { URLSearchParams } from 'url'\nimport Debug from 'debug'\nimport { slash } from '@antfu/utils'\nimport { MODULE_ID_VIRTUAL, cacheAllRouteRE, countSlashRE, dynamicRouteRE, nuxtCacheAllRouteRE, nuxtDynamicRouteRE, replaceDynamicRouteRE, replaceIndexRE } from './constants'\n\nimport type { ModuleNode, ViteDevServer } from 'vite'\nimport type { ResolvedOptions } from './types'\n\nexport const debug = {\n hmr: Debug('abulo-vite-plugin-pages:hmr'),\n routeBlock: Debug('abulo-vite-plugin-pages:routeBlock'),\n options: Debug('abulo-vite-plugin-pages:options'),\n pages: Debug('abulo-vite-plugin-pages:pages'),\n search: Debug('abulo-vite-plugin-pages:search'),\n env: Debug('abulo-vite-plugin-pages:env'),\n cache: Debug('abulo-vite-plugin-pages:cache'),\n resolver: Debug('abulo-vite-plugin-pages:resolver'),\n}\n\nexport function extsToGlob(extensions: string[]) {\n return extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0] || ''\n}\n\nexport function countSlash(value: string) {\n return (value.match(countSlashRE) || []).length\n}\n\nfunction isPagesDir(path: string, options: ResolvedOptions) {\n for (const page of options.dirs) {\n const dirPath = slash(resolve(options.root, page.dir))\n if (path.startsWith(dirPath)) return true\n }\n return false\n}\n\nexport function isTarget(path: string, options: ResolvedOptions) {\n return isPagesDir(path, options) && options.extensionsRE.test(path)\n}\n\nexport function isDynamicRoute(routePath: string, nuxtStyle = false) {\n return nuxtStyle\n ? nuxtDynamicRouteRE.test(routePath)\n : dynamicRouteRE.test(routePath)\n}\n\nexport function isCatchAllRoute(routePath: string, nuxtStyle = false) {\n return nuxtStyle\n ? nuxtCacheAllRouteRE.test(routePath)\n : cacheAllRouteRE.test(routePath)\n}\n\nexport function resolveImportMode(\n filepath: string,\n options: ResolvedOptions,\n) {\n const mode = options.importMode\n if (typeof mode === 'function')\n return mode(filepath, options)\n return mode\n}\n\nexport function invalidatePagesModule(server: ViteDevServer) {\n const { moduleGraph } = server\n const mods = moduleGraph.getModulesByFile(MODULE_ID_VIRTUAL)\n if (mods) {\n const seen = new Set<ModuleNode>()\n mods.forEach((mod) => {\n moduleGraph.invalidateModule(mod, seen)\n })\n }\n}\n\nexport function normalizeCase(str: string, caseSensitive: boolean) {\n if (!caseSensitive) return str.toLocaleLowerCase()\n return str\n}\n\nexport function normalizeName(name: string, isDynamic: boolean, nuxtStyle = false) {\n if (!isDynamic) return name\n\n return nuxtStyle\n ? name.replace(nuxtDynamicRouteRE, '$1') || 'all'\n : name.replace(replaceDynamicRouteRE, '$1')\n}\n\nexport function buildReactRoutePath(node: string, nuxtStyle = false): string | undefined {\n const isDynamic = isDynamicRoute(node, nuxtStyle)\n const isCatchAll = isCatchAllRoute(node, nuxtStyle)\n const normalizedName = normalizeName(node, isDynamic, nuxtStyle)\n\n if (isDynamic) {\n if (isCatchAll)\n return '*'\n\n return `:${normalizedName}`\n }\n\n return `${normalizedName}`\n}\n\n// https://github.dev/remix-run/remix/blob/264e3f8884c5cafd8d06acc3e01153b376745b7c/packages/remix-dev/config/routesConvention.ts#L105\nexport function buildReactRemixRoutePath(node: string): string | undefined {\n const escapeStart = '['\n const escapeEnd = ']'\n let result = ''\n let rawSegmentBuffer = ''\n\n let inEscapeSequence = 0\n let skipSegment = false\n for (let i = 0; i < node.length; i++) {\n const char = node.charAt(i)\n const lastChar = i > 0 ? node.charAt(i - 1) : undefined\n const nextChar = i < node.length - 1 ? node.charAt(i + 1) : undefined\n\n function isNewEscapeSequence() {\n return (\n !inEscapeSequence && char === escapeStart && lastChar !== escapeStart\n )\n }\n\n function isCloseEscapeSequence() {\n return inEscapeSequence && char === escapeEnd && nextChar !== escapeEnd\n }\n\n function isStartOfLayoutSegment() {\n return char === '_' && nextChar === '_' && !rawSegmentBuffer\n }\n\n if (skipSegment) {\n if (char === '/' || char === '.' || char === win32.sep)\n skipSegment = false\n\n continue\n }\n\n if (isNewEscapeSequence()) {\n inEscapeSequence++\n continue\n }\n\n if (isCloseEscapeSequence()) {\n inEscapeSequence--\n continue\n }\n\n if (inEscapeSequence) {\n result += char\n continue\n }\n\n if (char === '/' || char === win32.sep || char === '.') {\n if (rawSegmentBuffer === 'index' && result.endsWith('index'))\n result = result.replace(replaceIndexRE, '')\n else result += '/'\n\n rawSegmentBuffer = ''\n continue\n }\n\n if (isStartOfLayoutSegment()) {\n skipSegment = true\n continue\n }\n\n rawSegmentBuffer += char\n\n if (char === '$') {\n result += typeof nextChar === 'undefined' ? '*' : ':'\n continue\n }\n\n result += char\n }\n\n if (rawSegmentBuffer === 'index' && result.endsWith('index'))\n result = result.replace(replaceIndexRE, '')\n\n return result || undefined\n}\n\nexport function parsePageRequest(id: string) {\n const [moduleId, rawQuery] = id.split('?', 2)\n const query = new URLSearchParams(rawQuery)\n const pageId = query.get('id')\n return {\n moduleId,\n query,\n pageId,\n }\n}\n","import colors from 'picocolors'\nimport deepEqual from 'deep-equal'\nimport {\n countSlash,\n isCatchAllRoute,\n isDynamicRoute,\n normalizeCase,\n normalizeName,\n} from '../utils'\nimport { generateClientCode } from '../stringify'\n\nimport { getRouteBlock } from '../customBlock'\nimport type { CustomBlock, Optional, PageResolver } from '../types'\nimport type { PageContext } from '../context'\n\nexport interface VueRouteBase {\n name: string\n path: string\n props?: boolean\n component: string\n children?: VueRouteBase[]\n customBlock?: CustomBlock\n rawRoute: string\n}\n\nexport interface VueRoute extends Omit<Optional<VueRouteBase, 'rawRoute' | 'name'>, 'children'> {\n children?: VueRoute[]\n}\n\nfunction prepareRoutes(\n ctx: PageContext,\n routes: VueRoute[],\n parent?: VueRoute,\n) {\n for (const route of routes) {\n if (route.name)\n route.name = route.name.replace(RegExp(`${ctx.options.routeNameSeparator}index$`), '')\n\n if (parent)\n route.path = route.path?.replace(/^\\//, '')\n\n if (route.children)\n route.children = prepareRoutes(ctx, route.children, route)\n\n if (route.children?.find(c => c.name === route.name))\n delete route.name\n\n route.props = true\n\n delete route.rawRoute\n\n if (route.customBlock) {\n Object.assign(route, route.customBlock || {})\n delete route.customBlock\n }\n\n Object.assign(route, ctx.options.extendRoute?.(route, parent) || {})\n }\n\n return routes\n}\n\nasync function computeVueRoutes(ctx: PageContext, customBlockMap: Map<string, CustomBlock>): Promise<VueRoute[]> {\n const { routeStyle, caseSensitive, routeNameSeparator } = ctx.options\n\n const pageRoutes = [...ctx.pageRouteMap.values()]\n // sort routes for HMR\n .sort((a, b) => countSlash(a.route) - countSlash(b.route))\n\n const routes: VueRouteBase[] = []\n\n pageRoutes.forEach((page) => {\n const pathNodes = page.route.split('/')\n\n // add leading slash to component path if not already there\n const component = page.path.replace(ctx.root, '')\n const customBlock = customBlockMap.get(page.path)\n\n const route: VueRouteBase = {\n name: '',\n path: '',\n component,\n customBlock,\n rawRoute: page.route,\n }\n\n let parentRoutes = routes\n let dynamicRoute = false\n\n for (let i = 0; i < pathNodes.length; i++) {\n const node = pathNodes[i]\n const nuxtStyle = routeStyle === 'nuxt'\n const isDynamic = isDynamicRoute(node, nuxtStyle)\n const isCatchAll = isCatchAllRoute(node, nuxtStyle)\n const normalizedName = normalizeName(node, isDynamic, nuxtStyle)\n const normalizedPath = normalizeCase(normalizedName, caseSensitive)\n\n if (isDynamic)\n dynamicRoute = true\n\n route.name += route.name ? `${routeNameSeparator}${normalizedName}` : normalizedName\n\n // Check parent exits\n const parent = parentRoutes.find((parent) => {\n return pathNodes.slice(0, i + 1).join('/') === parent.rawRoute\n })\n\n if (parent) {\n // Make sure children exist in parent\n parent.children = parent.children || []\n // Append to parent's children\n parentRoutes = parent.children\n // Reset path\n route.path = ''\n } else if (normalizedPath === 'index') {\n if (!route.path)\n route.path = '/'\n } else if (normalizedPath !== 'index') {\n if (isDynamic) {\n route.path += `/:${normalizedName}`\n // Catch-all route\n if (isCatchAll) {\n if (i === 0)\n // root cache all route include children\n route.path += '(.*)*'\n else\n // nested cache all route not include children\n route.path += '(.*)'\n } else if (nuxtStyle && i === pathNodes.length - 1) {\n // we need to search if the folder provide `index.vue`\n const isIndexFound = pageRoutes.find(({ route }) => {\n return route === page.route.replace(pathNodes[i], 'index')\n })\n if (!isIndexFound) route.path += '?'\n }\n } else {\n route.path += `/${normalizedPath}`\n }\n }\n }\n\n // put dynamic routes at the end\n if (dynamicRoute)\n parentRoutes.push(route)\n else\n parentRoutes.unshift(route)\n })\n\n let finalRoutes = prepareRoutes(ctx, routes)\n\n finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes\n\n return finalRoutes\n}\n\nasync function resolveVueRoutes(ctx: PageContext, customBlockMap: Map<string, CustomBlock>) {\n const finalRoutes = await computeVueRoutes(ctx, customBlockMap)\n\n let client = generateClientCode(finalRoutes, ctx.options)\n client = (await ctx.options.onClientGenerated?.(client)) || client\n return client\n}\n\nexport function vueResolver(): PageResolver {\n const customBlockMap = new Map<string, CustomBlock>()\n\n async function checkCustomBlockChange(ctx: PageContext, path: string) {\n const exitsCustomBlock = customBlockMap.get(path)\n let customBlock: CustomBlock | undefined\n try {\n customBlock = await getRouteBlock(path, ctx.options)\n } catch (error: any) {\n ctx.logger?.error(colors.red(`[abulo-vite-plugin-pages] ${error.message}`))\n return\n }\n if (!exitsCustomBlock && !customBlock)\n return\n\n if (!customBlock) {\n customBlockMap.delete(path)\n ctx.debug.routeBlock('%s deleted', path)\n return\n }\n if (!exitsCustomBlock || !deepEqual(exitsCustomBlock, customBlock)) {\n ctx.debug.routeBlock('%s old: %O', path, exitsCustomBlock)\n ctx.debug.routeBlock('%s new: %O', path, customBlock)\n customBlockMap.set(path, customBlock)\n ctx.onUpdate()\n }\n }\n\n return {\n resolveExtensions() {\n return ['vue', 'ts', 'js']\n },\n resolveModuleIds() {\n return ['~pages', 'pages-generated', 'virtual:generated-pages']\n },\n async resolveRoutes(ctx) {\n return resolveVueRoutes(ctx, customBlockMap)\n },\n async getComputedRoutes(ctx) {\n return computeVueRoutes(ctx, customBlockMap)\n },\n hmr: {\n added: async(ctx, path) => checkCustomBlockChange(ctx, path),\n changed: async(ctx, path) => checkCustomBlockChange(ctx, path),\n removed: async(_ctx, path) => {\n customBlockMap.delete(path)\n },\n },\n }\n}\n","import { resolveImportMode } from './utils'\nimport { ROUTE_IMPORT_NAME } from './constants'\n\nimport type { ResolvedOptions } from './types'\n\nconst componentRE = /\"(?:component|element)\":(\"(.*?)\")/g\nconst hasFunctionRE = /\"(?:props|beforeEnter)\":(\"(.*?)\")/g\n\nconst multilineCommentsRE = /\\/\\*(.|[\\r\\n])*?\\*\\//gm\nconst singlelineCommentsRE = /\\/\\/.*/g\n\nfunction replaceFunction(_: any, value: any) {\n if (value instanceof Function || typeof value === 'function') {\n const fnBody = value.toString()\n .replace(multilineCommentsRE, '')\n .replace(singlelineCommentsRE, '')\n .replace(/(\\t|\\n|\\r|\\s)/g, '')\n\n // ES6 Arrow Function\n if (fnBody.length < 8 || fnBody.substring(0, 8) !== 'function')\n return `_NuFrRa_${fnBody}`\n\n return fnBody\n }\n\n return value\n}\n\n/**\n * Creates a stringified Vue Router route definition.\n */\nexport function stringifyRoutes(\n preparedRoutes: any[],\n options: ResolvedOptions,\n) {\n const importsMap: Map<string, string> = new Map()\n\n function getImportString(path: string, importName: string) {\n const mode = resolveImportMode(path, options)\n return mode === 'sync'\n ? `import ${importName} from \"${path}\"`\n : `const ${importName} = ${\n options.resolver.stringify?.dynamicImport?.(path) || `() => import(\"${path}\")`\n }`\n }\n\n function componentReplacer(str: string, replaceStr: string, path: string) {\n let importName = importsMap.get(path)\n\n if (!importName)\n importName = ROUTE_IMPORT_NAME.replace('$1', `${importsMap.size}`)\n\n importsMap.set(path, importName)\n\n importName = options.resolver.stringify?.component?.(importName) || importName\n\n return str.replace(replaceStr, importName)\n }\n\n function functionReplacer(str: string, replaceStr: string, content: string) {\n if (content.startsWith('function'))\n return str.replace(replaceStr, content)\n\n if (content.startsWith('_NuFrRa_'))\n return str.replace(replaceStr, content.slice(8))\n\n return str\n }\n\n const stringRoutes = JSON\n .stringify(preparedRoutes, replaceFunction)\n .replace(componentRE, componentReplacer)\n .replace(hasFunctionRE, functionReplacer)\n\n const imports = Array.from(importsMap).map(args => getImportString(...args))\n\n return {\n imports,\n stringRoutes,\n }\n}\n\nexport function generateClientCode(routes: any[], options: ResolvedOptions) {\n const { imports, stringRoutes } = stringifyRoutes(routes, options)\n const code = `${imports.join(';\\n')};\\n\\nconst routes = ${stringRoutes};\\n\\nexport default routes;`\n return options.resolver.stringify?.final?.(code) || code\n}\n","import fs from 'fs'\nimport JSON5 from 'json5'\nimport { parse as YAMLParser } from 'yaml'\n\nimport { importModule } from 'local-pkg'\n\n// @ts-expect-error no type\nimport extractComments from 'extract-comments'\nimport { debug } from './utils'\nimport type { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'\nimport type { CustomBlock, ParsedJSX, ResolvedOptions } from './types'\n\nconst routeJSXReg = /^[\\n\\s]+(route)[\\n\\s]+/gm\n\nexport function parseJSX(code: string): ParsedJSX[] {\n return extractComments(code).slice(0, 1)\n .filter((comment: ParsedJSX) => routeJSXReg.test(comment.value) && comment.value.includes(':') && comment.loc.start.line === 1)\n}\n\nexport function parseYamlComment(code: ParsedJSX[], path: string): CustomBlock {\n return code.reduce((memo, item) => {\n const { value } = item\n const v = value.replace(routeJSXReg, '')\n debug.routeBlock(`use ${v} parser`)\n try {\n const yamlResult = YAMLParser(v)\n\n return {\n ...memo,\n ...yamlResult,\n }\n } catch (err: any) {\n throw new Error(`Invalid YAML format of comment in ${path}\\n${err.message}`)\n }\n }, {})\n}\n\nexport async function parseSFC(code: string): Promise<SFCDescriptor> {\n try {\n const { parse } = await importModule('@vue/compiler-sfc') as typeof import('@vue/compiler-sfc')\n return parse(code, {\n pad: 'space',\n }).descriptor\n // for @vue/compiler-sfc ^2.7\n || (parse as any)({\n source: code,\n })\n } catch {\n throw new Error('[abulo-vite-plugin-pages] Vue3\\'s \"@vue/compiler-sfc\" is required.')\n }\n}\n\nexport function parseCustomBlock(block: SFCBlock, filePath: string, options: ResolvedOptions): any {\n const lang = block.lang ?? options.routeBlockLang\n\n debug.routeBlock(`use ${lang} parser`)\n\n if (lang === 'json5') {\n try {\n return JSON5.parse(block.content)\n } catch (err: any) {\n throw new Error(`Invalid JSON5 format of <${block.type}> content in ${filePath}\\n${err.message}`)\n }\n } else if (lang === 'json') {\n try {\n return JSON.parse(block.content)\n } catch (err: any) {\n throw new Error(`Invalid JSON format of <${block.type}> content in ${filePath}\\n${err.message}`)\n }\n } else if (lang === 'yaml' || lang === 'yml') {\n try {\n return YAMLParser(block.content)\n } catch (err: any) {\n throw new Error(`Invalid YAML format of <${block.type}> content in ${filePath}\\n${err.message}`)\n }\n }\n}\n\nexport async function getRouteBlock(path: string, options: ResolvedOptions) {\n const content = fs.readFileSync(path, 'utf8')\n\n const parsedSFC = await parseSFC(content)\n const blockStr = parsedSFC?.customBlocks.find(b => b.type === 'route')\n\n const parsedJSX = parseJSX(content)\n\n if (!blockStr && parsedJSX.length === 0)\n return\n\n let result\n\n if (blockStr)\n result = parseCustomBlock(blockStr, path, options) as CustomBlock\n\n if (parsedJSX.length > 0)\n result = parseYamlComment(parsedJSX, path) as CustomBlock\n\n return result\n}\n","import {\n buildReactRemixRoutePath,\n buildReactRoutePath,\n countSlash,\n normalizeCase,\n} from '../utils'\nimport { generateClientCode } from '../stringify'\n\nimport type { Optional, PageResolver, ResolvedOptions } from '../types'\nimport type { PageContext } from '../context'\n\nexport interface ReactRouteBase {\n caseSensitive?: boolean\n children?: ReactRouteBase[]\n element?: string\n index?: boolean\n path?: string\n rawRoute: string\n}\n\nexport interface ReactRoute extends Omit<Optional<ReactRouteBase, 'rawRoute' | 'path'>, 'children'> {\n children?: ReactRoute[]\n}\n\nfunction prepareRoutes(\n routes: ReactRoute[],\n options: ResolvedOptions,\n parent?: ReactRoute,\n) {\n for (const route of routes) {\n if (parent)\n route.path = route.path?.replace(/^\\//, '')\n\n if (route.children)\n route.children = prepareRoutes(route.children, options, route)\n\n delete route.rawRoute\n\n Object.assign(route, options.extendRoute?.(route, parent) || {})\n }\n\n return routes\n}\n\nasync function computeReactRoutes(ctx: PageContext): Promise<ReactRoute[]> {\n const { routeStyle, caseSensitive } = ctx.options\n const nuxtStyle = routeStyle === 'nuxt'\n\n const pageRoutes = [...ctx.pageRouteMap.values()]\n // sort routes for HMR\n .sort((a, b) => countSlash(a.route) - countSlash(b.route))\n\n const routes: ReactRouteBase[] = []\n\n pageRoutes.forEach((page) => {\n const pathNodes = page.route.split('/')\n const element = page.path.replace(ctx.root, '')\n let parentRoutes = routes\n\n for (let i = 0; i < pathNodes.length; i++) {\n const node = pathNodes[i]\n\n const route: ReactRouteBase = {\n caseSensitive,\n path: '',\n rawRoute: pathNodes.slice(0, i + 1).join('/'),\n }\n\n if (i === pathNodes.length - 1) route.element = element\n\n const isIndexRoute = normalizeCase(node, caseSensitive).endsWith('index')\n\n if (!route.path && isIndexRoute) {\n route.path = '/'\n } else if (!isIndexRoute) {\n if (routeStyle === 'remix')\n route.path = buildReactRemixRoutePath(node)\n else\n route.path = buildReactRoutePath(node, nuxtStyle)\n }\n\n // Check parent exits\n const parent = parentRoutes.find((parent) => {\n return pathNodes.slice(0, i).join('/') === parent.rawRoute\n })\n\n if (parent) {\n // Make sure children exits in parent\n parent.children = parent.children || []\n // Append to parent's children\n parentRoutes = parent.children\n }\n\n const exits = parentRoutes.some((parent) => {\n return pathNodes.slice(0, i + 1).join('/') === parent.rawRoute\n })\n if (!exits)\n parentRoutes.push(route)\n }\n })\n\n // sort by dynamic routes\n let finalRoutes = prepareRoutes(routes, ctx.options)\n\n finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes\n\n return finalRoutes\n}\n\nasync function resolveReactRoutes(ctx: PageContext) {\n const finalRoutes = await computeReactRoutes(ctx)\n let client = generateClientCode(finalRoutes, ctx.options)\n client = (await ctx.options.onClientGenerated?.(client)) || client\n return client\n}\n\nexport function reactResolver(): PageResolver {\n return {\