next-vite-router 1.3.0 → 1.3.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
@@ -251,7 +251,7 @@ replace React Router.
251
251
  For an interactive setup:
252
252
 
253
253
  ```bash
254
- npx next-vite-router init
254
+ npx next-vite-router create
255
255
  ```
256
256
 
257
257
  The CLI creates a Vite React project, installs the package and
package/dist/cli.cjs CHANGED
@@ -3,6 +3,8 @@
3
3
  const commander = require("commander");
4
4
  const node_child_process = require("node:child_process");
5
5
  const child_process = require("child_process");
6
+ const fs$1 = require("node:fs");
7
+ const path$1 = require("node:path");
6
8
  const prompts = require("prompts");
7
9
  const fs = require("fs");
8
10
  const path = require("path");
@@ -30,13 +32,13 @@ function detectPackageManager() {
30
32
  if (fromUA) candidates.push(fromUA);
31
33
  if (preferred && !candidates.includes(preferred)) candidates.push(preferred);
32
34
  ["pnpm", "bun", "yarn", "npm"].forEach((p) => {
33
- const pm2 = p;
34
- if (!candidates.includes(pm2)) candidates.push(pm2);
35
+ const pm = p;
36
+ if (!candidates.includes(pm)) candidates.push(pm);
35
37
  });
36
38
  let chosen = null;
37
- for (const pm2 of candidates) {
38
- if (isAvailable(pm2)) {
39
- chosen = pm2;
39
+ for (const pm of candidates) {
40
+ if (isAvailable(pm)) {
41
+ chosen = pm;
40
42
  break;
41
43
  }
42
44
  }
@@ -87,7 +89,7 @@ if (typeof process !== "undefined") {
87
89
  const $ = {
88
90
  enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== "dumb" && (FORCE_COLOR != null && FORCE_COLOR !== "0" || isTTY)
89
91
  };
90
- function init$1(x, y) {
92
+ function init(x, y) {
91
93
  let rgx = new RegExp(`\\x1b\\[${y}m`, "g");
92
94
  let open = `\x1B[${x}m`, close = `\x1B[${y}m`;
93
95
  return function(txt) {
@@ -95,10 +97,10 @@ function init$1(x, y) {
95
97
  return open + (!!~("" + txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + close;
96
98
  };
97
99
  }
98
- const red = init$1(31, 39);
99
- const green = init$1(32, 39);
100
- const yellow = init$1(33, 39);
101
- const cyan = init$1(36, 39);
100
+ const red = init(31, 39);
101
+ const green = init(32, 39);
102
+ const yellow = init(33, 39);
103
+ const cyan = init(36, 39);
102
104
  const highlighter = {
103
105
  error: red,
104
106
  warn: yellow,
@@ -164,7 +166,6 @@ function updateAppTsConfig(projectDir, src) {
164
166
  const aliasPath = src ? "./src/*" : "./*";
165
167
  tsConfigApp.compilerOptions = {
166
168
  ...tsConfigApp.compilerOptions,
167
- baseUrl: ".",
168
169
  paths: {
169
170
  ...tsConfigApp.compilerOptions?.paths,
170
171
  "@/*": [aliasPath]
@@ -194,12 +195,24 @@ function updateTsConfigJson(projectDir, src) {
194
195
  if (!tsConfig.compilerOptions) {
195
196
  tsConfig.compilerOptions = {};
196
197
  }
197
- tsConfig.compilerOptions.baseUrl = ".";
198
198
  tsConfig.compilerOptions.paths = {
199
199
  "@/*": [src ? "./src/*" : "./*"]
200
200
  };
201
201
  fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2), "utf-8");
202
202
  }
203
+ function updateIndexHtml(projectDir, src) {
204
+ if (src) return;
205
+ const indexPath = path.join(projectDir, "index.html");
206
+ if (!fs.existsSync(indexPath)) return;
207
+ const content = fs.readFileSync(indexPath, "utf-8");
208
+ const updated = content.replace(
209
+ /(src\s*=\s*["'])\/src\/main\.tsx(["'])/,
210
+ "$1/main.tsx$2"
211
+ );
212
+ if (updated !== content) {
213
+ fs.writeFileSync(indexPath, updated, "utf-8");
214
+ }
215
+ }
203
216
  function createVirtualDTS(projectDir, useSrc) {
204
217
  const targetDir = useSrc ? path.join(projectDir, "src") : projectDir;
205
218
  if (useSrc && !fs.existsSync(targetDir)) {
@@ -374,7 +387,6 @@ function addTailwindToViteConfig(projectDir) {
374
387
  }
375
388
  writeViteConfig(projectDir, updatedContent);
376
389
  }
377
- const pm$1 = detectPackageManager();
378
390
  function startNextViteRouterConfig({
379
391
  projectDir = process.cwd(),
380
392
  src,
@@ -382,16 +394,31 @@ function startNextViteRouterConfig({
382
394
  }) {
383
395
  startViteConfig(projectDir, src);
384
396
  const srcPath = path.join(projectDir, "src");
385
- if (fs.existsSync(srcPath)) {
397
+ if (src) {
398
+ for (const entry of [
399
+ "App.tsx",
400
+ "App.css",
401
+ "index.css",
402
+ "main.tsx",
403
+ "assets"
404
+ ]) {
405
+ const entryPath = path.join(srcPath, entry);
406
+ if (fs.existsSync(entryPath)) {
407
+ fs.rmSync(entryPath, { recursive: true, force: true });
408
+ }
409
+ }
410
+ } else if (fs.existsSync(srcPath)) {
386
411
  fs.rmSync(srcPath, { recursive: true, force: true });
387
412
  }
388
413
  updateTsConfigJson(projectDir, src);
389
414
  updateAppTsConfig(projectDir, src);
390
415
  createVirtualDTS(projectDir, src);
391
416
  createMainTSX(projectDir, src);
417
+ updateIndexHtml(projectDir, src);
392
418
  if (tailwindcss) {
393
419
  logger.info("Setting up Tailwind CSS...");
394
- const addDevDepsCmd = pm$1.addDevDepsCommand([
420
+ const pm = detectPackageManager();
421
+ const addDevDepsCmd = pm.addDevDepsCommand([
395
422
  "tailwindcss",
396
423
  "@tailwindcss/vite"
397
424
  ]);
@@ -434,7 +461,7 @@ ${resolveConfig}
434
461
  }
435
462
  const pluginsRegex = /plugins\s*:\s*\[(.*?)\]/s;
436
463
  const match = viteConfigContent.match(pluginsRegex);
437
- const pluginString = "nextViteRouter()";
464
+ const pluginString = src ? "nextViteRouter()" : 'nextViteRouter({ pagesDir: "app" })';
438
465
  if (match) {
439
466
  const existing = match[1].trim();
440
467
  const newPlugins = existing ? `${existing}, ${pluginString}` : pluginString;
@@ -451,9 +478,9 @@ ${resolveConfig}
451
478
  }
452
479
  writeViteConfig(projectDir, viteConfigContent);
453
480
  }
454
- const pm = detectPackageManager();
455
- const init = new commander.Command().name("init").description(
456
- "Initialize a new Vite project with next-vite-router configuration"
481
+ const PROJECT_NAME_RE = /^[a-z0-9][a-z0-9._-]*$/i;
482
+ const create = new commander.Command().name("create").description(
483
+ "Create a new Vite project with next-vite-router configuration"
457
484
  ).action(async () => {
458
485
  const projectNameResponse = await prompts(
459
486
  {
@@ -470,6 +497,16 @@ const init = new commander.Command().name("init").description(
470
497
  }
471
498
  );
472
499
  const projectName = projectNameResponse.projectName;
500
+ if (!PROJECT_NAME_RE.test(projectName)) {
501
+ logger.error(
502
+ "Invalid project name (letters, digits, '.', '_' and '-' only)."
503
+ );
504
+ process.exit(1);
505
+ }
506
+ if (fs$1.existsSync(path$1.resolve(projectName))) {
507
+ logger.error(`Directory "${projectName}" already exists.`);
508
+ process.exit(1);
509
+ }
473
510
  const variantResponse = await prompts(
474
511
  {
475
512
  type: "select",
@@ -522,12 +559,10 @@ const init = new commander.Command().name("init").description(
522
559
  }
523
560
  );
524
561
  const useTailwind = tailwindResponse.useTailwind;
562
+ const pm = detectPackageManager();
525
563
  const createCommand = pm.createProjectCommand(projectName, variant);
526
564
  logger.info(`Creating project "${projectName}" using ${pm.name}...`);
527
- child_process.execSync(createCommand, {
528
- stdio: ["pipe", "inherit", "inherit"],
529
- input: "n\n"
530
- });
565
+ child_process.execSync(createCommand, { stdio: "inherit" });
531
566
  logger.info("Installing dependencies...");
532
567
  child_process.execSync(pm.installCommand, { stdio: "inherit", cwd: projectName });
533
568
  logger.info(
@@ -548,13 +583,18 @@ const init = new commander.Command().name("init").description(
548
583
  Project "${projectName}" is ready!
549
584
  `);
550
585
  });
551
- const version = "1.3.0";
586
+ const version = "1.3.1";
552
587
  const pkg = {
553
588
  version
554
589
  };
555
590
  async function main() {
556
591
  const program = new commander.Command();
557
- program.name("create-next-vite-router").version(pkg.version).addCommand(init);
558
- program.parse(process.argv);
592
+ program.name("next-vite-router").version(pkg.version).addCommand(create);
593
+ try {
594
+ await program.parseAsync(process.argv);
595
+ } catch (e) {
596
+ logger.error(e instanceof Error ? e.message : String(e));
597
+ process.exit(1);
598
+ }
559
599
  }
560
600
  main();
package/dist/plugin.cjs CHANGED
@@ -1,5 +1,5 @@
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(`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("node:fs"),f=require("node:path"),x=require("./tree-hc1dSMdb.cjs");function S(r){const a=r.map(x.routeSegmentToPath).filter(Boolean);return a.length>0?`/${a.join("/")}`:"/"}function T(r){const a=[["pagesDir",r.pagesDir],["pageFile",r.pageFile],["layoutFile",r.layoutFile],["notFoundFile",r.notFoundFile]];for(const[o,e]of a)if(!e.trim())throw new Error(`nextViteRouter option "${o}" must not be empty.`);const s=a.slice(1),i=s.find(([,o],e)=>s.slice(e+1).some(([,n])=>n===o));if(i)throw new Error(`nextViteRouter file options must be distinct; "${i[0]}" is duplicated.`)}function k(r){return r.replace(/\\/g,"/").replace(/^\.\//,"").replace(/^\/|\/$/g,"")}function G(r,a,s){const i=a.pageFile??"page.tsx",o=new Map;for(const e of R(r,i,s)){const n=x.extractRoutePath(e,a),l=x.parseRouteSegments(n),g=l.filter(u=>u.kind==="dynamic"||u.kind==="catchAll").map(u=>({name:u.name,optional:u.kind==="catchAll"&&u.optional})),p=S(l);o.set(p,{path:p,params:g});const m=l[l.length-1];if((m==null?void 0:m.kind)==="catchAll"&&m.optional){const u=S(l.slice(0,-1));o.has(u)||o.set(u,{path:u,params:[]})}}return[...o.values()].sort((e,n)=>e.path.localeCompare(n.path))}function R(r,a,s){const i=[];function o(e){for(const n of b.readdirSync(e,{withFileTypes:!0})){const l=f.join(e,n.name);if(n.isDirectory())o(l);else if(n.isFile()&&n.name===a){const g=f.relative(r,l).replace(/\\/g,"/");i.push(`/${s}/${g}`)}}}return b.existsSync(r)&&o(r),i}function O(r){const a=r.map(({path:i})=>` | ${JSON.stringify(i)}`).join(`
2
+ `),s=r.map(({path:i,params:o})=>{const e=o.length>0?[...o.map(({name:n,optional:l})=>` ${n}${l?"?":""}: string;`)].join(`
3
3
  `):" [key: string]: never;";return` ${JSON.stringify(i)}: {
4
4
  ${e}
5
5
  };`}).join(`
@@ -23,12 +23,12 @@ ${s}
23
23
  export function useParams<T extends Record<string, string | undefined> = Record<string, string | undefined>>(): T;
24
24
  export { Link, useRoutes } from 'react-router-dom';
25
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`
26
+ `}function j(r={}){const{pagesDir:a="src/app",pageFile:s="page.tsx",layoutFile:i="layout.tsx",notFoundFile:o="not-found.tsx"}=r;T({pagesDir:a,pageFile:s,layoutFile:i,notFoundFile:o});const e=k(a),n={pagesDir:e,pageFile:s,layoutFile:i,notFoundFile:o},l="virtual:next-vite-router",g="\0"+l;let p="",m="",u,P,h;function v(){const t=O(G(p,n,e));try{b.mkdirSync(f.dirname(m),{recursive:!0}),b.writeFileSync(m,t,"utf-8")}catch(d){const c=`next-vite-router could not write route types to ${m}: ${d instanceof Error?d.message:String(d)}`;u?u.warn(c):console.warn(c)}}return{name:"next-vite-router",configResolved(t){p=f.resolve(t.root,a),m=f.join(f.dirname(p),"virtual-next-vite-router.d.ts"),u=t.logger,x.assertNoRouteConflicts(R(p,s,e),R(p,o,e),n),v()},configureServer(t){if(P===t)return;h==null||h(),P=t;const d=$=>{if(f.relative(p,$).startsWith("..")||![s,i,o].includes(f.basename($)))return;try{x.assertNoRouteConflicts(R(p,s,e),R(p,o,e),n)}catch(F){t.config.logger.error(F instanceof Error?F.message:String(F));return}v();const w=t.moduleGraph.getModuleById(g);w&&(t.moduleGraph.invalidateModule(w),c&&clearTimeout(c),c=setTimeout(()=>{c=void 0,t.ws.send({type:"full-reload",path:"*"})},0))};let c;const y=()=>{c&&(clearTimeout(c),c=void 0),t.watcher.off("add",d),t.watcher.off("unlink",d),t.watcher.off("close",y),P===t&&(P=void 0,h=void 0)};t.watcher.on("add",d),t.watcher.on("unlink",d),t.watcher.once("close",y),h=y},resolveId(t){if(t===l)return g},load(t){if(t===g){const d=`/${e}/**/${s}`,c=`/${e}/**/${i}`,y=`/${e}/**/${o}`;return`
27
27
  import { buildRouteTree, treeToRoutes, normalizeSegment, createLazyElement, useParams } from 'next-vite-router';
28
28
  import { Link, useRoutes } from 'react-router-dom';
29
29
 
30
- const pagesGlob = import.meta.glob(${JSON.stringify(g)});
31
- const layoutsGlob = import.meta.glob(${JSON.stringify(p)}, { eager: true });
30
+ const pagesGlob = import.meta.glob(${JSON.stringify(d)});
31
+ const layoutsGlob = import.meta.glob(${JSON.stringify(c)}, { eager: true });
32
32
  const notFoundsGlob = import.meta.glob(${JSON.stringify(y)});
33
33
 
34
34
  // Keep the raw root-relative glob keys. extractRoutePath (via buildRouteTree)
@@ -40,7 +40,7 @@ const layouts = layoutsGlob;
40
40
  const notFounds = notFoundsGlob;
41
41
 
42
42
  export function generateRoutes() {
43
- const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(r)});
43
+ const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(n)});
44
44
  const routes = treeToRoutes(tree, true);
45
45
  return routes;
46
46
  }
@@ -60,4 +60,4 @@ if (import.meta.hot) {
60
60
 
61
61
  export { Link, useRoutes };
62
62
  export { useParams };
63
- `}}}}exports.nextViteRouter=O;
63
+ `}}}}exports.nextViteRouter=j;
package/dist/plugin.js CHANGED
@@ -1,73 +1,73 @@
1
1
  import P from "node:fs";
2
2
  import f from "node:path";
3
- import { c as v, e as S, p as T, r as k } from "./tree-DfnssFxU.js";
4
- function w(n) {
5
- const a = n.map(k).filter(Boolean);
3
+ import { c as w, e as T, p as k, r as G } from "./tree-DfnssFxU.js";
4
+ function S(r) {
5
+ const a = r.map(G).filter(Boolean);
6
6
  return a.length > 0 ? `/${a.join("/")}` : "/";
7
7
  }
8
- function G(n) {
8
+ function O(r) {
9
9
  const a = [
10
- ["pagesDir", n.pagesDir],
11
- ["pageFile", n.pageFile],
12
- ["layoutFile", n.layoutFile],
13
- ["notFoundFile", n.notFoundFile]
10
+ ["pagesDir", r.pagesDir],
11
+ ["pageFile", r.pageFile],
12
+ ["layoutFile", r.layoutFile],
13
+ ["notFoundFile", r.notFoundFile]
14
14
  ];
15
- for (const [e, t] of a)
16
- if (!t.trim())
17
- throw new Error(`nextViteRouter option "${e}" must not be empty.`);
15
+ for (const [o, e] of a)
16
+ if (!e.trim())
17
+ throw new Error(`nextViteRouter option "${o}" must not be empty.`);
18
18
  const s = a.slice(1), i = s.find(
19
- ([, e], t) => s.slice(t + 1).some(([, r]) => r === e)
19
+ ([, o], e) => s.slice(e + 1).some(([, n]) => n === o)
20
20
  );
21
21
  if (i)
22
22
  throw new Error(
23
23
  `nextViteRouter file options must be distinct; "${i[0]}" is duplicated.`
24
24
  );
25
25
  }
26
- function O(n) {
27
- return n.replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/|\/$/g, "");
26
+ function j(r) {
27
+ return r.replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/|\/$/g, "");
28
28
  }
29
- function j(n, a, s) {
30
- const i = a.pageFile ?? "page.tsx", e = /* @__PURE__ */ new Map();
31
- for (const t of R(n, i, s)) {
32
- const r = S(t, a), l = T(r), d = l.filter(
29
+ function D(r, a, s) {
30
+ const i = a.pageFile ?? "page.tsx", o = /* @__PURE__ */ new Map();
31
+ for (const e of R(r, i, s)) {
32
+ const n = T(e, a), l = k(n), g = l.filter(
33
33
  (u) => u.kind === "dynamic" || u.kind === "catchAll"
34
34
  ).map((u) => ({
35
35
  name: u.name,
36
36
  optional: u.kind === "catchAll" && u.optional
37
- })), c = w(l);
38
- e.set(c, { path: c, params: d });
37
+ })), p = S(l);
38
+ o.set(p, { path: p, params: g });
39
39
  const m = l[l.length - 1];
40
40
  if ((m == null ? void 0 : m.kind) === "catchAll" && m.optional) {
41
- const u = w(l.slice(0, -1));
42
- e.has(u) || e.set(u, { path: u, params: [] });
41
+ const u = S(l.slice(0, -1));
42
+ o.has(u) || o.set(u, { path: u, params: [] });
43
43
  }
44
44
  }
45
- return [...e.values()].sort((t, r) => t.path.localeCompare(r.path));
45
+ return [...o.values()].sort((e, n) => e.path.localeCompare(n.path));
46
46
  }
47
- function R(n, a, s) {
47
+ function R(r, a, s) {
48
48
  const i = [];
49
- function e(t) {
50
- for (const r of P.readdirSync(t, { withFileTypes: !0 })) {
51
- const l = f.join(t, r.name);
52
- if (r.isDirectory())
53
- e(l);
54
- else if (r.isFile() && r.name === a) {
55
- const d = f.relative(n, l).replace(/\\/g, "/");
56
- i.push(`/${s}/${d}`);
49
+ function o(e) {
50
+ for (const n of P.readdirSync(e, { withFileTypes: !0 })) {
51
+ const l = f.join(e, n.name);
52
+ if (n.isDirectory())
53
+ o(l);
54
+ else if (n.isFile() && n.name === a) {
55
+ const g = f.relative(r, l).replace(/\\/g, "/");
56
+ i.push(`/${s}/${g}`);
57
57
  }
58
58
  }
59
59
  }
60
- return P.existsSync(n) && e(n), i;
60
+ return P.existsSync(r) && o(r), i;
61
61
  }
62
- function D(n) {
63
- const a = n.map(({ path: i }) => ` | ${JSON.stringify(i)}`).join(`
64
- `), s = n.map(({ path: i, params: e }) => {
65
- const t = e.length > 0 ? [
66
- ...e.map(({ name: r, optional: l }) => ` ${r}${l ? "?" : ""}: string;`)
62
+ function N(r) {
63
+ const a = r.map(({ path: i }) => ` | ${JSON.stringify(i)}`).join(`
64
+ `), s = r.map(({ path: i, params: o }) => {
65
+ const e = o.length > 0 ? [
66
+ ...o.map(({ name: n, optional: l }) => ` ${n}${l ? "?" : ""}: string;`)
67
67
  ].join(`
68
68
  `) : " [key: string]: never;";
69
69
  return ` ${JSON.stringify(i)}: {
70
- ${t}
70
+ ${e}
71
71
  };`;
72
72
  }).join(`
73
73
  `);
@@ -93,73 +93,78 @@ ${s}
93
93
  }
94
94
  `;
95
95
  }
96
- function z(n = {}) {
96
+ function L(r = {}) {
97
97
  const {
98
98
  pagesDir: a = "src/app",
99
99
  pageFile: s = "page.tsx",
100
100
  layoutFile: i = "layout.tsx",
101
- notFoundFile: e = "not-found.tsx"
102
- } = n;
103
- G({ pagesDir: a, pageFile: s, layoutFile: i, notFoundFile: e });
104
- const t = O(a), r = { pagesDir: t, pageFile: s, layoutFile: i, notFoundFile: e }, l = "virtual:next-vite-router", d = "\0" + l;
105
- let c = "", m = "", u, h;
106
- function F() {
107
- const o = D(j(c, r, t));
108
- P.writeFileSync(m, o, "utf-8");
101
+ notFoundFile: o = "not-found.tsx"
102
+ } = r;
103
+ O({ pagesDir: a, pageFile: s, layoutFile: i, notFoundFile: o });
104
+ const e = j(a), n = { pagesDir: e, pageFile: s, layoutFile: i, notFoundFile: o }, l = "virtual:next-vite-router", g = "\0" + l;
105
+ let p = "", m = "", u, x, h;
106
+ function $() {
107
+ const t = N(D(p, n, e));
108
+ try {
109
+ P.mkdirSync(f.dirname(m), { recursive: !0 }), P.writeFileSync(m, t, "utf-8");
110
+ } catch (d) {
111
+ const c = `next-vite-router could not write route types to ${m}: ${d instanceof Error ? d.message : String(d)}`;
112
+ u ? u.warn(c) : console.warn(c);
113
+ }
109
114
  }
110
115
  return {
111
116
  name: "next-vite-router",
112
- configResolved(o) {
113
- c = f.resolve(o.root, a), m = f.join(f.dirname(c), "virtual-next-vite-router.d.ts"), v(
114
- R(c, s, t),
115
- R(c, e, t),
116
- r
117
- ), F();
117
+ configResolved(t) {
118
+ p = f.resolve(t.root, a), m = f.join(f.dirname(p), "virtual-next-vite-router.d.ts"), u = t.logger, w(
119
+ R(p, s, e),
120
+ R(p, o, e),
121
+ n
122
+ ), $();
118
123
  },
119
- configureServer(o) {
120
- if (u === o)
124
+ configureServer(t) {
125
+ if (x === t)
121
126
  return;
122
- h == null || h(), u = o;
123
- const g = (b) => {
124
- if (f.relative(c, b).startsWith("..") || ![s, i, e].includes(f.basename(b)))
127
+ h == null || h(), x = t;
128
+ const d = (b) => {
129
+ if (f.relative(p, b).startsWith("..") || ![s, i, o].includes(f.basename(b)))
125
130
  return;
126
131
  try {
127
- v(
128
- R(c, s, t),
129
- R(c, e, t),
130
- r
132
+ w(
133
+ R(p, s, e),
134
+ R(p, o, e),
135
+ n
131
136
  );
132
- } catch (x) {
133
- o.config.logger.error(
134
- x instanceof Error ? x.message : String(x)
137
+ } catch (F) {
138
+ t.config.logger.error(
139
+ F instanceof Error ? F.message : String(F)
135
140
  );
136
141
  return;
137
142
  }
138
- F();
139
- const $ = o.moduleGraph.getModuleById(d);
140
- $ && (o.moduleGraph.invalidateModule($), p && clearTimeout(p), p = setTimeout(() => {
141
- p = void 0, o.ws.send({ type: "full-reload", path: "*" });
143
+ $();
144
+ const v = t.moduleGraph.getModuleById(g);
145
+ v && (t.moduleGraph.invalidateModule(v), c && clearTimeout(c), c = setTimeout(() => {
146
+ c = void 0, t.ws.send({ type: "full-reload", path: "*" });
142
147
  }, 0));
143
148
  };
144
- let p;
149
+ let c;
145
150
  const y = () => {
146
- 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);
151
+ c && (clearTimeout(c), c = void 0), t.watcher.off("add", d), t.watcher.off("unlink", d), t.watcher.off("close", y), x === t && (x = void 0, h = void 0);
147
152
  };
148
- o.watcher.on("add", g), o.watcher.on("unlink", g), o.watcher.once("close", y), h = y;
153
+ t.watcher.on("add", d), t.watcher.on("unlink", d), t.watcher.once("close", y), h = y;
149
154
  },
150
- resolveId(o) {
151
- if (o === l)
152
- return d;
155
+ resolveId(t) {
156
+ if (t === l)
157
+ return g;
153
158
  },
154
- load(o) {
155
- if (o === d) {
156
- const g = `/${t}/**/${s}`, p = `/${t}/**/${i}`, y = `/${t}/**/${e}`;
159
+ load(t) {
160
+ if (t === g) {
161
+ const d = `/${e}/**/${s}`, c = `/${e}/**/${i}`, y = `/${e}/**/${o}`;
157
162
  return `
158
163
  import { buildRouteTree, treeToRoutes, normalizeSegment, createLazyElement, useParams } from 'next-vite-router';
159
164
  import { Link, useRoutes } from 'react-router-dom';
160
165
 
161
- const pagesGlob = import.meta.glob(${JSON.stringify(g)});
162
- const layoutsGlob = import.meta.glob(${JSON.stringify(p)}, { eager: true });
166
+ const pagesGlob = import.meta.glob(${JSON.stringify(d)});
167
+ const layoutsGlob = import.meta.glob(${JSON.stringify(c)}, { eager: true });
163
168
  const notFoundsGlob = import.meta.glob(${JSON.stringify(y)});
164
169
 
165
170
  // Keep the raw root-relative glob keys. extractRoutePath (via buildRouteTree)
@@ -171,7 +176,7 @@ const layouts = layoutsGlob;
171
176
  const notFounds = notFoundsGlob;
172
177
 
173
178
  export function generateRoutes() {
174
- const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(r)});
179
+ const tree = buildRouteTree(pages, layouts, notFounds, ${JSON.stringify(n)});
175
180
  const routes = treeToRoutes(tree, true);
176
181
  return routes;
177
182
  }
@@ -197,5 +202,5 @@ export { useParams };
197
202
  };
198
203
  }
199
204
  export {
200
- z as nextViteRouter
205
+ L as nextViteRouter
201
206
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "next-vite-router",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Next.js-like file-based routing for React + Vite",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "sideEffects": false,
9
9
  "bin": {
10
- "create-next-vite-router": "./dist/cli.cjs"
10
+ "next-vite-router": "./dist/cli.cjs"
11
11
  },
12
12
  "exports": {
13
13
  ".": {