vite-plugin-pages
Version:
File system base vue-router plugin for Vite
1 lines • 82.6 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/context.ts","../node_modules/.pnpm/@antfu+utils@9.2.0/node_modules/@antfu/utils/dist/index.mjs","../src/files.ts","../src/utils.ts","../src/options.ts","../src/stringify.ts","../src/resolvers/react.ts","../src/resolvers/solid.ts","../src/resolvers/vue.ts","../src/customBlock.ts"],"sourcesContent":["import type { Plugin } from 'vite'\nimport type { UserOptions } from './types'\nimport { MODULE_ID_VIRTUAL, ROUTE_BLOCK_ID_VIRTUAL, routeBlockQueryRE } from './constants'\n\nimport { PageContext } from './context'\nimport { parsePageRequest } from './utils'\n\nfunction pagesPlugin(userOptions: UserOptions = {}): Plugin {\n let ctx: PageContext\n\n return {\n name: 'vite-plugin-pages',\n enforce: 'pre',\n async configResolved(config) {\n // auto set resolver for react project\n if (\n !userOptions.resolver\n && config.plugins.find(i => i.name.includes('vite:react'))\n ) {\n userOptions.resolver = 'react'\n }\n\n // auto set resolver for solid project\n if (\n !userOptions.resolver\n && config.plugins.find(i => i.name.includes('solid'))\n ) {\n userOptions.resolver = 'solid'\n }\n\n ctx = new PageContext(userOptions, config.root)\n ctx.setLogger(config.logger)\n await ctx.searchGlob()\n },\n api: {\n getResolvedRoutes() {\n return ctx.options.resolver.getComputedRoutes(ctx)\n },\n },\n configureServer(server) {\n ctx.setupViteServer(server)\n },\n resolveId(id) {\n if (ctx.options.moduleIds.includes(id))\n return `${MODULE_ID_VIRTUAL}?id=${id}`\n\n if (routeBlockQueryRE.test(id))\n return ROUTE_BLOCK_ID_VIRTUAL\n\n return null\n },\n async load(id) {\n const {\n moduleId,\n pageId,\n } = parsePageRequest(id)\n\n if (moduleId === MODULE_ID_VIRTUAL && pageId && ctx.options.moduleIds.includes(pageId))\n return ctx.resolveRoutes()\n\n if (id === ROUTE_BLOCK_ID_VIRTUAL) {\n return {\n code: 'export default {};',\n map: null,\n }\n }\n\n return null\n },\n }\n}\n\nexport { syncIndexResolver } from './options'\nexport type {\n ReactRoute,\n SolidRoute,\n VueRoute,\n} from './resolvers'\n\nexport {\n reactResolver,\n solidResolver,\n vueResolver,\n} from './resolvers'\nexport * from './types'\nexport { PageContext }\nexport default pagesPlugin\n","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 = 'virtual:vite-plugin-pages/generated-pages'\nexport const ROUTE_BLOCK_ID_VIRTUAL = 'virtual: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 type { Logger, ViteDevServer } from 'vite'\nimport type { PageOptions, ResolvedOptions, UserOptions } from './types'\nimport { join, resolve } from 'node:path'\nimport process from 'node:process'\nimport { slash, toArray } from '@antfu/utils'\nimport { getPageFiles } from './files'\nimport { resolveOptions } from './options'\n\nimport { debug, invalidatePagesModule, isTarget } from './utils'\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: ViteDevServer['watcher']) {\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 extension = this.options.extensions.find(ext => p.endsWith(`.${ext}`))\n if (!extension)\n continue\n\n const route = slash(join(pageDir.baseRoute, p.replace(`${pageDirPath}/`, '').replace(`.${extension}`, '')))\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}\nfunction lerp(min, max, t) {\n const interpolation = clamp(t, 0, 1);\n return min + (max - min) * interpolation;\n}\nfunction remap(n, inMin, inMax, outMin, outMax) {\n const interpolation = (n - inMin) / (inMax - inMin);\n return lerp(outMin, outMax, interpolation);\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 = Array.from({ length: 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, quantity) {\n return Array.from({ length: quantity }, (_) => 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}\nfunction filterInPlace(array, predicate) {\n for (let i = array.length; i--; i >= 0) {\n if (!predicate(array[i], i, array))\n array.splice(i, 1);\n }\n return array;\n}\n\nfunction assert(condition, message) {\n if (!condition)\n throw new Error(message);\n}\nconst toString = (v) => Object.prototype.toString.call(v);\nfunction 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}\nfunction noop() {\n}\n\nfunction isDeepEqual(value1, value2) {\n const type1 = getTypeName(value1);\n const type2 = getTypeName(value2);\n if (type1 !== type2)\n return false;\n if (type1 === \"array\") {\n if (value1.length !== value2.length)\n return false;\n return value1.every((item, i) => {\n return isDeepEqual(item, value2[i]);\n });\n }\n if (type1 === \"object\") {\n const keyArr = Object.keys(value1);\n if (keyArr.length !== Object.keys(value2).length)\n return false;\n return keyArr.every((key) => {\n return isDeepEqual(value1[key], value2[key]);\n });\n }\n return Object.is(value1, value2);\n}\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 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]\";\nfunction isPrimitive(val) {\n return !val || Object(val) !== val;\n}\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 const [firstArg, fallback] = args;\n if (isObject(firstArg)) {\n const vars = firstArg;\n return str.replace(/\\{(\\w+)\\}/g, (_, key) => vars[key] || ((typeof fallback === \"function\" ? fallback(key) : fallback) ?? key));\n } else {\n return str.replace(/\\{(\\d+)\\}/g, (_, key) => {\n const index = Number(key);\n if (Number.isNaN(index))\n return key;\n return args[index];\n });\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}\nconst _reFullWs = /^\\s*$/;\nfunction unindent(str) {\n const lines = (typeof str === \"string\" ? str : str[0]).split(\"\\n\");\n const whitespaceLines = lines.map((line) => _reFullWs.test(line));\n const commonIndent = lines.reduce((min, line, idx) => {\n if (whitespaceLines[idx])\n return min;\n const indent = line.match(/^\\s*/)?.[0].length;\n return indent === void 0 ? min : Math.min(min, indent);\n }, Number.POSITIVE_INFINITY);\n let emptyLinesHead = 0;\n while (emptyLinesHead < lines.length && whitespaceLines[emptyLinesHead])\n emptyLinesHead++;\n let emptyLinesTail = 0;\n while (emptyLinesTail < lines.length && whitespaceLines[lines.length - emptyLinesTail - 1])\n emptyLinesTail++;\n return lines.slice(emptyLinesHead, lines.length - emptyLinesTail).map((line) => line.slice(commonIndent)).join(\"\\n\");\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 (key === \"__proto__\" || key === \"constructor\" || key === \"prototype\")\n return;\n if (isMergableObject(source[key])) {\n if (!target[key])\n target[key] = {};\n if (isMergableObject(target[key])) {\n deepMerge(target[key], source[key]);\n } else {\n target[key] = source[key];\n }\n } else {\n target[key] = source[key];\n }\n });\n }\n return deepMerge(target, ...sources);\n}\nfunction deepMergeWithArray(target, ...sources) {\n if (!sources.length)\n return target;\n const source = sources.shift();\n if (source === void 0)\n return target;\n if (Array.isArray(target) && Array.isArray(source))\n target.push(...source);\n if (isMergableObject(target) && isMergableObject(source)) {\n objectKeys(source).forEach((key) => {\n if (key === \"__proto__\" || key === \"constructor\" || key === \"prototype\")\n return;\n if (Array.isArray(source[key])) {\n if (!target[key])\n target[key] = [];\n deepMergeWithArray(target[key], source[key]);\n } else if (isMergableObject(source[key])) {\n if (!target[key])\n target[key] = {};\n deepMergeWithArray(target[key], source[key]);\n } else {\n target[key] = source[key];\n }\n });\n }\n return deepMergeWithArray(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}\nconst _objectIdMap = /* @__PURE__ */ new WeakMap();\nfunction objectId(obj) {\n if (isPrimitive(obj))\n return obj;\n if (!_objectIdMap.has(obj)) {\n _objectIdMap.set(obj, randomStr());\n }\n return _objectIdMap.get(obj);\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\tpeek() {\n\t\tif (!this.#head) {\n\t\t\treturn;\n\t\t}\n\n\t\treturn this.#head.value;\n\n\t\t// TODO: Node.js 18.\n\t\t// return this.#head?.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\tvalidateConcurrency(concurrency);\n\n\tconst queue = new Queue();\n\tlet activeCount = 0;\n\n\tconst resumeNext = () => {\n\t\tif (activeCount < concurrency && queue.size > 0) {\n\t\t\tqueue.dequeue()();\n\t\t\t// Since `pendingCount` has been decreased by one, increase `activeCount` by one.\n\t\t\tactiveCount++;\n\t\t}\n\t};\n\n\tconst next = () => {\n\t\tactiveCount--;\n\n\t\tresumeNext();\n\t};\n\n\tconst run = async (function_, resolve, arguments_) => {\n\t\tconst result = (async () => function_(...arguments_))();\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 = (function_, resolve, arguments_) => {\n\t\t// Queue `internalResolve` instead of the `run` function\n\t\t// to preserve asynchronous context.\n\t\tnew Promise(internalResolve => {\n\t\t\tqueue.enqueue(internalResolve);\n\t\t}).then(\n\t\t\trun.bind(undefined, function_, resolve, arguments_),\n\t\t);\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// after the `internalResolve` 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) {\n\t\t\t\tresumeNext();\n\t\t\t}\n\t\t})();\n\t};\n\n\tconst generator = (function_, ...arguments_) => new Promise(resolve => {\n\t\tenqueue(function_, resolve, arguments_);\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\tconcurrency: {\n\t\t\tget: () => concurrency,\n\n\t\t\tset(newConcurrency) {\n\t\t\t\tvalidateConcurrency(newConcurrency);\n\t\t\t\tconcurrency = newConcurrency;\n\n\t\t\t\tqueueMicrotask(() => {\n\t\t\t\t\t// eslint-disable-next-line no-unmodified-loop-condition\n\t\t\t\t\twhile (activeCount < concurrency && queue.size > 0) {\n\t\t\t\t\t\tresumeNext();\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t});\n\n\treturn generator;\n}\n\nfunction validateConcurrency(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\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 }\n promises = /* @__PURE__ */ new Set();\n get promise() {\n let batch;\n const items = [...Array.from(this.items), ...Array.from(this.promises)];\n if (this.options?.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(onfulfilled, onrejected) {\n return this.promise.then(onfulfilled, onrejected);\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\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?.();\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\nconst timestamp = () => +Date.now();\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$1 (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$1 (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$1(delay, callback, {\n debounceMode: atBegin !== false\n });\n}\n\nfunction throttle(...args) {\n return throttle$1(...args);\n}\nfunction debounce(...args) {\n return debounce$1(...args);\n}\n\nexport { assert, at, batchInvoke, capitalize, clamp, clampArrayRange, clearUndefined, createControlledPromise, createPromiseLock, createSingletonPromise, debounce, deepMerge, deepMergeWithArray, ensurePrefix, ensureSuffix, filterInPlace, flattenArrayable, getTypeName, hasOwnProperty, invoke, isBoolean, isBrowser, isDate, isDeepEqual, isDef, isFunction, isKeyOf, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isTruthy, isUndefined, isWindow, last, lerp, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectId, objectKeys, objectMap, objectPick, p, partition, randomStr, range, remap, remove, sample, shuffle, slash, sleep, sum, tap, template, throttle, timestamp, toArray, toString, unindent, uniq, uniqueBy };\n","import type { PageOptions, ResolvedOptions } from './types'\nimport { join } from 'node:path'\nimport { slash } from '@antfu/utils'\nimport { globSync } from 'tinyglobby'\n\nimport { extsToGlob } from './utils'\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 = globSync(slash(PageOptions.dir), {\n ignore: exclude,\n onlyDirectories: true,\n dot: true,\n expandDirectories: false,\n cwd: root,\n })\n\n const pageDirs = dirs.map(dir => ({\n ...PageOptions,\n dir: dir.replace(/\\/$/, ''),\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?.filePattern ?? `**/*.${ext}`\n\n const files = globSync(pattern, {\n ignore: exclude,\n onlyFiles: true,\n cwd: path,\n }).map(p => slash(join(path, p)))\n\n return files\n}\n","import type { ModuleNode, ViteDevServer } from 'vite'\nimport type { ResolvedOptions } from './types'\nimport { resolve, win32 } from 'node:path'\nimport { URLSearchParams } from 'node:url'\nimport { slash } from '@antfu/utils'\nimport Debug from 'debug'\nimport micromatch from 'micromatch'\n\nimport { cacheAllRouteRE, countSlashRE, dynamicRouteRE, MODULE_ID_VIRTUAL, nuxtCacheAllRouteRE, nuxtDynamicRouteRE, replaceDynamicRouteRE, replaceIndexRE } from './constants'\n\nexport const debug = {\n hmr: Debug('vite-plugin-pages:hmr'),\n routeBlock: Debug('vite-plugin-pages:routeBlock'),\n options: Debug('vite-plugin-pages:options'),\n pages: Debug('vite-plugin-pages:pages'),\n search: Debug('vite-plugin-pages:search'),\n env: Debug('vite-plugin-pages:env'),\n cache: Debug('vite-plugin-pages:cache'),\n resolver: Debug('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))\n return true\n }\n return false\n}\n\nexport function isTarget(path: string, options: ResolvedOptions) {\n return isPagesDir(path, options) && !micromatch.isMatch(path, options.exclude) && 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)\n return str.toLocaleLowerCase()\n return str\n}\n\nexport function normalizeName(name: string, isDynamic: boolean, nuxtStyle = false) {\n if (!isDynamic)\n 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 type { ImportModeResolver, ResolvedOptions, UserOptions } from './types'\nimport { resolve } from 'node:path'\nimport process from 'node:process'\nimport { slash, toArray } from '@antfu/utils'\n\nimport { MODULE_IDS } from './constants'\nimport { getPageDirs } from './files'\nimport { reactResolver, solidResolver, vueResolver } from './resolvers'\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\nfunction 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 = ['src/pages'],\n routeBlockLang = 'json5',\n exclude = ['node_modules', '.git', '**/__*__/**'],\n caseSensitive = false,\n routeNameSeparator = '-',\n extendRoute,\n onRoutesGenerated,\n onClientGenerated,\n } = userOptions\n\n const root = viteRoot || slash(process.cwd())\n\n const importMode = userOptions.importMode || syncIndexResolver\n\n const importPath = userOptions.importPath || 'relative'\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.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 importPath,\n exclude,\n caseSensitive,\n resolver,\n extensionsRE,\n extendRoute,\n onRoutesGenerated,\n onClientGenerated,\n routeNameSeparator,\n }\n\n return resolvedOptions\n}\n","import type { ResolvedOptions } from './types'\nimport { ROUTE_IMPORT_NAME } from './constants'\n\nimport { resolveImportMode } from './utils'\n\nconst componentRE = /\"(?:component|element)\":(\"(.*?)\")/g\nconst hasFunctionRE = /\"(?:props|beforeEnter)\":(\"(.*?)\")/g\n\nconst multilineCommentsRE = /\\/\\*(.|[\\r\\n])*?\\*\\//g\nconst singlelineCommentsRE = /\\/\\/.*/g\n\nfunction replaceFunction(_: any, value: any) {\n if (typeof value === 'function' || typeof value === 'function') {\n const fnBody = value.toString()\n .replace(multilineCommentsRE, '')\n .replace(singlelineCommentsRE, '')\n .replace(/(\\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 type { PageContext } from '../context'\nimport type { Optional, PageResolver, ResolvedOptions } from '../types'\n\nimport { generateClientCode } from '../stringify'\nimport {\n buildReactRemixRoutePath,\n buildReactRoutePath,\n countSlash,\n normalizeCase,\n} from '../utils'\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, importPath } = 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 = importPath === 'relative' ? page.path.replace(ctx.root, '') : page.path\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)\n route.element = element\n\n const isIndexRoute = normalizeCase(node, caseSensitive).endsWith('index')\n\n if (!route.path && isIndexRoute) {\n route.path = '/'\n }\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 {\n resolveModuleIds() {\n return ['~react-pages', 'virtual:generated-pages-react']\n },\n resolveExtensions() {\n return ['tsx', 'jsx', 'ts', 'js']\n },\n async resolveRoutes(ctx) {\n return resolveReactRoutes(ctx)\n },\n async getComputedRoutes(ctx) {\n return computeReactRoutes(ctx)\n },\n stringify: {\n component: path => `React.createElement(${path})`,\n dynamicImport: path => `React.lazy(() => import(\"${path}\"))`,\n final: code => `import React from \"react\";\\n${code}`,\n },\n }\n}\n","import type { PageContext } from '../context'\nimport type { Optional, PageResolver, ResolvedOptions } from '../types'\n\nimport { generateClientCode } from '../stringify'\nimport {\n buildReactRemixRoutePath,\n buildReactRoutePath,\n countSlash,\n normalizeCase,\n} from '../utils'\n\nexport interface SolidRouteBase {\n rawRoute: string\n path: string\n children?: SolidRouteBase[]\n component?: string\n element?: string\n}\n\nexport interface SolidRoute extends Omit<Optional<SolidRouteBase, 'rawRoute' | 'path'>, 'children'> {\n children?: SolidRoute[]\n}\n\nfunction prepareRoutes(\n options: ResolvedOptions,\n routes: SolidRoute[],\n parent?: SolidRoute,\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(options, route.children, route)\n\n delete route.rawRoute\n\n Object.assign(route, options.extendRoute?.(route, parent) || {})\n }\n\n return routes\n}\n\nasync function computeSolidRoutes(ctx: PageContext): Promise<SolidRoute[]> {\n const { routeStyle, caseSensitive, importPath } = 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: SolidRouteBase[] = []\n\n pageRoutes.forEach((page) => {\n const pathNodes = page.route.split('/')\n\n const component = importPath === 'relative' ? page.path.replace(ctx.root, '') : page.path\n const element = importPath === 'relative' ? page.path.replace(ctx.root, '') : pa