next-vite-router 1.2.0 → 1.2.2

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
@@ -408,6 +408,10 @@ function startViteConfig(projectDir, src) {
408
408
  if (!viteConfigContent) throw new Error("Vite config empty");
409
409
  if (!viteConfigContent.includes("import { resolve }")) {
410
410
  viteConfigContent = `import { resolve } from "path";
411
+ ` + viteConfigContent;
412
+ }
413
+ if (!viteConfigContent.includes("import { fileURLToPath }")) {
414
+ viteConfigContent = `import { fileURLToPath } from "node:url";
411
415
  ` + viteConfigContent;
412
416
  }
413
417
  if (!viteConfigContent.includes("import { nextViteRouter }")) {
@@ -416,7 +420,7 @@ function startViteConfig(projectDir, src) {
416
420
  }
417
421
  const resolveConfig = ` resolve: {
418
422
  alias: {
419
- "@": resolve(__dirname, "./${src ? "src" : "."}"),
423
+ "@": resolve(fileURLToPath(new URL("./${src ? "src" : "."}", import.meta.url))),
420
424
  },
421
425
  },`;
422
426
  const defineConfigRegex = /defineConfig\(\s*{/;
@@ -430,7 +434,7 @@ ${resolveConfig}
430
434
  }
431
435
  const pluginsRegex = /plugins\s*:\s*\[(.*?)\]/s;
432
436
  const match = viteConfigContent.match(pluginsRegex);
433
- const pluginString = `nextViteRouter({ pagesDir: "${src ? "src/app" : "app"}", pageFile: "page.tsx", layoutFile: "layout.tsx", notFoundFile: "not-found.tsx" })`;
437
+ const pluginString = "nextViteRouter()";
434
438
  if (match) {
435
439
  const existing = match[1].trim();
436
440
  const newPlugins = existing ? `${existing}, ${pluginString}` : pluginString;
@@ -544,7 +548,7 @@ const init = new commander.Command().name("init").description(
544
548
  Project "${projectName}" is ready!
545
549
  `);
546
550
  });
547
- const version = "1.2.0";
551
+ const version = "1.2.2";
548
552
  const pkg = {
549
553
  version
550
554
  };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,8 @@ import { default as default_2 } from 'react';
3
3
  import { ReactElement } from 'react';
4
4
  import { RouteObject } from 'react-router-dom';
5
5
 
6
+ export declare function assertNoDuplicateRoutes(filePaths: string[], fileOptions?: RouteFileOptions): void;
7
+
6
8
  export declare function buildRouteTree(pages: Record<string, () => Promise<any>>, layouts: Record<string, any>, notFounds: Record<string, () => Promise<any>>, fileOptions?: RouteFileOptions): RouteNode;
7
9
 
8
10
  export declare function CatchAllParamProvider({ name, children, }: {
package/dist/index.js CHANGED
@@ -1,196 +1,193 @@
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 { 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 = {
5
6
  enableLazyLoading: !0
6
7
  };
7
- let m = { ...A };
8
- function B(e) {
8
+ let m = { ...M };
9
+ function j(e) {
9
10
  m = { ...m, ...e };
10
11
  }
11
- function S() {
12
+ function k() {
12
13
  return m;
13
14
  }
14
15
  let g = [];
15
- function H(e, t) {
16
+ function q(e, t) {
16
17
  g.push({ pattern: e, middleware: t });
17
18
  }
18
- function I() {
19
+ function B() {
19
20
  g = [];
20
21
  }
21
- function d(e, t) {
22
+ function h(e, t) {
22
23
  let n = t;
23
24
  for (const r of g)
24
- r.pattern.test(e) && (n = r.middleware(n));
25
+ r.pattern.lastIndex = 0, r.pattern.test(e) && (n = r.middleware(n)), r.pattern.lastIndex = 0;
25
26
  return n;
26
27
  }
27
- const T = () => i.createElement("div"), C = /* @__PURE__ */ new Map();
28
- function w(e, t, n) {
29
- const r = S();
28
+ const S = () => c.createElement("div"), C = /* @__PURE__ */ new Map();
29
+ function R(e, t, n) {
30
+ const r = k();
30
31
  if (r.enableLazyLoading === !1) {
31
- const u = i.lazy(e), s = i.createElement(
32
- E,
32
+ const s = c.lazy(e), i = c.createElement(
33
+ d,
33
34
  { fallback: null },
34
35
  // No loading fallback when disabled
35
- i.createElement(u)
36
+ c.createElement(s)
36
37
  );
37
- return n ? d(n, s) : s;
38
+ return n ? h(n, i) : i;
38
39
  }
39
40
  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) },
44
- i.createElement(a)
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)
45
46
  );
46
- return n ? d(n, l) : l;
47
- }
48
- function h(e, t, n, r, a) {
49
- const o = z(t, a), l = o ? o.split("/") : [];
50
- let u = e, s = "";
51
- l.forEach((c, v) => {
52
- s = s ? `${s}/${c}` : c, u.children.has(c) || u.children.set(c, {
53
- segment: c,
54
- fullPath: s,
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);
58
- }
59
- function j(e, t, n, r) {
60
- const a = {
61
- segment: "",
62
- fullPath: "",
63
- children: /* @__PURE__ */ new Map()
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);
71
- }), a;
72
- }
73
- const F = M(void 0);
74
- function N({
47
+ return n ? h(n, o) : o;
48
+ }
49
+ const w = v(void 0);
50
+ function D({
75
51
  name: e,
76
52
  children: t
77
53
  }) {
78
- return i.createElement(
79
- F.Provider,
54
+ return c.createElement(
55
+ w.Provider,
80
56
  { value: e },
81
57
  t
82
58
  );
83
59
  }
84
- function D() {
85
- return x(F);
60
+ function N() {
61
+ return L(w);
86
62
  }
87
- function P(e, t) {
88
- return d(e, i.createElement(t));
63
+ function y(e, t) {
64
+ return h(e, c.createElement(t));
65
+ }
66
+ function T(e) {
67
+ return /^\[\[\.\.\..+\]\]$/.test(e);
89
68
  }
90
69
  function f(e, t, n = !1) {
91
70
  const r = e.page;
92
71
  if (!r)
93
72
  throw new Error(`Route node "${e.fullPath}" has no page loader`);
94
- const a = w(
73
+ const a = R(
95
74
  async () => ({ default: (await r()).default }),
96
75
  `page:${e.fullPath || t}`,
97
76
  t
98
- ), o = e.segment.match(/^\[\[?\.\.\.(.+?)\]\]?$/), l = o ? i.createElement(N, {
99
- name: o[1],
77
+ ), l = e.segment.match(/^\[\[?\.\.\.(.+?)\]\]?$/), o = l ? c.createElement(D, {
78
+ name: l[1],
100
79
  children: a
101
80
  }) : a;
102
- return n ? { index: !0, element: l } : { path: p(e.segment), element: l };
81
+ return n ? { index: !0, element: o } : { path: p(e.segment), element: o };
103
82
  }
104
- function R(e) {
83
+ function P(e) {
105
84
  const t = e.notFound;
106
85
  if (!t)
107
86
  throw new Error(`Route node "${e.fullPath}" has no not-found loader`);
108
87
  const n = e.fullPath ? `/${e.fullPath}` : "/";
109
88
  return {
110
89
  path: "*",
111
- element: w(
90
+ element: R(
112
91
  async () => ({ default: (await t()).default }),
113
92
  `notfound:${e.fullPath}`,
114
93
  n
115
94
  )
116
95
  };
117
96
  }
118
- function $(e, t) {
97
+ function F(e, t) {
119
98
  const n = [];
120
99
  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));
100
+ const l = p(r), o = l ? t ? `${t}/${l}` : `/${l}` : t;
101
+ n.push(...$(a, !1, o));
123
102
  }), n;
124
103
  }
125
- function y(e, t) {
104
+ function E(e, t) {
126
105
  const n = [];
127
- return e.page && n.push(f(e, t || "/", !0)), n.push(...$(e, t)), e.notFound && n.push(R(e)), n;
106
+ return e.page && n.push(f(e, t || "/", !0)), n.push(...F(e, t)), e.notFound && n.push(P(e)), n;
128
107
  }
129
- function b(e, t = !1, n = "") {
108
+ function $(e, t = !1, n = "") {
130
109
  const r = [];
131
110
  if (t) {
132
- const c = y(e, "");
111
+ const u = E(e, "");
133
112
  return e.layout ? (r.push({
134
113
  path: "/",
135
- element: P("/", e.layout),
136
- children: c
137
- }), r) : e.page ? (r.push({
114
+ element: y("/", e.layout),
115
+ children: u
116
+ }), r) : e.page ? u.length > 1 ? [{
138
117
  path: "/",
139
- element: f(e, "/", !1).element,
140
- children: c.slice(1).length > 0 ? c.slice(1) : void 0
141
- }), r) : e.notFound ? (r.push({
118
+ children: u
119
+ }] : [{
142
120
  path: "/",
143
- children: c
144
- }), r) : c;
121
+ element: f(e, "/", !1).element
122
+ }] : e.notFound ? (r.push({
123
+ path: "/",
124
+ children: u
125
+ }), r) : u;
145
126
  }
146
- const a = p(e.segment), o = !a, l = o ? n : n + "/" + a, u = $(e, l), s = e.notFound ? [R(e)] : [];
127
+ const a = p(e.segment), l = !a, o = l ? n : n + "/" + a, s = F(e, o), i = e.notFound ? [P(e)] : [];
147
128
  if (e.layout) {
148
- const c = y(e, l);
129
+ const u = E(e, o);
149
130
  return [{
150
- ...o ? {} : { path: a },
151
- element: P(l, e.layout),
152
- children: c.length > 0 ? c : void 0
131
+ ...l ? {} : { path: a },
132
+ element: y(o, e.layout),
133
+ children: u.length > 0 ? u : void 0
153
134
  }];
154
135
  }
155
- if (e.page && !o)
156
- return [{
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 ? [{
157
146
  path: a,
158
- element: f(e, l).element,
159
- children: [...u, ...s].length > 0 ? [...u, ...s] : void 0
147
+ children: [
148
+ f(e, o, !0),
149
+ ...s,
150
+ ...i
151
+ ]
152
+ }] : [{
153
+ path: a,
154
+ element: f(e, o).element
160
155
  }];
161
- if (e.page || e.notFound || !o) {
162
- const c = [
163
- ...e.page ? [f(e, l, !0)] : [],
164
- ...u,
165
- ...s
156
+ }
157
+ if (e.page || e.notFound || !l) {
158
+ const u = [
159
+ ...e.page ? [f(e, o, !0)] : [],
160
+ ...s,
161
+ ...i
166
162
  ];
167
163
  return [{
168
- ...o ? {} : { path: a },
169
- children: c.length > 0 ? c : void 0
164
+ ...l ? {} : { path: a },
165
+ children: u.length > 0 ? u : void 0
170
166
  }];
171
167
  }
172
- return u;
168
+ return s;
173
169
  }
174
- function J(e, t, n, r) {
170
+ function H(e, t, n, r) {
175
171
  return () => {
176
- const a = j(e, t, n, r);
177
- return b(a, !0);
172
+ const a = z(e, t, n, r);
173
+ return $(a, !0);
178
174
  };
179
175
  }
180
- function Q() {
181
- const e = k(), t = D();
176
+ function J() {
177
+ const e = A(), t = N();
182
178
  return typeof t == "string" && e["*"] !== void 0 ? { ...e, [t]: e["*"] } : e;
183
179
  }
184
180
  export {
185
- N as CatchAllParamProvider,
186
- j as buildRouteTree,
187
- I as clearMiddlewares,
188
- B as configureRouter,
189
- w as createLazyElement,
190
- J as createRouteGenerator,
191
- z as extractRoutePath,
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,
192
189
  p as normalizeSegment,
193
- H as registerMiddleware,
194
- b as treeToRoutes,
195
- Q as useParams
190
+ q as registerMiddleware,
191
+ $ as treeToRoutes,
192
+ J as useParams
196
193
  };
package/dist/plugin.js CHANGED
@@ -1,31 +1,60 @@
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 h from "node:fs";
2
+ import l from "node:path";
3
+ import { a as v, n as b } from "./tree-HwBRWJNI.js";
4
+ function x(r) {
5
+ const e = r.split(l.sep).filter(Boolean).map(b).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 $(r, e) {
9
+ const n = /* @__PURE__ */ new Map();
10
+ function s(a) {
11
+ for (const t of h.readdirSync(a, { withFileTypes: !0 })) {
12
+ const c = l.join(a, t.name);
13
+ if (t.isDirectory())
14
+ s(c);
15
+ else if (t.isFile() && t.name === e) {
16
+ const i = l.relative(r, a), u = i.split(l.sep).filter(Boolean), y = u.map((p) => {
17
+ const m = p.match(/^\[\[\.\.\.(.+)\]\]$/);
18
+ if (m)
19
+ return { name: m[1], optional: !0 };
20
+ const d = p.match(/^\[\.\.\.(.+)\]$/);
21
+ return d ? { name: d[1], optional: !1 } : void 0;
22
+ }).filter((p) => !!p), f = x(i);
23
+ n.set(f, { path: f, params: y });
24
+ const o = u[u.length - 1];
25
+ if (o == null ? void 0 : o.match(/^\[\[\.\.\.(.+)\]\]$/)) {
26
+ const p = u.slice(0, -1).join(l.sep), m = x(p);
27
+ n.has(m) || n.set(m, { path: m, params: [] });
28
+ }
29
+ }
14
30
  }
15
31
  }
16
- return m.existsSync(o) && r(o), [...s].sort();
32
+ return h.existsSync(r) && s(r), [...n.values()].sort((a, t) => a.path.localeCompare(t.path));
17
33
  }
18
- function v(o) {
19
- const e = o.match(/^\/(?:\[\.\.\.|:)([^/\]]+)\]?$/);
34
+ function P(r, e) {
35
+ const n = [];
36
+ function s(a) {
37
+ for (const t of h.readdirSync(a, { withFileTypes: !0 })) {
38
+ const c = l.join(a, t.name);
39
+ t.isDirectory() ? s(c) : t.isFile() && t.name === e && n.push(l.relative(r, c));
40
+ }
41
+ }
42
+ return h.existsSync(r) && s(r), n;
43
+ }
44
+ function F(r) {
45
+ const e = r.match(/^\/(?:\[\.\.\.|:)([^/\]]+)\]?$/);
20
46
  return e == null ? void 0 : e[1];
21
47
  }
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(`
48
+ function j(r) {
49
+ const e = r.map(({ path: s }) => ` | "${s}"`).join(`
50
+ `), n = r.map(({ path: s, params: a }) => {
51
+ const t = s.split("/").map((i) => F(`/${i}`)).filter((i) => !!i), c = t.length > 0 || a.length > 0 ? [
52
+ ...t.map((i) => ` ${i}: string;`),
53
+ ...a.map(({ name: i, optional: u }) => ` ${i}${u ? "?" : ""}: string;`)
54
+ ].join(`
26
55
  `) : " [key: string]: never;";
27
- return ` "${r}": {
28
- ${u}
56
+ return ` "${s}": {
57
+ ${c}
29
58
  };`;
30
59
  }).join(`
31
60
  `);
@@ -41,7 +70,7 @@ ${e || ' | "/"'};
41
70
  : never;
42
71
 
43
72
  export interface RoutePathParams {
44
- ${s}
73
+ ${n}
45
74
  }
46
75
 
47
76
  export const generateRoutes: () => RouteObject[];
@@ -51,46 +80,54 @@ ${s}
51
80
  }
52
81
  `;
53
82
  }
54
- function k(o = {}) {
83
+ function S(r = {}) {
55
84
  const {
56
85
  pagesDir: e = "src/app",
57
- pageFile: s = "page.tsx",
58
- layoutFile: r = "layout.tsx",
59
- 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");
86
+ pageFile: n = "page.tsx",
87
+ layoutFile: s = "layout.tsx",
88
+ notFoundFile: a = "not-found.tsx"
89
+ } = r, t = { pagesDir: e, pageFile: n, layoutFile: s, notFoundFile: a }, c = "virtual:next-vite-router", i = "\0" + c;
90
+ let u = "", y = "";
91
+ function f() {
92
+ const o = j($(u, n));
93
+ h.writeFileSync(y, o, "utf-8");
65
94
  }
66
95
  return {
67
96
  name: "next-vite-router",
68
- configResolved(t) {
69
- l = i.resolve(t.root, e), p = i.join(i.dirname(l), "virtual-next-vite-router.d.ts"), d();
97
+ configResolved(o) {
98
+ u = l.resolve(o.root, e), y = l.join(l.dirname(u), "virtual-next-vite-router.d.ts"), v(P(u, n), t), f();
70
99
  },
71
- configureServer(t) {
72
- const f = (y) => {
73
- if (i.relative(l, y).startsWith("..") || ![s, r, n].includes(i.basename(y)))
100
+ configureServer(o) {
101
+ const g = (p) => {
102
+ if (l.relative(u, p).startsWith("..") || ![n, s, a].includes(l.basename(p)))
103
+ return;
104
+ try {
105
+ v(P(u, n), t);
106
+ } catch (R) {
107
+ o.config.logger.error(
108
+ R instanceof Error ? R.message : String(R)
109
+ );
74
110
  return;
75
- d();
76
- const R = t.moduleGraph.getModuleById(c);
77
- R && (t.moduleGraph.invalidateModule(R), t.ws.send({ type: "full-reload", path: "*" }));
111
+ }
112
+ f();
113
+ const d = o.moduleGraph.getModuleById(i);
114
+ d && (o.moduleGraph.invalidateModule(d), o.ws.send({ type: "full-reload", path: "*" }));
78
115
  };
79
- t.watcher.on("add", f), t.watcher.on("unlink", f);
116
+ o.watcher.on("add", g), o.watcher.on("unlink", g);
80
117
  },
81
- resolveId(t) {
82
- if (t === a)
83
- return c;
118
+ resolveId(o) {
119
+ if (o === c)
120
+ return i;
84
121
  },
85
- load(t) {
86
- if (t === c)
122
+ load(o) {
123
+ if (o === i)
87
124
  return `
88
125
  import { buildRouteTree, treeToRoutes, normalizeSegment, createLazyElement, useParams } from 'next-vite-router';
89
126
  import { Link, useRoutes } from 'react-router-dom';
90
127
 
91
- const pagesGlob = import.meta.glob('/${e}/**/${s}');
92
- const layoutsGlob = import.meta.glob('/${e}/**/${r}', { eager: true });
93
- const notFoundsGlob = import.meta.glob('/${e}/**/${n}');
128
+ const pagesGlob = import.meta.glob('/${e}/**/${n}');
129
+ const layoutsGlob = import.meta.glob('/${e}/**/${s}', { eager: true });
130
+ const notFoundsGlob = import.meta.glob('/${e}/**/${a}');
94
131
 
95
132
  const transformPath = (path) => path.replace('/${e}/', '');
96
133
 
@@ -105,7 +142,7 @@ const notFounds = Object.fromEntries(
105
142
  );
106
143
 
107
144
  export function generateRoutes() {
108
- const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(u)});
145
+ const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(t)});
109
146
  const routes = treeToRoutes(tree, true);
110
147
  return routes;
111
148
  }
@@ -124,5 +161,5 @@ export { useParams };
124
161
  };
125
162
  }
126
163
  export {
127
- k as nextViteRouter
164
+ S as nextViteRouter
128
165
  };
@@ -0,0 +1,64 @@
1
+ function p(t) {
2
+ return /^\([^)]+\)$/.test(t) ? "" : /^\[\[\.\.\.(.+)\]\]$/.test(t) || /^\[\.\.\.(.+)\]$/.test(t) ? "*" : /^\[([^\]]+)\]$/.test(t) ? t.replace(/^\[([^\]]+)\]$/, ":$1") : t;
3
+ }
4
+ function h(t, { pagesDir: c, pageFile: a = "page.tsx", layoutFile: o = "layout.tsx", notFoundFile: r = "not-found.tsx" } = {}) {
5
+ const n = t.replace(/\\/g, "/"), e = c == null ? void 0 : c.replace(/\\/g, "/").replace(/^\/|\/$/g, ""), l = [a, o, r].map((u) => u.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
6
+ return n.replace(
7
+ e ? new RegExp(`^.*(?:^|/)${e}/`) : /^.*(?:^|\/)(?:app|pages)\//,
8
+ ""
9
+ ).replace(/\\/g, "/").replace(new RegExp(`/?(?:${l})$`), "");
10
+ }
11
+ function $(t, c) {
12
+ const a = h(t, c), o = a ? a.split("/").map(p).filter(Boolean) : [];
13
+ return o.length > 0 ? `/${o.join("/")}` : "/";
14
+ }
15
+ function d(t, c) {
16
+ const a = /* @__PURE__ */ new Map();
17
+ for (const r of t) {
18
+ const n = $(r, c), e = a.get(n) ?? [];
19
+ e.push(r), a.set(n, e);
20
+ }
21
+ const o = [...a.entries()].filter(([, r]) => r.length > 1).sort(([r], [n]) => r.localeCompare(n));
22
+ if (o.length > 0)
23
+ throw new Error(
24
+ o.map(
25
+ ([r, n]) => `Duplicate route "${r}" detected:
26
+ ${n.sort((e, l) => e.localeCompare(l)).map((e) => ` ${e}`).join(`
27
+ `)}`
28
+ ).join(`
29
+
30
+ `)
31
+ );
32
+ }
33
+ function i(t, c, a, o, r) {
34
+ const n = h(c, r), e = n ? n.split("/") : [];
35
+ let l = t, u = "";
36
+ e.forEach((s, f) => {
37
+ u = u ? `${u}/${s}` : s, l.children.has(s) || l.children.set(s, {
38
+ segment: s,
39
+ fullPath: u,
40
+ children: /* @__PURE__ */ new Map()
41
+ }), l = l.children.get(s), f === e.length - 1 && (l[o] = a);
42
+ }), e.length === 0 && (t[o] = a);
43
+ }
44
+ function P(t, c, a, o) {
45
+ d(Object.keys(t), o);
46
+ const r = {
47
+ segment: "",
48
+ fullPath: "",
49
+ children: /* @__PURE__ */ new Map()
50
+ };
51
+ return Object.entries(c).forEach(([n, e]) => {
52
+ i(r, n, e.default, "layout", o);
53
+ }), Object.entries(t).forEach(([n, e]) => {
54
+ i(r, n, e, "page", o);
55
+ }), Object.entries(a).forEach(([n, e]) => {
56
+ i(r, n, e, "notFound", o);
57
+ }), r;
58
+ }
59
+ export {
60
+ d as a,
61
+ P as b,
62
+ h as e,
63
+ p as n
64
+ };
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.2",
4
4
  "description": "Next.js-like file-based routing for React + Vite",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,14 +0,0 @@
1
- function $(e) {
2
- return /^\([^)]+\)$/.test(e) ? "" : /^\[\[\.\.\.(.+)\]\]$/.test(e) || /^\[\.\.\.(.+)\]$/.test(e) ? "*" : /^\[([^\]]+)\]$/.test(e) ? e.replace(/^\[([^\]]+)\]$/, ":$1") : e;
3
- }
4
- function u(e, { pagesDir: t, pageFile: a = "page.tsx", layoutFile: n = "layout.tsx", notFoundFile: c = "not-found.tsx" } = {}) {
5
- const o = e.replace(/\\/g, "/"), r = t == null ? void 0 : t.replace(/\\/g, "/").replace(/^\/|\/$/g, ""), l = [a, n, c].map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
6
- return o.replace(
7
- r ? new RegExp(`^.*(?:^|/)${r}/`) : /^.*(?:^|\/)(?:app|pages)\//,
8
- ""
9
- ).replace(/\\/g, "/").replace(new RegExp(`/?(?:${l})$`), "");
10
- }
11
- export {
12
- u as e,
13
- $ as n
14
- };