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 CHANGED
@@ -238,12 +238,13 @@ replace React Router.
238
238
  For an interactive setup:
239
239
 
240
240
  ```bash
241
- npx create-next-vite-router init
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
@@ -544,7 +544,7 @@ const init = new commander.Command().name("init").description(
544
544
  Project "${projectName}" is ready!
545
545
  `);
546
546
  });
547
- const version = "1.2.0";
547
+ const version = "1.2.1";
548
548
  const pkg = {
549
549
  version
550
550
  };
package/dist/index.js CHANGED
@@ -1,60 +1,60 @@
1
- import { e as z, n as p } from "./path-BodMUTCM.js";
2
- import i, { Suspense as E, lazy as L, createContext as M, useContext as x } from "react";
3
- import { useParams as k } from "react-router-dom";
4
- const A = {
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 m = { ...A };
7
+ let d = { ...k };
8
8
  function B(e) {
9
- m = { ...m, ...e };
9
+ d = { ...d, ...e };
10
10
  }
11
11
  function S() {
12
- return m;
12
+ return d;
13
13
  }
14
- let g = [];
14
+ let E = [];
15
15
  function H(e, t) {
16
- g.push({ pattern: e, middleware: t });
16
+ E.push({ pattern: e, middleware: t });
17
17
  }
18
- function I() {
19
- g = [];
18
+ function J() {
19
+ E = [];
20
20
  }
21
- function d(e, t) {
21
+ function p(e, t) {
22
22
  let n = t;
23
- for (const r of g)
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"), C = /* @__PURE__ */ new Map();
28
- function w(e, t, n) {
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
- E,
32
+ C,
33
33
  { fallback: null },
34
34
  // No loading fallback when disabled
35
35
  i.createElement(u)
36
36
  );
37
- return n ? d(n, s) : s;
37
+ return n ? p(n, s) : s;
38
38
  }
39
- let a = C.get(t);
40
- a || (a = L(e), C.set(t, a));
41
- const o = r.loadingFallback || T, l = i.createElement(
42
- E,
43
- { fallback: i.createElement(o) },
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 ? d(n, l) : l;
46
+ return n ? p(n, o) : o;
47
47
  }
48
- function h(e, t, n, r, a) {
49
- const o = z(t, a), l = o ? o.split("/") : [];
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
- l.forEach((c, v) => {
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), v === l.length - 1 && (u[r] = n);
57
- }), l.length === 0 && (e[r] = n);
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(([o, l]) => {
66
- h(a, o, l.default, "layout", r);
67
- }), Object.entries(e).forEach(([o, l]) => {
68
- h(a, o, l, "page", r);
69
- }), Object.entries(n).forEach(([o, l]) => {
70
- h(a, o, l, "notFound", r);
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 = M(void 0);
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 x(F);
85
+ return M(F);
86
86
  }
87
87
  function P(e, t) {
88
- return d(e, i.createElement(t));
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 = w(
97
+ const a = R(
95
98
  async () => ({ default: (await r()).default }),
96
99
  `page:${e.fullPath || t}`,
97
100
  t
98
- ), o = e.segment.match(/^\[\[?\.\.\.(.+?)\]\]?$/), l = o ? i.createElement(N, {
99
- name: o[1],
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: l } : { path: p(e.segment), element: l };
105
+ return n ? { index: !0, element: o } : { path: g(e.segment), element: o };
103
106
  }
104
- function R(e) {
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: w(
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 $(e, t) {
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 o = p(r), l = o ? t ? `${t}/${o}` : `/${o}` : t;
122
- n.push(...b(a, !1, l));
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 y(e, t) {
128
+ function w(e, t) {
126
129
  const n = [];
127
- return e.page && n.push(f(e, t || "/", !0)), n.push(...$(e, t)), e.notFound && n.push(R(e)), n;
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 b(e, t = !1, n = "") {
132
+ function v(e, t = !1, n = "") {
130
133
  const r = [];
131
134
  if (t) {
132
- const c = y(e, "");
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 = p(e.segment), o = !a, l = o ? n : n + "/" + a, u = $(e, l), s = e.notFound ? [R(e)] : [];
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 = y(e, l);
151
+ const c = w(e, o);
149
152
  return [{
150
- ...o ? {} : { path: a },
151
- element: P(l, e.layout),
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 && !o)
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, l).element,
169
+ element: f(e, o).element,
159
170
  children: [...u, ...s].length > 0 ? [...u, ...s] : void 0
160
171
  }];
161
- if (e.page || e.notFound || !o) {
172
+ }
173
+ if (e.page || e.notFound || !l) {
162
174
  const c = [
163
- ...e.page ? [f(e, l, !0)] : [],
175
+ ...e.page ? [f(e, o, !0)] : [],
164
176
  ...u,
165
177
  ...s
166
178
  ];
167
179
  return [{
168
- ...o ? {} : { path: a },
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 J(e, t, n, r) {
186
+ function Q(e, t, n, r) {
175
187
  return () => {
176
188
  const a = j(e, t, n, r);
177
- return b(a, !0);
189
+ return v(a, !0);
178
190
  };
179
191
  }
180
- function Q() {
181
- const e = k(), t = D();
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
- I as clearMiddlewares,
199
+ J as clearMiddlewares,
188
200
  B as configureRouter,
189
- w as createLazyElement,
190
- J as createRouteGenerator,
201
+ R as createLazyElement,
202
+ Q as createRouteGenerator,
191
203
  z as extractRoutePath,
192
- p as normalizeSegment,
204
+ g as normalizeSegment,
193
205
  H as registerMiddleware,
194
- b as treeToRoutes,
195
- Q as useParams
206
+ v as treeToRoutes,
207
+ U as useParams
196
208
  };
package/dist/plugin.js CHANGED
@@ -1,31 +1,48 @@
1
- import m from "node:fs";
2
- import i from "node:path";
3
- import { n as g } from "./path-BodMUTCM.js";
4
- function h(o) {
5
- const e = o.split(i.sep).filter(Boolean).map(g).filter(Boolean);
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 x(o, e) {
9
- const s = /* @__PURE__ */ new Set();
10
- function r(n) {
11
- for (const u of m.readdirSync(n, { withFileTypes: !0 })) {
12
- const a = i.join(n, u.name);
13
- u.isDirectory() ? r(a) : u.isFile() && u.name === e && s.add(h(i.relative(o, n)));
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 m.existsSync(o) && r(o), [...s].sort();
30
+ return d.existsSync(r) && a(r), [...i.values()].sort((n, s) => n.path.localeCompare(s.path));
17
31
  }
18
- function v(o) {
19
- const e = o.match(/^\/(?:\[\.\.\.|:)([^/\]]+)\]?$/);
32
+ function x(r) {
33
+ const e = r.match(/^\/(?:\[\.\.\.|:)([^/\]]+)\]?$/);
20
34
  return e == null ? void 0 : e[1];
21
35
  }
22
- function P(o) {
23
- const e = o.map((r) => ` | "${r}"`).join(`
24
- `), s = o.map((r) => {
25
- const n = r.split("/").map((a) => v(`/${a}`)).filter((a) => !!a), u = n.length > 0 ? n.map((a) => ` ${a}: string;`).join(`
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 ` "${r}": {
28
- ${u}
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
- ${s}
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(o = {}) {
71
+ function k(r = {}) {
55
72
  const {
56
73
  pagesDir: e = "src/app",
57
- pageFile: s = "page.tsx",
58
- layoutFile: r = "layout.tsx",
74
+ pageFile: i = "page.tsx",
75
+ layoutFile: a = "layout.tsx",
59
76
  notFoundFile: n = "not-found.tsx"
60
- } = o, u = { pagesDir: e, pageFile: s, layoutFile: r, notFoundFile: n }, a = "virtual:next-vite-router", c = "\0" + a;
61
- let l = "", p = "";
62
- function d() {
63
- const t = P(x(l, s));
64
- m.writeFileSync(p, t, "utf-8");
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 = i.resolve(t.root, e), p = i.join(i.dirname(l), "virtual-next-vite-router.d.ts"), d();
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 f = (y) => {
73
- if (i.relative(l, y).startsWith("..") || ![s, r, n].includes(i.basename(y)))
89
+ const p = (h) => {
90
+ if (u.relative(l, h).startsWith("..") || ![i, a, n].includes(u.basename(h)))
74
91
  return;
75
- d();
76
- const R = t.moduleGraph.getModuleById(c);
77
- R && (t.moduleGraph.invalidateModule(R), t.ws.send({ type: "full-reload", path: "*" }));
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", f), t.watcher.on("unlink", f);
96
+ t.watcher.on("add", p), t.watcher.on("unlink", p);
80
97
  },
81
98
  resolveId(t) {
82
- if (t === a)
83
- return c;
99
+ if (t === m)
100
+ return o;
84
101
  },
85
102
  load(t) {
86
- if (t === c)
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}/**/${s}');
92
- const layoutsGlob = import.meta.glob('/${e}/**/${r}', { eager: true });
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(u)});
125
+ const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(s)});
109
126
  const routes = treeToRoutes(tree, true);
110
127
  return routes;
111
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-vite-router",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Next.js-like file-based routing for React + Vite",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",