@rangojs/router 0.0.0-experimental.5035014b → 0.0.0-experimental.5047b519
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 +124 -38
- package/dist/bin/rango.js +130 -47
- package/dist/vite/index.js +829 -368
- package/dist/vite/index.js.bak +5448 -0
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +4 -4
- package/skills/handler-use/SKILL.md +362 -0
- package/skills/hooks/SKILL.md +25 -19
- package/skills/intercept/SKILL.md +20 -0
- package/skills/layout/SKILL.md +22 -0
- package/skills/links/SKILL.md +77 -12
- package/skills/middleware/SKILL.md +34 -3
- package/skills/migrate-nextjs/SKILL.md +560 -0
- package/skills/migrate-react-router/SKILL.md +765 -0
- package/skills/parallel/SKILL.md +59 -0
- package/skills/prerender/SKILL.md +110 -68
- package/skills/rango/SKILL.md +24 -22
- package/skills/response-routes/SKILL.md +8 -0
- package/skills/route/SKILL.md +24 -0
- package/skills/router-setup/SKILL.md +35 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/src/__internal.ts +1 -1
- package/src/browser/app-version.ts +14 -0
- package/src/browser/navigation-bridge.ts +37 -5
- package/src/browser/navigation-client.ts +128 -69
- package/src/browser/navigation-store.ts +43 -8
- package/src/browser/partial-update.ts +41 -7
- package/src/browser/prefetch/cache.ts +113 -21
- package/src/browser/prefetch/fetch.ts +156 -18
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/react/Link.tsx +72 -8
- package/src/browser/react/NavigationProvider.tsx +14 -3
- package/src/browser/react/context.ts +7 -2
- package/src/browser/react/use-handle.ts +9 -58
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +11 -1
- package/src/browser/react/use-router.ts +21 -8
- package/src/browser/rsc-router.tsx +26 -3
- package/src/browser/scroll-restoration.ts +10 -8
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/server-action-bridge.ts +8 -18
- package/src/browser/types.ts +20 -5
- package/src/build/generate-manifest.ts +6 -6
- package/src/build/generate-route-types.ts +3 -0
- package/src/build/route-trie.ts +50 -24
- package/src/build/route-types/include-resolution.ts +8 -1
- package/src/build/route-types/router-processing.ts +211 -72
- package/src/build/route-types/scan-filter.ts +8 -1
- package/src/client.tsx +84 -230
- package/src/deps/browser.ts +0 -1
- package/src/handle.ts +40 -0
- package/src/index.rsc.ts +3 -1
- package/src/index.ts +46 -6
- package/src/prerender/store.ts +5 -4
- package/src/prerender.ts +138 -77
- package/src/response-utils.ts +28 -0
- package/src/reverse.ts +25 -1
- package/src/route-definition/dsl-helpers.ts +194 -32
- package/src/route-definition/helpers-types.ts +61 -14
- package/src/route-definition/index.ts +3 -0
- package/src/route-definition/redirect.ts +9 -1
- package/src/route-definition/resolve-handler-use.ts +149 -0
- package/src/route-types.ts +18 -0
- package/src/router/content-negotiation.ts +100 -1
- package/src/router/decode-params.ts +21 -0
- package/src/router/handler-context.ts +51 -15
- package/src/router/intercept-resolution.ts +9 -4
- package/src/router/lazy-includes.ts +5 -5
- package/src/router/loader-resolution.ts +150 -21
- package/src/router/manifest.ts +22 -13
- package/src/router/match-api.ts +124 -189
- package/src/router/match-middleware/cache-lookup.ts +28 -8
- package/src/router/match-middleware/segment-resolution.ts +53 -0
- package/src/router/match-result.ts +82 -4
- package/src/router/middleware-types.ts +0 -6
- package/src/router/middleware.ts +22 -6
- package/src/router/navigation-snapshot.ts +182 -0
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/prerender-match.ts +110 -10
- package/src/router/preview-match.ts +30 -102
- package/src/router/request-classification.ts +310 -0
- package/src/router/route-snapshot.ts +245 -0
- package/src/router/router-interfaces.ts +36 -4
- package/src/router/router-options.ts +37 -11
- package/src/router/segment-resolution/fresh.ts +70 -5
- package/src/router/segment-resolution/revalidation.ts +87 -9
- package/src/router/trie-matching.ts +10 -4
- package/src/router.ts +53 -5
- package/src/rsc/handler.ts +476 -394
- package/src/rsc/helpers.ts +69 -41
- package/src/rsc/loader-fetch.ts +18 -3
- package/src/rsc/manifest-init.ts +5 -1
- package/src/rsc/progressive-enhancement.ts +14 -3
- package/src/rsc/response-route-handler.ts +11 -1
- package/src/rsc/rsc-rendering.ts +15 -2
- package/src/rsc/server-action.ts +10 -2
- package/src/rsc/ssr-setup.ts +2 -2
- package/src/rsc/types.ts +6 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +11 -61
- package/src/server/context.ts +65 -5
- package/src/server/handle-store.ts +19 -0
- package/src/server/loader-registry.ts +9 -8
- package/src/server/request-context.ts +132 -13
- package/src/ssr/index.tsx +3 -0
- package/src/static-handler.ts +18 -6
- package/src/types/cache-types.ts +4 -4
- package/src/types/handler-context.ts +17 -11
- package/src/types/loader-types.ts +32 -5
- package/src/types/route-entry.ts +12 -1
- package/src/types/segments.ts +1 -1
- package/src/urls/include-helper.ts +24 -14
- package/src/urls/path-helper-types.ts +39 -6
- package/src/urls/path-helper.ts +47 -12
- package/src/urls/pattern-types.ts +12 -0
- package/src/urls/response-types.ts +16 -6
- package/src/use-loader.tsx +77 -5
- package/src/vite/discovery/bundle-postprocess.ts +30 -33
- package/src/vite/discovery/discover-routers.ts +5 -1
- package/src/vite/discovery/prerender-collection.ts +128 -74
- package/src/vite/discovery/state.ts +13 -4
- package/src/vite/index.ts +4 -0
- package/src/vite/plugin-types.ts +60 -5
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-id-utils.ts +12 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +30 -0
- package/src/vite/plugins/expose-internal-ids.ts +257 -40
- package/src/vite/plugins/performance-tracks.ts +66 -198
- package/src/vite/plugins/refresh-cmd.ts +88 -26
- package/src/vite/rango.ts +18 -5
- package/src/vite/router-discovery.ts +237 -37
- package/src/vite/utils/prerender-utils.ts +37 -5
- package/src/vite/utils/shared-utils.ts +3 -2
- package/src/browser/debug-channel.ts +0 -93
package/README.md
CHANGED
|
@@ -91,24 +91,29 @@ This file is a server/RSC module and should import router construction APIs from
|
|
|
91
91
|
|
|
92
92
|
```tsx
|
|
93
93
|
// src/router.tsx
|
|
94
|
-
import { createRouter
|
|
95
|
-
import { Document } from "./document";
|
|
94
|
+
import { createRouter } from "@rangojs/router";
|
|
96
95
|
|
|
97
|
-
const
|
|
98
|
-
path("/",
|
|
99
|
-
path("
|
|
96
|
+
export const router = createRouter().routes(({ path }) => [
|
|
97
|
+
path("/", HomePage, { name: "home" }),
|
|
98
|
+
path("/about", AboutPage, { name: "about" }),
|
|
100
99
|
]);
|
|
101
100
|
|
|
101
|
+
export const reverse = router.reverse;
|
|
102
|
+
// reverse("home") -> "/"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For larger apps, extract route modules with `urls()` and compose with `include()`:
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
import { createRouter, urls } from "@rangojs/router";
|
|
109
|
+
import { blogPatterns } from "./urls/blog";
|
|
110
|
+
|
|
102
111
|
const urlpatterns = urls(({ path, include }) => [
|
|
103
112
|
path("/", HomePage, { name: "home" }),
|
|
104
113
|
include("/blog", blogPatterns, { name: "blog" }),
|
|
105
114
|
]);
|
|
106
115
|
|
|
107
|
-
export const router = createRouter(
|
|
108
|
-
|
|
109
|
-
// Export typed reverse function for URL generation by route name
|
|
110
|
-
export const reverse = router.reverse;
|
|
111
|
-
|
|
116
|
+
export const router = createRouter().routes(urlpatterns);
|
|
112
117
|
// reverse("blog.post", { slug: "hello-world" }) -> "/blog/hello-world"
|
|
113
118
|
```
|
|
114
119
|
|
|
@@ -156,13 +161,18 @@ const urlpatterns = urls(({ path }) => [
|
|
|
156
161
|
]);
|
|
157
162
|
```
|
|
158
163
|
|
|
159
|
-
Use `reverse()` as the default way to link to routes:
|
|
164
|
+
Use `ctx.reverse()` from handler context as the default way to link to routes from server code:
|
|
160
165
|
|
|
161
166
|
```tsx
|
|
162
|
-
|
|
163
|
-
|
|
167
|
+
const ProductPage: Handler<"product"> = (ctx) => {
|
|
168
|
+
const url = ctx.reverse("product", { slug: "widget" }); // "/product/widget"
|
|
169
|
+
const searchUrl = ctx.reverse("search", undefined, { q: "rsc" }); // "/search?q=rsc"
|
|
170
|
+
return <Link to={url}>Widget</Link>;
|
|
171
|
+
};
|
|
164
172
|
```
|
|
165
173
|
|
|
174
|
+
`router.reverse()` (exported from the router module) is the same function without a handler context, useful in scripts or tests. In request code, prefer `ctx.reverse()` — it auto-fills mount params from the current match.
|
|
175
|
+
|
|
166
176
|
### Composable URL Modules
|
|
167
177
|
|
|
168
178
|
Local route names compose cleanly with `include(..., { name })`:
|
|
@@ -474,39 +484,62 @@ const urlpatterns = urls(({ path, loader }) => [
|
|
|
474
484
|
|
|
475
485
|
## Navigation & Links
|
|
476
486
|
|
|
477
|
-
### Named Routes with `reverse()` (Server
|
|
487
|
+
### Named Routes with `ctx.reverse()` (Server)
|
|
478
488
|
|
|
479
|
-
In server components, use `reverse()` to generate URLs by route name:
|
|
489
|
+
In server components and handlers, use `ctx.reverse()` to generate URLs by route name. This is the default — it is typed, auto-fills mount params from the current match, and resolves both local (`.name`) and absolute (`name.sub`) names:
|
|
480
490
|
|
|
481
491
|
```tsx
|
|
482
492
|
import { Link } from "@rangojs/router/client";
|
|
493
|
+
import type { Handler } from "@rangojs/router";
|
|
494
|
+
|
|
495
|
+
const BlogPostPage: Handler<"blogPost"> = (ctx) => {
|
|
496
|
+
const backUrl = ctx.reverse("blog");
|
|
497
|
+
return <Link to={backUrl}>Back to blog</Link>;
|
|
498
|
+
};
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
`reverse()` is type-safe — route names and required params are checked at compile time. Included routes use dotted names: `ctx.reverse("api.health")`.
|
|
502
|
+
|
|
503
|
+
For scripts, tests, or other code without a handler context, import the router-level `reverse`:
|
|
504
|
+
|
|
505
|
+
```tsx
|
|
483
506
|
import { reverse } from "./router";
|
|
507
|
+
reverse("blogPost", { slug: "my-post" });
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
### Client Components
|
|
484
511
|
|
|
485
|
-
|
|
512
|
+
**`reverse()` is server-only.** It depends on the route manifest and handler context — neither is available in the browser bundle. Client components receive URLs as props, loader data, or server-action return values:
|
|
513
|
+
|
|
514
|
+
```tsx
|
|
515
|
+
// server
|
|
516
|
+
function BlogIndex(ctx: HandlerContext) {
|
|
517
|
+
return (
|
|
518
|
+
<Nav
|
|
519
|
+
home={ctx.reverse("home")}
|
|
520
|
+
post={ctx.reverse("blogPost", { slug: "my-post" })}
|
|
521
|
+
/>
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// client
|
|
526
|
+
"use client";
|
|
527
|
+
import { Link } from "@rangojs/router/client";
|
|
528
|
+
export function Nav({ home, post }: { home: string; post: string }) {
|
|
486
529
|
return (
|
|
487
530
|
<nav>
|
|
488
|
-
<Link to={
|
|
489
|
-
<Link to={
|
|
490
|
-
<Link to={reverse("about")}>About</Link>
|
|
531
|
+
<Link to={home}>Home</Link>
|
|
532
|
+
<Link to={post}>My Post</Link>
|
|
491
533
|
</nav>
|
|
492
534
|
);
|
|
493
535
|
}
|
|
494
536
|
```
|
|
495
537
|
|
|
496
|
-
`
|
|
497
|
-
|
|
498
|
-
Handlers also have `ctx.reverse()` directly on the context:
|
|
499
|
-
|
|
500
|
-
```tsx
|
|
501
|
-
const BlogPostPage: Handler<"blogPost"> = (ctx) => {
|
|
502
|
-
const backUrl = ctx.reverse("blog");
|
|
503
|
-
return <Link to={backUrl}>Back to blog</Link>;
|
|
504
|
-
};
|
|
505
|
-
```
|
|
538
|
+
For client-side navigation to static paths (no named-route lookup), use `href()` — see below. For URLs tied to named routes, always generate on the server and pass the string in.
|
|
506
539
|
|
|
507
540
|
### `href()` for Path Validation (Client Components)
|
|
508
541
|
|
|
509
|
-
In client components, use `href()` for compile-time path validation:
|
|
542
|
+
In client components, use `href()` for compile-time path validation on static path strings:
|
|
510
543
|
|
|
511
544
|
```tsx
|
|
512
545
|
"use client";
|
|
@@ -711,10 +744,12 @@ export const BlogPost = Prerender(
|
|
|
711
744
|
|
|
712
745
|
### Passthrough for Unknown Params
|
|
713
746
|
|
|
747
|
+
Wrap a `Prerender` definition with `Passthrough()` to add a live handler for unknown params at runtime. The build handler runs at build time, the live handler runs at request time for params not in the prerender cache.
|
|
748
|
+
|
|
714
749
|
```tsx
|
|
715
|
-
import { Prerender } from "@rangojs/router";
|
|
750
|
+
import { Prerender, Passthrough } from "@rangojs/router";
|
|
716
751
|
|
|
717
|
-
export const
|
|
752
|
+
export const ProductPageDef = Prerender(
|
|
718
753
|
async () => {
|
|
719
754
|
const featured = await db.getFeaturedProducts();
|
|
720
755
|
return featured.map((p) => ({ id: p.id }));
|
|
@@ -723,16 +758,22 @@ export const ProductPage = Prerender(
|
|
|
723
758
|
const product = await db.getProduct(ctx.params.id);
|
|
724
759
|
return <Product data={product} />;
|
|
725
760
|
},
|
|
726
|
-
{ passthrough: true },
|
|
727
761
|
);
|
|
728
|
-
```
|
|
729
762
|
|
|
730
|
-
|
|
763
|
+
// In route definition:
|
|
764
|
+
path(
|
|
765
|
+
"/products/:id",
|
|
766
|
+
Passthrough(ProductPageDef, async (ctx) => {
|
|
767
|
+
const product = await ctx.env.DB.getProduct(ctx.params.id);
|
|
768
|
+
return <Product data={product} />;
|
|
769
|
+
}),
|
|
770
|
+
);
|
|
771
|
+
```
|
|
731
772
|
|
|
732
|
-
|
|
773
|
+
Build handlers can also skip individual param sets with `ctx.passthrough()`, deferring them to the live handler:
|
|
733
774
|
|
|
734
775
|
```tsx
|
|
735
|
-
export const
|
|
776
|
+
export const ProductPageDef = Prerender(
|
|
736
777
|
async () => {
|
|
737
778
|
const all = await db.getAllProducts();
|
|
738
779
|
return all.map((p) => ({ id: p.id }));
|
|
@@ -742,10 +783,55 @@ export const ProductPage = Prerender(
|
|
|
742
783
|
if (!product.published) return ctx.passthrough();
|
|
743
784
|
return <Product data={product} />;
|
|
744
785
|
},
|
|
745
|
-
{ passthrough: true },
|
|
746
786
|
);
|
|
747
787
|
```
|
|
748
788
|
|
|
789
|
+
### Build-Time Environment Bindings
|
|
790
|
+
|
|
791
|
+
Prerender handlers can access platform bindings (KV, D1, R2) at build time when `buildEnv` is configured in the Vite plugin:
|
|
792
|
+
|
|
793
|
+
```ts
|
|
794
|
+
// vite.config.ts
|
|
795
|
+
import { rango } from "@rangojs/router/vite";
|
|
796
|
+
|
|
797
|
+
rango({ preset: "cloudflare", buildEnv: "auto" });
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
With `buildEnv: "auto"`, the plugin calls `wrangler.getPlatformProxy()` to provide local bindings. Handlers then access `ctx.env` during build:
|
|
801
|
+
|
|
802
|
+
```tsx
|
|
803
|
+
export const BlogPosts = Prerender<{ slug: string }>(
|
|
804
|
+
async (ctx) => {
|
|
805
|
+
const rows = await ctx.env.DB.prepare("SELECT slug FROM posts").all();
|
|
806
|
+
return rows.map((r) => ({ slug: r.slug }));
|
|
807
|
+
},
|
|
808
|
+
async (ctx) => {
|
|
809
|
+
const post = await ctx.env.DB.prepare("SELECT * FROM posts WHERE slug = ?")
|
|
810
|
+
.bind(ctx.params.slug)
|
|
811
|
+
.first();
|
|
812
|
+
return <BlogPost post={post} />;
|
|
813
|
+
},
|
|
814
|
+
);
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
`buildEnv` also accepts a factory function or plain object:
|
|
818
|
+
|
|
819
|
+
```ts
|
|
820
|
+
// Custom factory
|
|
821
|
+
rango({
|
|
822
|
+
buildEnv: async (ctx) => {
|
|
823
|
+
const { getPlatformProxy } = await import("wrangler");
|
|
824
|
+
const proxy = await getPlatformProxy();
|
|
825
|
+
return { env: proxy.env, dispose: proxy.dispose };
|
|
826
|
+
},
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
// Plain object (Node.js)
|
|
830
|
+
rango({ buildEnv: { DATABASE_URL: process.env.DATABASE_URL } });
|
|
831
|
+
```
|
|
832
|
+
|
|
833
|
+
Build-time env applies to both production builds and dev on-demand prerender. Without `buildEnv`, accessing `ctx.env` in a Prerender handler throws with a clear error.
|
|
834
|
+
|
|
749
835
|
## Theme
|
|
750
836
|
|
|
751
837
|
### Router Configuration
|
package/dist/bin/rango.js
CHANGED
|
@@ -218,7 +218,8 @@ function findTsFiles(dir, filter) {
|
|
|
218
218
|
for (const entry of entries) {
|
|
219
219
|
const fullPath = join(dir, entry.name);
|
|
220
220
|
if (entry.isDirectory()) {
|
|
221
|
-
if (entry.name === "node_modules" || entry.name.startsWith("."))
|
|
221
|
+
if (entry.name === "node_modules" || entry.name.startsWith(".") || entry.name === "dist" || entry.name === "build" || entry.name === "coverage")
|
|
222
|
+
continue;
|
|
222
223
|
results.push(...findTsFiles(fullPath, filter));
|
|
223
224
|
} else if ((entry.name.endsWith(".ts") || entry.name.endsWith(".tsx") || entry.name.endsWith(".js") || entry.name.endsWith(".jsx")) && !entry.name.includes(".gen.")) {
|
|
224
225
|
if (filter && !filter(fullPath)) continue;
|
|
@@ -450,7 +451,7 @@ function buildRouteMapFromBlock(block, fullSource, filePath, visited, searchSche
|
|
|
450
451
|
}
|
|
451
452
|
return routeMap;
|
|
452
453
|
}
|
|
453
|
-
function buildCombinedRouteMapWithSearch(filePath, variableName, visited, diagnosticsOut) {
|
|
454
|
+
function buildCombinedRouteMapWithSearch(filePath, variableName, visited, diagnosticsOut, inlineBlock) {
|
|
454
455
|
visited = visited ?? /* @__PURE__ */ new Set();
|
|
455
456
|
const realPath = resolve(filePath);
|
|
456
457
|
const key = variableName ? `${realPath}:${variableName}` : realPath;
|
|
@@ -466,7 +467,9 @@ function buildCombinedRouteMapWithSearch(filePath, variableName, visited, diagno
|
|
|
466
467
|
return { routes: {}, searchSchemas: {} };
|
|
467
468
|
}
|
|
468
469
|
let block;
|
|
469
|
-
if (
|
|
470
|
+
if (inlineBlock) {
|
|
471
|
+
block = inlineBlock;
|
|
472
|
+
} else if (variableName) {
|
|
470
473
|
const extracted = extractUrlsBlockForVariable(source, variableName);
|
|
471
474
|
if (!extracted) return { routes: {}, searchSchemas: {} };
|
|
472
475
|
block = extracted;
|
|
@@ -671,7 +674,7 @@ Router root: ${conflict.ancestor}
|
|
|
671
674
|
Nested router: ${conflict.nested}
|
|
672
675
|
Move the nested router into a sibling directory or configure it as a separate app root.`;
|
|
673
676
|
}
|
|
674
|
-
function
|
|
677
|
+
function extractUrlsFromRouter(code) {
|
|
675
678
|
const sourceFile = ts5.createSourceFile(
|
|
676
679
|
"router.tsx",
|
|
677
680
|
code,
|
|
@@ -685,24 +688,70 @@ function extractUrlsVariableFromRouter(code) {
|
|
|
685
688
|
const callee = node.expression;
|
|
686
689
|
return ts5.isIdentifier(callee) && callee.text === "createRouter";
|
|
687
690
|
}
|
|
691
|
+
function isInlineBuilder(node) {
|
|
692
|
+
return ts5.isArrowFunction(node) || ts5.isFunctionExpression(node);
|
|
693
|
+
}
|
|
694
|
+
function isRoutesOnCreateRouter(node) {
|
|
695
|
+
if (!ts5.isPropertyAccessExpression(node.expression) || node.expression.name.text !== "routes")
|
|
696
|
+
return false;
|
|
697
|
+
let inner = node.expression.expression;
|
|
698
|
+
while (ts5.isCallExpression(inner) && ts5.isPropertyAccessExpression(inner.expression)) {
|
|
699
|
+
inner = inner.expression.expression;
|
|
700
|
+
}
|
|
701
|
+
return isCreateRouterCall(inner);
|
|
702
|
+
}
|
|
688
703
|
function visit(node) {
|
|
689
704
|
if (result) return;
|
|
690
|
-
if (ts5.isCallExpression(node) &&
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
result = node.arguments[0].text;
|
|
697
|
-
return;
|
|
705
|
+
if (ts5.isCallExpression(node) && node.arguments.length >= 1 && isRoutesOnCreateRouter(node)) {
|
|
706
|
+
const arg = node.arguments[0];
|
|
707
|
+
if (ts5.isIdentifier(arg)) {
|
|
708
|
+
result = { kind: "variable", name: arg.text };
|
|
709
|
+
} else if (isInlineBuilder(arg)) {
|
|
710
|
+
result = { kind: "inline", block: arg.getText(sourceFile) };
|
|
698
711
|
}
|
|
712
|
+
return;
|
|
699
713
|
}
|
|
700
714
|
if (isCreateRouterCall(node)) {
|
|
701
715
|
const callExpr = node;
|
|
702
|
-
for (const
|
|
716
|
+
for (const callArg of callExpr.arguments) {
|
|
717
|
+
if (ts5.isObjectLiteralExpression(callArg)) {
|
|
718
|
+
for (const prop of callArg.properties) {
|
|
719
|
+
if (ts5.isPropertyAssignment(prop) && ts5.isIdentifier(prop.name) && prop.name.text === "urls") {
|
|
720
|
+
if (ts5.isIdentifier(prop.initializer)) {
|
|
721
|
+
result = { kind: "variable", name: prop.initializer.text };
|
|
722
|
+
} else if (isInlineBuilder(prop.initializer)) {
|
|
723
|
+
result = {
|
|
724
|
+
kind: "inline",
|
|
725
|
+
block: prop.initializer.getText(sourceFile)
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
ts5.forEachChild(node, visit);
|
|
735
|
+
}
|
|
736
|
+
visit(sourceFile);
|
|
737
|
+
return result;
|
|
738
|
+
}
|
|
739
|
+
function extractBasenameFromRouter(code) {
|
|
740
|
+
const sourceFile = ts5.createSourceFile(
|
|
741
|
+
"router.tsx",
|
|
742
|
+
code,
|
|
743
|
+
ts5.ScriptTarget.Latest,
|
|
744
|
+
true,
|
|
745
|
+
ts5.ScriptKind.TSX
|
|
746
|
+
);
|
|
747
|
+
let result;
|
|
748
|
+
function visit(node) {
|
|
749
|
+
if (result !== void 0) return;
|
|
750
|
+
if (ts5.isCallExpression(node) && ts5.isIdentifier(node.expression) && node.expression.text === "createRouter") {
|
|
751
|
+
for (const arg of node.arguments) {
|
|
703
752
|
if (ts5.isObjectLiteralExpression(arg)) {
|
|
704
753
|
for (const prop of arg.properties) {
|
|
705
|
-
if (ts5.isPropertyAssignment(prop) && ts5.isIdentifier(prop.name) && prop.name.text === "
|
|
754
|
+
if (ts5.isPropertyAssignment(prop) && ts5.isIdentifier(prop.name) && prop.name.text === "basename" && ts5.isStringLiteral(prop.initializer)) {
|
|
706
755
|
result = prop.initializer.text;
|
|
707
756
|
return;
|
|
708
757
|
}
|
|
@@ -715,6 +764,19 @@ function extractUrlsVariableFromRouter(code) {
|
|
|
715
764
|
visit(sourceFile);
|
|
716
765
|
return result;
|
|
717
766
|
}
|
|
767
|
+
function applyBasenameToRoutes(result, basename2) {
|
|
768
|
+
const prefixed = {};
|
|
769
|
+
for (const [name, pattern] of Object.entries(result.routes)) {
|
|
770
|
+
if (pattern === "/") {
|
|
771
|
+
prefixed[name] = basename2;
|
|
772
|
+
} else if (basename2.endsWith("/") && pattern.startsWith("/")) {
|
|
773
|
+
prefixed[name] = basename2 + pattern.slice(1);
|
|
774
|
+
} else {
|
|
775
|
+
prefixed[name] = basename2 + pattern;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return { routes: prefixed, searchSchemas: result.searchSchemas };
|
|
779
|
+
}
|
|
718
780
|
function buildCombinedRouteMapForRouterFile(routerFilePath) {
|
|
719
781
|
let routerSource;
|
|
720
782
|
try {
|
|
@@ -722,19 +784,40 @@ function buildCombinedRouteMapForRouterFile(routerFilePath) {
|
|
|
722
784
|
} catch {
|
|
723
785
|
return { routes: {}, searchSchemas: {} };
|
|
724
786
|
}
|
|
725
|
-
const
|
|
726
|
-
if (!
|
|
787
|
+
const extraction = extractUrlsFromRouter(routerSource);
|
|
788
|
+
if (!extraction) {
|
|
727
789
|
return { routes: {}, searchSchemas: {} };
|
|
728
790
|
}
|
|
729
|
-
const
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
791
|
+
const rawBasename = extractBasenameFromRouter(routerSource);
|
|
792
|
+
const basename2 = rawBasename ? ("/" + rawBasename.replace(/^\/+|\/+$/g, "")).replace(/^\/$/, "") : void 0;
|
|
793
|
+
let result;
|
|
794
|
+
if (extraction.kind === "inline") {
|
|
795
|
+
result = buildCombinedRouteMapWithSearch(
|
|
796
|
+
routerFilePath,
|
|
797
|
+
void 0,
|
|
798
|
+
void 0,
|
|
799
|
+
void 0,
|
|
800
|
+
extraction.block
|
|
801
|
+
);
|
|
802
|
+
} else {
|
|
803
|
+
const imported = resolveImportedVariable(routerSource, extraction.name);
|
|
804
|
+
if (imported) {
|
|
805
|
+
const targetFile = resolveImportPath(imported.specifier, routerFilePath);
|
|
806
|
+
if (!targetFile) {
|
|
807
|
+
return { routes: {}, searchSchemas: {} };
|
|
808
|
+
}
|
|
809
|
+
result = buildCombinedRouteMapWithSearch(
|
|
810
|
+
targetFile,
|
|
811
|
+
imported.exportedName
|
|
812
|
+
);
|
|
813
|
+
} else {
|
|
814
|
+
result = buildCombinedRouteMapWithSearch(routerFilePath, extraction.name);
|
|
734
815
|
}
|
|
735
|
-
return buildCombinedRouteMapWithSearch(targetFile, imported.exportedName);
|
|
736
816
|
}
|
|
737
|
-
|
|
817
|
+
if (basename2) {
|
|
818
|
+
result = applyBasenameToRoutes(result, basename2);
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
738
821
|
}
|
|
739
822
|
function detectUnresolvableIncludes(routerFilePath) {
|
|
740
823
|
const realPath = resolve2(routerFilePath);
|
|
@@ -744,9 +827,20 @@ function detectUnresolvableIncludes(routerFilePath) {
|
|
|
744
827
|
} catch {
|
|
745
828
|
return [];
|
|
746
829
|
}
|
|
747
|
-
const
|
|
748
|
-
if (!
|
|
749
|
-
const
|
|
830
|
+
const extraction = extractUrlsFromRouter(source);
|
|
831
|
+
if (!extraction) return [];
|
|
832
|
+
const diagnostics = [];
|
|
833
|
+
if (extraction.kind === "inline") {
|
|
834
|
+
buildCombinedRouteMapWithSearch(
|
|
835
|
+
realPath,
|
|
836
|
+
void 0,
|
|
837
|
+
/* @__PURE__ */ new Set(),
|
|
838
|
+
diagnostics,
|
|
839
|
+
extraction.block
|
|
840
|
+
);
|
|
841
|
+
return diagnostics;
|
|
842
|
+
}
|
|
843
|
+
const imported = resolveImportedVariable(source, extraction.name);
|
|
750
844
|
let targetFile;
|
|
751
845
|
let exportedName;
|
|
752
846
|
if (imported) {
|
|
@@ -766,9 +860,8 @@ function detectUnresolvableIncludes(routerFilePath) {
|
|
|
766
860
|
exportedName = imported.exportedName;
|
|
767
861
|
} else {
|
|
768
862
|
targetFile = realPath;
|
|
769
|
-
exportedName =
|
|
863
|
+
exportedName = extraction.name;
|
|
770
864
|
}
|
|
771
|
-
const diagnostics = [];
|
|
772
865
|
buildCombinedRouteMapWithSearch(
|
|
773
866
|
targetFile,
|
|
774
867
|
exportedName,
|
|
@@ -816,25 +909,15 @@ function writeCombinedRouteTypes(root, knownRouterFiles, opts) {
|
|
|
816
909
|
throw new Error(formatNestedRouterConflictError(nestedRouterConflict));
|
|
817
910
|
}
|
|
818
911
|
for (const routerFilePath of routerFilePaths) {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
routerSource
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
const imported = resolveImportedVariable(routerSource, urlsVarName);
|
|
829
|
-
if (imported) {
|
|
830
|
-
const targetFile = resolveImportPath(imported.specifier, routerFilePath);
|
|
831
|
-
if (!targetFile) continue;
|
|
832
|
-
result = buildCombinedRouteMapWithSearch(
|
|
833
|
-
targetFile,
|
|
834
|
-
imported.exportedName
|
|
835
|
-
);
|
|
836
|
-
} else {
|
|
837
|
-
result = buildCombinedRouteMapWithSearch(routerFilePath, urlsVarName);
|
|
912
|
+
const result = buildCombinedRouteMapForRouterFile(routerFilePath);
|
|
913
|
+
if (Object.keys(result.routes).length === 0 && Object.keys(result.searchSchemas).length === 0) {
|
|
914
|
+
let routerSource;
|
|
915
|
+
try {
|
|
916
|
+
routerSource = readFileSync3(routerFilePath, "utf-8");
|
|
917
|
+
} catch {
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
if (!extractUrlsFromRouter(routerSource)) continue;
|
|
838
921
|
}
|
|
839
922
|
const routerBasename = pathBasename(routerFilePath).replace(
|
|
840
923
|
/\.(tsx?|jsx?)$/,
|