next-vite-router 1.2.0 → 1.2.1
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/README.md +3 -2
- package/dist/cli.cjs +1 -1
- package/dist/index.js +82 -70
- package/dist/plugin.js +59 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -238,12 +238,13 @@ replace React Router.
|
|
|
238
238
|
For an interactive setup:
|
|
239
239
|
|
|
240
240
|
```bash
|
|
241
|
-
npx
|
|
241
|
+
npx next-vite-router init
|
|
242
242
|
```
|
|
243
243
|
|
|
244
244
|
The CLI creates a Vite React project, installs the package and
|
|
245
245
|
`react-router-dom@^7.0.0`, configures the plugin, and adds an example route
|
|
246
|
-
tree.
|
|
246
|
+
tree. The CLI is published as part of `next-vite-router`; `create-next-vite-router`
|
|
247
|
+
is its executable name, not a separate npm package.
|
|
247
248
|
|
|
248
249
|
## Compatibility
|
|
249
250
|
|
package/dist/cli.cjs
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { e as z, n as
|
|
2
|
-
import i, { Suspense as
|
|
3
|
-
import { useParams as
|
|
4
|
-
const
|
|
1
|
+
import { e as z, n as g } from "./path-BodMUTCM.js";
|
|
2
|
+
import i, { Suspense as C, lazy as x, createContext as L, useContext as M } from "react";
|
|
3
|
+
import { useParams as A } from "react-router-dom";
|
|
4
|
+
const k = {
|
|
5
5
|
enableLazyLoading: !0
|
|
6
6
|
};
|
|
7
|
-
let
|
|
7
|
+
let d = { ...k };
|
|
8
8
|
function B(e) {
|
|
9
|
-
|
|
9
|
+
d = { ...d, ...e };
|
|
10
10
|
}
|
|
11
11
|
function S() {
|
|
12
|
-
return
|
|
12
|
+
return d;
|
|
13
13
|
}
|
|
14
|
-
let
|
|
14
|
+
let E = [];
|
|
15
15
|
function H(e, t) {
|
|
16
|
-
|
|
16
|
+
E.push({ pattern: e, middleware: t });
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
18
|
+
function J() {
|
|
19
|
+
E = [];
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function p(e, t) {
|
|
22
22
|
let n = t;
|
|
23
|
-
for (const r of
|
|
24
|
-
r.pattern.test(e) && (n = r.middleware(n));
|
|
23
|
+
for (const r of E)
|
|
24
|
+
r.pattern.lastIndex = 0, r.pattern.test(e) && (n = r.middleware(n)), r.pattern.lastIndex = 0;
|
|
25
25
|
return n;
|
|
26
26
|
}
|
|
27
|
-
const T = () => i.createElement("div"),
|
|
28
|
-
function
|
|
27
|
+
const T = () => i.createElement("div"), y = /* @__PURE__ */ new Map();
|
|
28
|
+
function R(e, t, n) {
|
|
29
29
|
const r = S();
|
|
30
30
|
if (r.enableLazyLoading === !1) {
|
|
31
31
|
const u = i.lazy(e), s = i.createElement(
|
|
32
|
-
|
|
32
|
+
C,
|
|
33
33
|
{ fallback: null },
|
|
34
34
|
// No loading fallback when disabled
|
|
35
35
|
i.createElement(u)
|
|
36
36
|
);
|
|
37
|
-
return n ?
|
|
37
|
+
return n ? p(n, s) : s;
|
|
38
38
|
}
|
|
39
|
-
let a =
|
|
40
|
-
a || (a =
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
{ fallback: i.createElement(
|
|
39
|
+
let a = y.get(t);
|
|
40
|
+
a || (a = x(e), y.set(t, a));
|
|
41
|
+
const l = r.loadingFallback || T, o = i.createElement(
|
|
42
|
+
C,
|
|
43
|
+
{ fallback: i.createElement(l) },
|
|
44
44
|
i.createElement(a)
|
|
45
45
|
);
|
|
46
|
-
return n ?
|
|
46
|
+
return n ? p(n, o) : o;
|
|
47
47
|
}
|
|
48
|
-
function
|
|
49
|
-
const
|
|
48
|
+
function m(e, t, n, r, a) {
|
|
49
|
+
const l = z(t, a), o = l ? l.split("/") : [];
|
|
50
50
|
let u = e, s = "";
|
|
51
|
-
|
|
51
|
+
o.forEach((c, h) => {
|
|
52
52
|
s = s ? `${s}/${c}` : c, u.children.has(c) || u.children.set(c, {
|
|
53
53
|
segment: c,
|
|
54
54
|
fullPath: s,
|
|
55
55
|
children: /* @__PURE__ */ new Map()
|
|
56
|
-
}), u = u.children.get(c),
|
|
57
|
-
}),
|
|
56
|
+
}), u = u.children.get(c), h === o.length - 1 && (u[r] = n);
|
|
57
|
+
}), o.length === 0 && (e[r] = n);
|
|
58
58
|
}
|
|
59
59
|
function j(e, t, n, r) {
|
|
60
60
|
const a = {
|
|
@@ -62,15 +62,15 @@ function j(e, t, n, r) {
|
|
|
62
62
|
fullPath: "",
|
|
63
63
|
children: /* @__PURE__ */ new Map()
|
|
64
64
|
};
|
|
65
|
-
return Object.entries(t).forEach(([
|
|
66
|
-
|
|
67
|
-
}), Object.entries(e).forEach(([
|
|
68
|
-
|
|
69
|
-
}), Object.entries(n).forEach(([
|
|
70
|
-
|
|
65
|
+
return Object.entries(t).forEach(([l, o]) => {
|
|
66
|
+
m(a, l, o.default, "layout", r);
|
|
67
|
+
}), Object.entries(e).forEach(([l, o]) => {
|
|
68
|
+
m(a, l, o, "page", r);
|
|
69
|
+
}), Object.entries(n).forEach(([l, o]) => {
|
|
70
|
+
m(a, l, o, "notFound", r);
|
|
71
71
|
}), a;
|
|
72
72
|
}
|
|
73
|
-
const F =
|
|
73
|
+
const F = L(void 0);
|
|
74
74
|
function N({
|
|
75
75
|
name: e,
|
|
76
76
|
children: t
|
|
@@ -82,54 +82,57 @@ function N({
|
|
|
82
82
|
);
|
|
83
83
|
}
|
|
84
84
|
function D() {
|
|
85
|
-
return
|
|
85
|
+
return M(F);
|
|
86
86
|
}
|
|
87
87
|
function P(e, t) {
|
|
88
|
-
return
|
|
88
|
+
return p(e, i.createElement(t));
|
|
89
|
+
}
|
|
90
|
+
function I(e) {
|
|
91
|
+
return /^\[\[\.\.\..+\]\]$/.test(e);
|
|
89
92
|
}
|
|
90
93
|
function f(e, t, n = !1) {
|
|
91
94
|
const r = e.page;
|
|
92
95
|
if (!r)
|
|
93
96
|
throw new Error(`Route node "${e.fullPath}" has no page loader`);
|
|
94
|
-
const a =
|
|
97
|
+
const a = R(
|
|
95
98
|
async () => ({ default: (await r()).default }),
|
|
96
99
|
`page:${e.fullPath || t}`,
|
|
97
100
|
t
|
|
98
|
-
),
|
|
99
|
-
name:
|
|
101
|
+
), l = e.segment.match(/^\[\[?\.\.\.(.+?)\]\]?$/), o = l ? i.createElement(N, {
|
|
102
|
+
name: l[1],
|
|
100
103
|
children: a
|
|
101
104
|
}) : a;
|
|
102
|
-
return n ? { index: !0, element:
|
|
105
|
+
return n ? { index: !0, element: o } : { path: g(e.segment), element: o };
|
|
103
106
|
}
|
|
104
|
-
function
|
|
107
|
+
function $(e) {
|
|
105
108
|
const t = e.notFound;
|
|
106
109
|
if (!t)
|
|
107
110
|
throw new Error(`Route node "${e.fullPath}" has no not-found loader`);
|
|
108
111
|
const n = e.fullPath ? `/${e.fullPath}` : "/";
|
|
109
112
|
return {
|
|
110
113
|
path: "*",
|
|
111
|
-
element:
|
|
114
|
+
element: R(
|
|
112
115
|
async () => ({ default: (await t()).default }),
|
|
113
116
|
`notfound:${e.fullPath}`,
|
|
114
117
|
n
|
|
115
118
|
)
|
|
116
119
|
};
|
|
117
120
|
}
|
|
118
|
-
function
|
|
121
|
+
function b(e, t) {
|
|
119
122
|
const n = [];
|
|
120
123
|
return [...e.children.entries()].sort(([r], [a]) => r.localeCompare(a)).forEach(([r, a]) => {
|
|
121
|
-
const
|
|
122
|
-
n.push(...
|
|
124
|
+
const l = g(r), o = l ? t ? `${t}/${l}` : `/${l}` : t;
|
|
125
|
+
n.push(...v(a, !1, o));
|
|
123
126
|
}), n;
|
|
124
127
|
}
|
|
125
|
-
function
|
|
128
|
+
function w(e, t) {
|
|
126
129
|
const n = [];
|
|
127
|
-
return e.page && n.push(f(e, t || "/", !0)), n.push(
|
|
130
|
+
return e.page && n.push(f(e, t || "/", !0)), n.push(...b(e, t)), e.notFound && n.push($(e)), n;
|
|
128
131
|
}
|
|
129
|
-
function
|
|
132
|
+
function v(e, t = !1, n = "") {
|
|
130
133
|
const r = [];
|
|
131
134
|
if (t) {
|
|
132
|
-
const c =
|
|
135
|
+
const c = w(e, "");
|
|
133
136
|
return e.layout ? (r.push({
|
|
134
137
|
path: "/",
|
|
135
138
|
element: P("/", e.layout),
|
|
@@ -143,54 +146,63 @@ function b(e, t = !1, n = "") {
|
|
|
143
146
|
children: c
|
|
144
147
|
}), r) : c;
|
|
145
148
|
}
|
|
146
|
-
const a =
|
|
149
|
+
const a = g(e.segment), l = !a, o = l ? n : n + "/" + a, u = b(e, o), s = e.notFound ? [$(e)] : [];
|
|
147
150
|
if (e.layout) {
|
|
148
|
-
const c =
|
|
151
|
+
const c = w(e, o);
|
|
149
152
|
return [{
|
|
150
|
-
...
|
|
151
|
-
element: P(
|
|
153
|
+
...l ? {} : { path: a },
|
|
154
|
+
element: P(o, e.layout),
|
|
152
155
|
children: c.length > 0 ? c : void 0
|
|
153
156
|
}];
|
|
154
157
|
}
|
|
155
|
-
if (e.page && !
|
|
158
|
+
if (e.page && !l) {
|
|
159
|
+
if (I(e.segment)) {
|
|
160
|
+
const c = f(e, n, !0), h = {
|
|
161
|
+
path: a,
|
|
162
|
+
element: f(e, o).element,
|
|
163
|
+
children: [...u, ...s].length > 0 ? [...u, ...s] : void 0
|
|
164
|
+
};
|
|
165
|
+
return [c, h];
|
|
166
|
+
}
|
|
156
167
|
return [{
|
|
157
168
|
path: a,
|
|
158
|
-
element: f(e,
|
|
169
|
+
element: f(e, o).element,
|
|
159
170
|
children: [...u, ...s].length > 0 ? [...u, ...s] : void 0
|
|
160
171
|
}];
|
|
161
|
-
|
|
172
|
+
}
|
|
173
|
+
if (e.page || e.notFound || !l) {
|
|
162
174
|
const c = [
|
|
163
|
-
...e.page ? [f(e,
|
|
175
|
+
...e.page ? [f(e, o, !0)] : [],
|
|
164
176
|
...u,
|
|
165
177
|
...s
|
|
166
178
|
];
|
|
167
179
|
return [{
|
|
168
|
-
...
|
|
180
|
+
...l ? {} : { path: a },
|
|
169
181
|
children: c.length > 0 ? c : void 0
|
|
170
182
|
}];
|
|
171
183
|
}
|
|
172
184
|
return u;
|
|
173
185
|
}
|
|
174
|
-
function
|
|
186
|
+
function Q(e, t, n, r) {
|
|
175
187
|
return () => {
|
|
176
188
|
const a = j(e, t, n, r);
|
|
177
|
-
return
|
|
189
|
+
return v(a, !0);
|
|
178
190
|
};
|
|
179
191
|
}
|
|
180
|
-
function
|
|
181
|
-
const e =
|
|
192
|
+
function U() {
|
|
193
|
+
const e = A(), t = D();
|
|
182
194
|
return typeof t == "string" && e["*"] !== void 0 ? { ...e, [t]: e["*"] } : e;
|
|
183
195
|
}
|
|
184
196
|
export {
|
|
185
197
|
N as CatchAllParamProvider,
|
|
186
198
|
j as buildRouteTree,
|
|
187
|
-
|
|
199
|
+
J as clearMiddlewares,
|
|
188
200
|
B as configureRouter,
|
|
189
|
-
|
|
190
|
-
|
|
201
|
+
R as createLazyElement,
|
|
202
|
+
Q as createRouteGenerator,
|
|
191
203
|
z as extractRoutePath,
|
|
192
|
-
|
|
204
|
+
g as normalizeSegment,
|
|
193
205
|
H as registerMiddleware,
|
|
194
|
-
|
|
195
|
-
|
|
206
|
+
v as treeToRoutes,
|
|
207
|
+
U as useParams
|
|
196
208
|
};
|
package/dist/plugin.js
CHANGED
|
@@ -1,31 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { n as
|
|
4
|
-
function
|
|
5
|
-
const e =
|
|
1
|
+
import d from "node:fs";
|
|
2
|
+
import u from "node:path";
|
|
3
|
+
import { n as R } from "./path-BodMUTCM.js";
|
|
4
|
+
function g(r) {
|
|
5
|
+
const e = r.split(u.sep).filter(Boolean).map(R).filter(Boolean);
|
|
6
6
|
return e.length > 0 ? `/${e.join("/")}` : "/";
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
function
|
|
11
|
-
for (const
|
|
12
|
-
const
|
|
13
|
-
|
|
8
|
+
function v(r, e) {
|
|
9
|
+
const i = /* @__PURE__ */ new Map();
|
|
10
|
+
function a(n) {
|
|
11
|
+
for (const s of d.readdirSync(n, { withFileTypes: !0 })) {
|
|
12
|
+
const m = u.join(n, s.name);
|
|
13
|
+
if (s.isDirectory())
|
|
14
|
+
a(m);
|
|
15
|
+
else if (s.isFile() && s.name === e) {
|
|
16
|
+
const o = u.relative(r, n), f = o.split(u.sep).filter(Boolean).map((c) => {
|
|
17
|
+
const t = c.match(/^\[\[\.\.\.(.+)\]\]$/);
|
|
18
|
+
if (t)
|
|
19
|
+
return { name: t[1], optional: !0 };
|
|
20
|
+
const p = c.match(/^\[\.\.\.(.+)\]$/);
|
|
21
|
+
return p ? { name: p[1], optional: !1 } : void 0;
|
|
22
|
+
}).filter((c) => !!c);
|
|
23
|
+
i.set(g(o), {
|
|
24
|
+
path: g(o),
|
|
25
|
+
params: f
|
|
26
|
+
});
|
|
27
|
+
}
|
|
14
28
|
}
|
|
15
29
|
}
|
|
16
|
-
return
|
|
30
|
+
return d.existsSync(r) && a(r), [...i.values()].sort((n, s) => n.path.localeCompare(s.path));
|
|
17
31
|
}
|
|
18
|
-
function
|
|
19
|
-
const e =
|
|
32
|
+
function x(r) {
|
|
33
|
+
const e = r.match(/^\/(?:\[\.\.\.|:)([^/\]]+)\]?$/);
|
|
20
34
|
return e == null ? void 0 : e[1];
|
|
21
35
|
}
|
|
22
|
-
function P(
|
|
23
|
-
const e =
|
|
24
|
-
`),
|
|
25
|
-
const
|
|
36
|
+
function P(r) {
|
|
37
|
+
const e = r.map(({ path: a }) => ` | "${a}"`).join(`
|
|
38
|
+
`), i = r.map(({ path: a, params: n }) => {
|
|
39
|
+
const s = a.split("/").map((o) => x(`/${o}`)).filter((o) => !!o), m = s.length > 0 || n.length > 0 ? [
|
|
40
|
+
...s.map((o) => ` ${o}: string;`),
|
|
41
|
+
...n.map(({ name: o, optional: l }) => ` ${o}${l ? "?" : ""}: string;`)
|
|
42
|
+
].join(`
|
|
26
43
|
`) : " [key: string]: never;";
|
|
27
|
-
return ` "${
|
|
28
|
-
${
|
|
44
|
+
return ` "${a}": {
|
|
45
|
+
${m}
|
|
29
46
|
};`;
|
|
30
47
|
}).join(`
|
|
31
48
|
`);
|
|
@@ -41,7 +58,7 @@ ${e || ' | "/"'};
|
|
|
41
58
|
: never;
|
|
42
59
|
|
|
43
60
|
export interface RoutePathParams {
|
|
44
|
-
${
|
|
61
|
+
${i}
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
export const generateRoutes: () => RouteObject[];
|
|
@@ -51,45 +68,45 @@ ${s}
|
|
|
51
68
|
}
|
|
52
69
|
`;
|
|
53
70
|
}
|
|
54
|
-
function k(
|
|
71
|
+
function k(r = {}) {
|
|
55
72
|
const {
|
|
56
73
|
pagesDir: e = "src/app",
|
|
57
|
-
pageFile:
|
|
58
|
-
layoutFile:
|
|
74
|
+
pageFile: i = "page.tsx",
|
|
75
|
+
layoutFile: a = "layout.tsx",
|
|
59
76
|
notFoundFile: n = "not-found.tsx"
|
|
60
|
-
} =
|
|
61
|
-
let l = "",
|
|
62
|
-
function
|
|
63
|
-
const t = P(
|
|
64
|
-
|
|
77
|
+
} = r, s = { pagesDir: e, pageFile: i, layoutFile: a, notFoundFile: n }, m = "virtual:next-vite-router", o = "\0" + m;
|
|
78
|
+
let l = "", f = "";
|
|
79
|
+
function c() {
|
|
80
|
+
const t = P(v(l, i));
|
|
81
|
+
d.writeFileSync(f, t, "utf-8");
|
|
65
82
|
}
|
|
66
83
|
return {
|
|
67
84
|
name: "next-vite-router",
|
|
68
85
|
configResolved(t) {
|
|
69
|
-
l =
|
|
86
|
+
l = u.resolve(t.root, e), f = u.join(u.dirname(l), "virtual-next-vite-router.d.ts"), c();
|
|
70
87
|
},
|
|
71
88
|
configureServer(t) {
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
89
|
+
const p = (h) => {
|
|
90
|
+
if (u.relative(l, h).startsWith("..") || ![i, a, n].includes(u.basename(h)))
|
|
74
91
|
return;
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
92
|
+
c();
|
|
93
|
+
const y = t.moduleGraph.getModuleById(o);
|
|
94
|
+
y && (t.moduleGraph.invalidateModule(y), t.ws.send({ type: "full-reload", path: "*" }));
|
|
78
95
|
};
|
|
79
|
-
t.watcher.on("add",
|
|
96
|
+
t.watcher.on("add", p), t.watcher.on("unlink", p);
|
|
80
97
|
},
|
|
81
98
|
resolveId(t) {
|
|
82
|
-
if (t ===
|
|
83
|
-
return
|
|
99
|
+
if (t === m)
|
|
100
|
+
return o;
|
|
84
101
|
},
|
|
85
102
|
load(t) {
|
|
86
|
-
if (t ===
|
|
103
|
+
if (t === o)
|
|
87
104
|
return `
|
|
88
105
|
import { buildRouteTree, treeToRoutes, normalizeSegment, createLazyElement, useParams } from 'next-vite-router';
|
|
89
106
|
import { Link, useRoutes } from 'react-router-dom';
|
|
90
107
|
|
|
91
|
-
const pagesGlob = import.meta.glob('/${e}/**/${
|
|
92
|
-
const layoutsGlob = import.meta.glob('/${e}/**/${
|
|
108
|
+
const pagesGlob = import.meta.glob('/${e}/**/${i}');
|
|
109
|
+
const layoutsGlob = import.meta.glob('/${e}/**/${a}', { eager: true });
|
|
93
110
|
const notFoundsGlob = import.meta.glob('/${e}/**/${n}');
|
|
94
111
|
|
|
95
112
|
const transformPath = (path) => path.replace('/${e}/', '');
|
|
@@ -105,7 +122,7 @@ const notFounds = Object.fromEntries(
|
|
|
105
122
|
);
|
|
106
123
|
|
|
107
124
|
export function generateRoutes() {
|
|
108
|
-
const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(
|
|
125
|
+
const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(s)});
|
|
109
126
|
const routes = treeToRoutes(tree, true);
|
|
110
127
|
return routes;
|
|
111
128
|
}
|