next-vite-router 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,193 +1,227 @@
1
- import { n as p, b as z } from "./tree-HwBRWJNI.js";
2
- import { a as V, e as W } from "./tree-HwBRWJNI.js";
3
- import c, { Suspense as d, lazy as b, createContext as v, useContext as L } from "react";
4
- import { useParams as A } from "react-router-dom";
5
- const M = {
1
+ import { p as h, r as F, b } from "./tree-DfnssFxU.js";
2
+ import { a as et, c as nt, e as rt, n as at } from "./tree-DfnssFxU.js";
3
+ import u, { lazy as v, Suspense as z, createContext as $, useContext as A } from "react";
4
+ import { useParams as k } from "react-router-dom";
5
+ const d = {
6
6
  enableLazyLoading: !0
7
7
  };
8
- let m = { ...M };
9
- function j(e) {
10
- m = { ...m, ...e };
11
- }
12
- function k() {
13
- return m;
14
- }
15
- let g = [];
16
- function q(e, t) {
17
- g.push({ pattern: e, middleware: t });
18
- }
19
- function B() {
20
- g = [];
21
- }
22
- function h(e, t) {
23
- let n = t;
24
- for (const r of g)
25
- r.pattern.lastIndex = 0, r.pattern.test(e) && (n = r.middleware(n)), r.pattern.lastIndex = 0;
26
- return n;
27
- }
28
- const S = () => c.createElement("div"), C = /* @__PURE__ */ new Map();
29
- function R(e, t, n) {
30
- const r = k();
31
- if (r.enableLazyLoading === !1) {
32
- const s = c.lazy(e), i = c.createElement(
33
- d,
34
- { fallback: null },
35
- // No loading fallback when disabled
36
- c.createElement(s)
37
- );
38
- return n ? h(n, i) : i;
39
- }
40
- let a = C.get(t);
41
- a || (a = b(e), C.set(t, a));
42
- const l = r.loadingFallback || S, o = c.createElement(
43
- d,
44
- { fallback: c.createElement(l) },
45
- c.createElement(a)
8
+ let i = { ...d };
9
+ function U(t) {
10
+ i = { ...i, ...t };
11
+ }
12
+ function V() {
13
+ i = { ...d };
14
+ }
15
+ function N() {
16
+ return i;
17
+ }
18
+ let p = [];
19
+ function W(t, e) {
20
+ p.push({ pattern: t, middleware: e });
21
+ }
22
+ function X() {
23
+ p = [];
24
+ }
25
+ function R(t, e) {
26
+ let r = e;
27
+ for (const n of p)
28
+ n.pattern.lastIndex = 0, n.pattern.test(t) && (r = n.middleware(r)), n.pattern.lastIndex = 0;
29
+ return r;
30
+ }
31
+ const M = () => u.createElement("div"), m = /* @__PURE__ */ new Map();
32
+ function T() {
33
+ const t = N();
34
+ if (t.enableLazyLoading === !1)
35
+ return null;
36
+ const e = t.loadingFallback || M;
37
+ return u.createElement(e);
38
+ }
39
+ function Y() {
40
+ m.clear();
41
+ }
42
+ function C(t, e, r) {
43
+ let n = m.get(e);
44
+ n || (n = v(t), m.set(e, n));
45
+ const a = u.createElement(
46
+ z,
47
+ { fallback: u.createElement(T) },
48
+ u.createElement(n)
46
49
  );
47
- return n ? h(n, o) : o;
50
+ return r ? R(r, a) : a;
48
51
  }
49
- const w = v(void 0);
50
- function D({
51
- name: e,
52
- children: t
52
+ const y = $(void 0);
53
+ function I({
54
+ name: t,
55
+ children: e
53
56
  }) {
54
- return c.createElement(
55
- w.Provider,
56
- { value: e },
57
- t
57
+ return u.createElement(
58
+ y.Provider,
59
+ { value: t },
60
+ e
58
61
  );
59
62
  }
60
- function N() {
61
- return L(w);
62
- }
63
- function y(e, t) {
64
- return h(e, c.createElement(t));
65
- }
66
- function T(e) {
67
- return /^\[\[\.\.\..+\]\]$/.test(e);
68
- }
69
- function f(e, t, n = !1) {
70
- const r = e.page;
71
- if (!r)
72
- throw new Error(`Route node "${e.fullPath}" has no page loader`);
73
- const a = R(
74
- async () => ({ default: (await r()).default }),
75
- `page:${e.fullPath || t}`,
76
- t
77
- ), l = e.segment.match(/^\[\[?\.\.\.(.+?)\]\]?$/), o = l ? c.createElement(D, {
78
- name: l[1],
63
+ function S() {
64
+ return A(y);
65
+ }
66
+ function P(t, e) {
67
+ return R(t, u.createElement(e));
68
+ }
69
+ function D(t) {
70
+ const e = h(t)[0];
71
+ return (e == null ? void 0 : e.kind) === "catchAll" && e.optional;
72
+ }
73
+ function g(t) {
74
+ const e = h(t)[0];
75
+ return e ? F(e) : "";
76
+ }
77
+ function c(t, e) {
78
+ return f(t, e, !0);
79
+ }
80
+ function f(t, e, r = !1) {
81
+ const n = t.page;
82
+ if (!n)
83
+ throw new Error(`Route node "${t.fullPath}" has no page loader`);
84
+ const a = C(
85
+ async () => ({ default: (await n()).default }),
86
+ `page:${t.fullPath || e}`,
87
+ e
88
+ ), o = h(t.segment)[0], l = (o == null ? void 0 : o.kind) === "catchAll" ? u.createElement(I, {
89
+ name: o.name,
79
90
  children: a
80
91
  }) : a;
81
- return n ? { index: !0, element: o } : { path: p(e.segment), element: o };
92
+ return r ? { index: !0, element: l } : { path: g(t.segment), element: l };
82
93
  }
83
- function P(e) {
84
- const t = e.notFound;
85
- if (!t)
86
- throw new Error(`Route node "${e.fullPath}" has no not-found loader`);
87
- const n = e.fullPath ? `/${e.fullPath}` : "/";
94
+ function G(t) {
95
+ const e = t.notFound;
96
+ if (!e)
97
+ throw new Error(`Route node "${t.fullPath}" has no not-found loader`);
98
+ const r = t.fullPath ? `/${t.fullPath}` : "/";
88
99
  return {
89
100
  path: "*",
90
- element: R(
91
- async () => ({ default: (await t()).default }),
92
- `notfound:${e.fullPath}`,
93
- n
101
+ element: C(
102
+ async () => ({ default: (await e()).default }),
103
+ `notfound:${t.fullPath}`,
104
+ r
94
105
  )
95
106
  };
96
107
  }
97
- function F(e, t) {
98
- const n = [];
99
- return [...e.children.entries()].sort(([r], [a]) => r.localeCompare(a)).forEach(([r, a]) => {
100
- const l = p(r), o = l ? t ? `${t}/${l}` : `/${l}` : t;
101
- n.push(...$(a, !1, o));
102
- }), n;
108
+ function E(t) {
109
+ return t.notFound ? [G(t)] : [];
103
110
  }
104
- function E(e, t) {
105
- const n = [];
106
- return e.page && n.push(f(e, t || "/", !0)), n.push(...F(e, t)), e.notFound && n.push(P(e)), n;
111
+ function O(t, e, r, n) {
112
+ return {
113
+ ...e ? { path: e } : {},
114
+ element: P(t, r),
115
+ children: n.length > 0 ? n : void 0
116
+ };
107
117
  }
108
- function $(e, t = !1, n = "") {
118
+ function w(t, e) {
109
119
  const r = [];
110
- if (t) {
111
- const u = E(e, "");
112
- return e.layout ? (r.push({
113
- path: "/",
114
- element: y("/", e.layout),
115
- children: u
116
- }), r) : e.page ? u.length > 1 ? [{
117
- path: "/",
118
- children: u
119
- }] : [{
120
- path: "/",
121
- element: f(e, "/", !1).element
122
- }] : e.notFound ? (r.push({
123
- path: "/",
124
- children: u
125
- }), r) : u;
126
- }
127
- const a = p(e.segment), l = !a, o = l ? n : n + "/" + a, s = F(e, o), i = e.notFound ? [P(e)] : [];
128
- if (e.layout) {
129
- const u = E(e, o);
130
- return [{
131
- ...l ? {} : { path: a },
132
- element: y(o, e.layout),
133
- children: u.length > 0 ? u : void 0
134
- }];
135
- }
136
- if (e.page && !l) {
137
- if (T(e.segment)) {
138
- const u = f(e, n, !0), x = {
139
- path: a,
140
- element: f(e, o).element,
141
- children: [...s, ...i].length > 0 ? [...s, ...i] : void 0
142
- };
143
- return [u, x];
144
- }
145
- return s.length > 0 || i.length > 0 ? [{
146
- path: a,
147
- children: [
148
- f(e, o, !0),
149
- ...s,
150
- ...i
151
- ]
152
- }] : [{
153
- path: a,
154
- element: f(e, o).element
155
- }];
156
- }
157
- if (e.page || e.notFound || !l) {
158
- const u = [
159
- ...e.page ? [f(e, o, !0)] : [],
160
- ...s,
161
- ...i
120
+ return [...t.children.entries()].sort(([n], [a]) => n.localeCompare(a)).forEach(([n, a]) => {
121
+ const o = g(n), l = o ? e ? `${e}/${o}` : `/${o}` : e;
122
+ r.push(...x(a, !1, l));
123
+ }), r;
124
+ }
125
+ function L(t, e) {
126
+ const r = [];
127
+ return t.page && r.push(c(t, e || "/")), r.push(...w(t, e)), r.push(...E(t)), r;
128
+ }
129
+ function j(t, e, r, n, a, o) {
130
+ if (D(t.segment)) {
131
+ const l = [...a, ...o];
132
+ return [
133
+ c(t, e),
134
+ {
135
+ path: n,
136
+ element: f(t, r).element,
137
+ children: l.length > 0 ? l : void 0
138
+ }
162
139
  ];
163
- return [{
164
- ...l ? {} : { path: a },
165
- children: u.length > 0 ? u : void 0
166
- }];
167
140
  }
168
- return s;
169
- }
170
- function H(e, t, n, r) {
141
+ return a.length > 0 || o.length > 0 ? [{
142
+ path: n,
143
+ children: [
144
+ c(t, r),
145
+ ...a,
146
+ ...o
147
+ ]
148
+ }] : [{
149
+ path: n,
150
+ element: f(t, r).element
151
+ }];
152
+ }
153
+ function q(t, e, r, n) {
154
+ const a = [
155
+ ...t.page ? [c(t, e)] : [],
156
+ ...r,
157
+ ...n
158
+ ];
159
+ return [{
160
+ children: a.length > 0 ? a : void 0
161
+ }];
162
+ }
163
+ function B(t) {
164
+ const e = L(t, "");
165
+ return t.layout ? [{
166
+ path: "/",
167
+ element: P("/", t.layout),
168
+ children: e
169
+ }] : t.page ? e.length > 1 ? [{ path: "/", children: e }] : [{
170
+ path: "/",
171
+ element: f(t, "/", !1).element
172
+ }] : t.notFound ? [{ path: "/", children: e }] : e;
173
+ }
174
+ function x(t, e = !1, r = "") {
175
+ if (e)
176
+ return B(t);
177
+ const n = g(t.segment), a = !n, o = a ? r : r + "/" + n, l = w(t, o), s = E(t);
178
+ return t.layout ? [O(
179
+ o,
180
+ a ? "" : n,
181
+ t.layout,
182
+ L(t, o)
183
+ )] : t.page && !a ? j(
184
+ t,
185
+ r,
186
+ o,
187
+ n,
188
+ l,
189
+ s
190
+ ) : t.page || t.notFound || !a ? a ? q(t, o, l, s) : [{
191
+ path: n,
192
+ children: [
193
+ ...t.page ? [c(t, o)] : [],
194
+ ...l,
195
+ ...s
196
+ ]
197
+ }] : l;
198
+ }
199
+ function Z(t, e, r, n) {
171
200
  return () => {
172
- const a = z(e, t, n, r);
173
- return $(a, !0);
201
+ const a = b(t, e, r, n);
202
+ return x(a, !0);
174
203
  };
175
204
  }
176
- function J() {
177
- const e = A(), t = N();
178
- return typeof t == "string" && e["*"] !== void 0 ? { ...e, [t]: e["*"] } : e;
205
+ function _() {
206
+ const t = k(), e = S();
207
+ return typeof e == "string" && t["*"] !== void 0 ? { ...t, [e]: t["*"] } : t;
179
208
  }
180
209
  export {
181
- D as CatchAllParamProvider,
182
- V as assertNoDuplicateRoutes,
183
- z as buildRouteTree,
184
- B as clearMiddlewares,
185
- j as configureRouter,
186
- R as createLazyElement,
187
- H as createRouteGenerator,
188
- W as extractRoutePath,
189
- p as normalizeSegment,
190
- q as registerMiddleware,
191
- $ as treeToRoutes,
192
- J as useParams
210
+ I as CatchAllParamProvider,
211
+ et as assertNoDuplicateRoutes,
212
+ nt as assertNoRouteConflicts,
213
+ b as buildRouteTree,
214
+ Y as clearLazyCache,
215
+ X as clearMiddlewares,
216
+ U as configureRouter,
217
+ C as createLazyElement,
218
+ Z as createRouteGenerator,
219
+ rt as extractRoutePath,
220
+ at as normalizeSegment,
221
+ h as parseRouteSegments,
222
+ W as registerMiddleware,
223
+ V as resetRouterConfig,
224
+ F as routeSegmentToPath,
225
+ x as treeToRoutes,
226
+ _ as useParams
193
227
  };
@@ -0,0 +1,63 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("node:fs"),m=require("node:path"),x=require("./tree-hc1dSMdb.cjs");function w(n){const a=n.map(x.routeSegmentToPath).filter(Boolean);return a.length>0?`/${a.join("/")}`:"/"}function S(n){const a=[["pagesDir",n.pagesDir],["pageFile",n.pageFile],["layoutFile",n.layoutFile],["notFoundFile",n.notFoundFile]];for(const[t,e]of a)if(!e.trim())throw new Error(`nextViteRouter option "${t}" must not be empty.`);const s=a.slice(1),i=s.find(([,t],e)=>s.slice(e+1).some(([,r])=>r===t));if(i)throw new Error(`nextViteRouter file options must be distinct; "${i[0]}" is duplicated.`)}function T(n){return n.replace(/\\/g,"/").replace(/^\.\//,"").replace(/^\/|\/$/g,"")}function k(n,a,s){const i=a.pageFile??"page.tsx",t=new Map;for(const e of R(n,i,s)){const r=x.extractRoutePath(e,a),l=x.parseRouteSegments(r),d=l.filter(u=>u.kind==="dynamic"||u.kind==="catchAll").map(u=>({name:u.name,optional:u.kind==="catchAll"&&u.optional})),c=w(l);t.set(c,{path:c,params:d});const f=l[l.length-1];if((f==null?void 0:f.kind)==="catchAll"&&f.optional){const u=w(l.slice(0,-1));t.has(u)||t.set(u,{path:u,params:[]})}}return[...t.values()].sort((e,r)=>e.path.localeCompare(r.path))}function R(n,a,s){const i=[];function t(e){for(const r of b.readdirSync(e,{withFileTypes:!0})){const l=m.join(e,r.name);if(r.isDirectory())t(l);else if(r.isFile()&&r.name===a){const d=m.relative(n,l).replace(/\\/g,"/");i.push(`/${s}/${d}`)}}}return b.existsSync(n)&&t(n),i}function G(n){const a=n.map(({path:i})=>` | ${JSON.stringify(i)}`).join(`
2
+ `),s=n.map(({path:i,params:t})=>{const e=t.length>0?[...t.map(({name:r,optional:l})=>` ${r}${l?"?":""}: string;`)].join(`
3
+ `):" [key: string]: never;";return` ${JSON.stringify(i)}: {
4
+ ${e}
5
+ };`}).join(`
6
+ `);return`declare module 'virtual:next-vite-router' {
7
+ import type { ReactElement } from 'react';
8
+ import type { RouteObject } from 'react-router-dom';
9
+
10
+ export type RoutePath =
11
+ ${a||' | "/"'};
12
+
13
+ export type RouteParams<P extends RoutePath> = P extends keyof RoutePathParams
14
+ ? RoutePathParams[P]
15
+ : never;
16
+
17
+ export interface RoutePathParams {
18
+ ${s}
19
+ }
20
+
21
+ export const generateRoutes: () => RouteObject[];
22
+ export function Router(): ReactElement | null;
23
+ export function useParams<T extends Record<string, string | undefined> = Record<string, string | undefined>>(): T;
24
+ export { Link, useRoutes } from 'react-router-dom';
25
+ }
26
+ `}function O(n={}){const{pagesDir:a="src/app",pageFile:s="page.tsx",layoutFile:i="layout.tsx",notFoundFile:t="not-found.tsx"}=n;S({pagesDir:a,pageFile:s,layoutFile:i,notFoundFile:t});const e=T(a),r={pagesDir:e,pageFile:s,layoutFile:i,notFoundFile:t},l="virtual:next-vite-router",d="\0"+l;let c="",f="",u,h;function F(){const o=G(k(c,r,e));b.writeFileSync(f,o,"utf-8")}return{name:"next-vite-router",configResolved(o){c=m.resolve(o.root,a),f=m.join(m.dirname(c),"virtual-next-vite-router.d.ts"),x.assertNoRouteConflicts(R(c,s,e),R(c,t,e),r),F()},configureServer(o){if(u===o)return;h==null||h(),u=o;const g=$=>{if(m.relative(c,$).startsWith("..")||![s,i,t].includes(m.basename($)))return;try{x.assertNoRouteConflicts(R(c,s,e),R(c,t,e),r)}catch(P){o.config.logger.error(P instanceof Error?P.message:String(P));return}F();const v=o.moduleGraph.getModuleById(d);v&&(o.moduleGraph.invalidateModule(v),p&&clearTimeout(p),p=setTimeout(()=>{p=void 0,o.ws.send({type:"full-reload",path:"*"})},0))};let p;const y=()=>{p&&(clearTimeout(p),p=void 0),o.watcher.off("add",g),o.watcher.off("unlink",g),o.watcher.off("close",y),u===o&&(u=void 0,h=void 0)};o.watcher.on("add",g),o.watcher.on("unlink",g),o.watcher.once("close",y),h=y},resolveId(o){if(o===l)return d},load(o){if(o===d){const g=`/${e}/**/${s}`,p=`/${e}/**/${i}`,y=`/${e}/**/${t}`;return`
27
+ import { buildRouteTree, treeToRoutes, normalizeSegment, createLazyElement, useParams } from 'next-vite-router';
28
+ import { Link, useRoutes } from 'react-router-dom';
29
+
30
+ const pagesGlob = import.meta.glob(${JSON.stringify(g)});
31
+ const layoutsGlob = import.meta.glob(${JSON.stringify(p)}, { eager: true });
32
+ const notFoundsGlob = import.meta.glob(${JSON.stringify(y)});
33
+
34
+ // Keep the raw root-relative glob keys. extractRoutePath (via buildRouteTree)
35
+ // is the single place that strips the pagesDir prefix; stripping it here as
36
+ // well would double-remove it and let the prefix regex match a same-named
37
+ // nested folder (e.g. pagesDir "app" turning "app/docs/app" into "/").
38
+ const pages = pagesGlob;
39
+ const layouts = layoutsGlob;
40
+ const notFounds = notFoundsGlob;
41
+
42
+ export function generateRoutes() {
43
+ const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(r)});
44
+ const routes = treeToRoutes(tree, true);
45
+ return routes;
46
+ }
47
+
48
+ export function Router() {
49
+ routes ??= generateRoutes();
50
+ return useRoutes(routes);
51
+ }
52
+
53
+ let routes;
54
+
55
+ if (import.meta.hot) {
56
+ import.meta.hot.accept(() => {
57
+ routes = undefined;
58
+ });
59
+ }
60
+
61
+ export { Link, useRoutes };
62
+ export { useParams };
63
+ `}}}}exports.nextViteRouter=O;
@@ -0,0 +1,12 @@
1
+ import { Plugin as Plugin_2 } from 'vite';
2
+
3
+ export declare function nextViteRouter(options?: NextViteRouterPluginOptions): Plugin_2;
4
+
5
+ export declare type NextViteRouterPluginOptions = {
6
+ pagesDir?: string;
7
+ pageFile?: string;
8
+ layoutFile?: string;
9
+ notFoundFile?: string;
10
+ };
11
+
12
+ export { }