@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.89 → 3.2.0-ultramodern.90

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.
Files changed (43) hide show
  1. package/dist/cjs/cli/index.js +12 -0
  2. package/dist/cjs/cli/routeSplitting.js +83 -0
  3. package/dist/cjs/cli/tanstackTypes.js +45 -7
  4. package/dist/cjs/runtime/index.js +12 -0
  5. package/dist/cjs/runtime/plugin.js +2 -0
  6. package/dist/cjs/runtime/plugin.node.js +2 -0
  7. package/dist/cjs/runtime/routeTree.js +8 -0
  8. package/dist/cjs/runtime/types.js +27 -1
  9. package/dist/esm/cli/index.mjs +4 -1
  10. package/dist/esm/cli/routeSplitting.mjs +43 -0
  11. package/dist/esm/cli/tanstackTypes.mjs +45 -7
  12. package/dist/esm/runtime/index.mjs +1 -0
  13. package/dist/esm/runtime/plugin.mjs +2 -0
  14. package/dist/esm/runtime/plugin.node.mjs +2 -0
  15. package/dist/esm/runtime/routeTree.mjs +8 -0
  16. package/dist/esm/runtime/types.mjs +7 -0
  17. package/dist/esm-node/cli/index.mjs +4 -1
  18. package/dist/esm-node/cli/routeSplitting.mjs +44 -0
  19. package/dist/esm-node/cli/tanstackTypes.mjs +45 -7
  20. package/dist/esm-node/runtime/index.mjs +1 -0
  21. package/dist/esm-node/runtime/plugin.mjs +2 -0
  22. package/dist/esm-node/runtime/plugin.node.mjs +2 -0
  23. package/dist/esm-node/runtime/routeTree.mjs +8 -0
  24. package/dist/esm-node/runtime/types.mjs +7 -0
  25. package/dist/types/cli/index.d.ts +4 -0
  26. package/dist/types/cli/routeSplitting.d.ts +29 -0
  27. package/dist/types/runtime/index.d.ts +2 -0
  28. package/dist/types/runtime/plugin.d.ts +1 -1
  29. package/dist/types/runtime/plugin.node.d.ts +1 -1
  30. package/dist/types/runtime/types.d.ts +7 -0
  31. package/package.json +10 -10
  32. package/src/cli/index.ts +17 -0
  33. package/src/cli/routeSplitting.ts +81 -0
  34. package/src/cli/tanstackTypes.ts +102 -13
  35. package/src/runtime/index.tsx +5 -0
  36. package/src/runtime/plugin.node.tsx +6 -1
  37. package/src/runtime/plugin.tsx +5 -1
  38. package/src/runtime/routeTree.ts +12 -0
  39. package/src/runtime/types.ts +13 -0
  40. package/tests/router/cli.test.ts +239 -0
  41. package/tests/router/fastDefaults.test.ts +25 -0
  42. package/tests/router/routeTree.test.ts +87 -0
  43. package/tests/router/tanstackTypes.test.ts +120 -0
@@ -27,6 +27,8 @@ type TestRouteObject = RouteObject & {
27
27
  inValidSSRRoute?: boolean;
28
28
  isClientComponent?: boolean;
29
29
  lazyImport?: () => Promise<unknown>;
30
+ loaderDeps?: unknown;
31
+ validateSearch?: unknown;
30
32
  };
31
33
 
32
34
  type TestNestedRoute = NestedRoute & {
@@ -34,6 +36,8 @@ type TestNestedRoute = NestedRoute & {
34
36
  hasAction?: boolean;
35
37
  hasClientLoader?: boolean;
36
38
  hasLoader?: boolean;
39
+ loaderDeps?: unknown;
40
+ validateSearch?: unknown;
37
41
  };
38
42
 
39
43
  type ShouldRevalidateArgs = {
@@ -55,6 +59,8 @@ type TestRoute = {
55
59
  shouldReload?: (args: ShouldReloadArgs) => boolean | undefined;
56
60
  ssr?: boolean;
57
61
  staticData: Record<string, unknown>;
62
+ loaderDeps?: unknown;
63
+ validateSearch?: unknown;
58
64
  };
59
65
  };
60
66
 
@@ -149,6 +155,46 @@ describe('tanstack route tree from RouteObject[]', () => {
149
155
  expect(userMatch?.loaderData).toEqual({ id: '123' });
150
156
  });
151
157
 
158
+ test('preserves TanStack search contracts from RouteObject routes', () => {
159
+ const rootValidateSearch = (search: unknown) => ({ root: search });
160
+ const rootLoaderDeps = ({ search }: { search: unknown }) => ({ search });
161
+ const childValidateSearch = (search: unknown) => ({ child: search });
162
+ const childLoaderDeps = ({ search }: { search: unknown }) => ({ search });
163
+ const routes: TestRouteObject[] = [
164
+ {
165
+ id: 'root',
166
+ path: '/',
167
+ validateSearch: rootValidateSearch,
168
+ loaderDeps: rootLoaderDeps,
169
+ Component: () => null,
170
+ children: [
171
+ {
172
+ id: 'search',
173
+ path: 'search',
174
+ validateSearch: childValidateSearch,
175
+ loaderDeps: childLoaderDeps,
176
+ Component: () => null,
177
+ },
178
+ ],
179
+ },
180
+ ];
181
+
182
+ const routeTree = createRouteTreeFromRouteObjects(routes);
183
+ const router = createRouter({
184
+ routeTree,
185
+ history: createMemoryHistory({
186
+ initialEntries: ['/search'],
187
+ }),
188
+ context: {},
189
+ }) as unknown as TestRouter;
190
+ const searchRoute = getLooseRoute(router, '/search');
191
+
192
+ expect(routeTree.options.validateSearch).toBe(rootValidateSearch);
193
+ expect(routeTree.options.loaderDeps).toBe(rootLoaderDeps);
194
+ expect(searchRoute.options.validateSearch).toBe(childValidateSearch);
195
+ expect(searchRoute.options.loaderDeps).toBe(childLoaderDeps);
196
+ });
197
+
152
198
  test('uses TanStack route ids when loading RSC payload route data', async () => {
153
199
  const rootLoader = rstest.fn(() => ({ source: 'modern-root' }));
154
200
  const userLoader = rstest.fn(() => ({ source: 'modern-user' }));
@@ -522,6 +568,47 @@ describe('tanstack route tree from RouteObject[]', () => {
522
568
  });
523
569
  });
524
570
 
571
+ test('preserves TanStack search contracts from Modern generated routes', () => {
572
+ const rootValidateSearch = (search: unknown) => ({ root: search });
573
+ const rootLoaderDeps = ({ search }: { search: unknown }) => ({ search });
574
+ const childValidateSearch = (search: unknown) => ({ child: search });
575
+ const childLoaderDeps = ({ search }: { search: unknown }) => ({ search });
576
+ const modernRoutes: TestNestedRoute[] = [
577
+ {
578
+ type: 'nested',
579
+ origin: 'config',
580
+ id: 'root',
581
+ isRoot: true,
582
+ validateSearch: rootValidateSearch,
583
+ loaderDeps: rootLoaderDeps,
584
+ children: [
585
+ {
586
+ type: 'nested',
587
+ origin: 'config',
588
+ id: 'search',
589
+ path: 'search',
590
+ validateSearch: childValidateSearch,
591
+ loaderDeps: childLoaderDeps,
592
+ },
593
+ ],
594
+ },
595
+ ];
596
+ const routeTree = createRouteTreeFromModernRoutes(modernRoutes);
597
+ const router = createRouter({
598
+ routeTree,
599
+ history: createMemoryHistory({
600
+ initialEntries: ['/search'],
601
+ }),
602
+ context: {},
603
+ }) as unknown as TestRouter;
604
+ const searchRoute = getLooseRouteByModernRouteId(router, 'search');
605
+
606
+ expect(routeTree.options.validateSearch).toBe(rootValidateSearch);
607
+ expect(routeTree.options.loaderDeps).toBe(rootLoaderDeps);
608
+ expect(searchRoute.options.validateSearch).toBe(childValidateSearch);
609
+ expect(searchRoute.options.loaderDeps).toBe(childLoaderDeps);
610
+ });
611
+
525
612
  test('preserves Modern generated client route metadata', () => {
526
613
  const modernRoutes: TestNestedRoute[] = [
527
614
  {
@@ -1,8 +1,12 @@
1
+ import { execFile } from 'node:child_process';
1
2
  import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
2
3
  import { tmpdir } from 'node:os';
3
4
  import path from 'node:path';
5
+ import { promisify } from 'node:util';
4
6
  import { generateTanstackRouterTypesSourceForEntry } from '../../src/cli/tanstackTypes';
5
7
 
8
+ const execFileAsync = promisify(execFile);
9
+
6
10
  describe('tanstack router type generation', () => {
7
11
  let tempDir: string | undefined;
8
12
 
@@ -62,6 +66,122 @@ describe('tanstack router type generation', () => {
62
66
  expect(routerGenTs).toContain(
63
67
  "} from '@modern-js/plugin-tanstack/runtime';",
64
68
  );
69
+ expect(routerGenTs).toContain('modernTanstackRouterFastDefaults,');
70
+ expect(routerGenTs).toContain('...modernTanstackRouterFastDefaults,');
71
+ });
72
+
73
+ test('typechecks generated TanStack search contracts', async () => {
74
+ tempDir = await mkdtemp(path.join(tmpdir(), 'modern-tanstack-types-'));
75
+ const srcDirectory = path.join(tempDir, 'src');
76
+ const routeDir = path.join(srcDirectory, 'routes');
77
+ const generatedDir = path.join(srcDirectory, 'modern-tanstack', 'index');
78
+ await mkdir(routeDir, { recursive: true });
79
+ await mkdir(generatedDir, { recursive: true });
80
+ await writeFile(
81
+ path.join(routeDir, 'search.contract.ts'),
82
+ [
83
+ 'export const validateSearch = (search: { q?: string }) => ({ q: search.q ?? "" });',
84
+ 'export const loaderDeps = ({ search }: { search: { q: string } }) => ({ q: search.q });',
85
+ ].join('\n'),
86
+ );
87
+
88
+ const { routerGenTs } = await generateTanstackRouterTypesSourceForEntry({
89
+ appContext: {
90
+ srcDirectory,
91
+ internalSrcAlias: '@/_',
92
+ } as any,
93
+ entryName: 'index',
94
+ routes: [
95
+ {
96
+ type: 'nested',
97
+ id: 'layout',
98
+ isRoot: true,
99
+ validateSearch: '@/_/routes/search.contract',
100
+ loaderDeps: '@/_/routes/search.contract',
101
+ children: [
102
+ {
103
+ type: 'nested',
104
+ id: 'search/page',
105
+ path: 'search',
106
+ validateSearch: '@/_/routes/search.contract',
107
+ loaderDeps: '@/_/routes/search.contract',
108
+ },
109
+ ],
110
+ },
111
+ ] as any,
112
+ });
113
+
114
+ await writeFile(path.join(generatedDir, 'router.gen.ts'), routerGenTs);
115
+ await writeFile(
116
+ path.join(srcDirectory, 'runtime-shim.d.ts'),
117
+ [
118
+ "declare module '@modern-js/plugin-tanstack/runtime' {",
119
+ ' type RouteOptions = {',
120
+ ' getParentRoute?: () => unknown;',
121
+ ' id?: string;',
122
+ ' loader?: unknown;',
123
+ ' loaderDeps?: unknown;',
124
+ ' path?: string;',
125
+ ' staticData?: unknown;',
126
+ ' validateSearch?: unknown;',
127
+ ' };',
128
+ ' type Route<TOptions extends RouteOptions> = {',
129
+ ' options: TOptions;',
130
+ ' addChildren<TChildren extends readonly unknown[]>(children: TChildren): Route<TOptions> & { children: TChildren };',
131
+ ' };',
132
+ ' export function createMemoryHistory(options: unknown): unknown;',
133
+ ' export const modernTanstackRouterFastDefaults: Record<string, unknown>;',
134
+ ' export function createRootRouteWithContext<TContext>(): <TOptions extends RouteOptions>(options: TOptions) => Route<TOptions>;',
135
+ ' export function createRoute<TOptions extends RouteOptions>(options: TOptions): Route<TOptions>;',
136
+ ' export function createRouter<TOptions extends Record<string, unknown>>(options: TOptions): TOptions;',
137
+ ' export function notFound(): never;',
138
+ ' export function redirect(options: unknown): never;',
139
+ '}',
140
+ ].join('\n'),
141
+ );
142
+ await writeFile(
143
+ path.join(srcDirectory, 'assert-search-contracts.ts'),
144
+ [
145
+ "import { rootRoute, routeTree } from './modern-tanstack/index/router.gen';",
146
+ "import { loaderDeps, validateSearch } from './routes/search.contract';",
147
+ '',
148
+ 'const rootValidateSearch: typeof validateSearch = rootRoute.options.validateSearch;',
149
+ 'const rootLoaderDeps: typeof loaderDeps = rootRoute.options.loaderDeps;',
150
+ 'const childValidateSearch: typeof validateSearch = routeTree.children[0].options.validateSearch;',
151
+ 'const childLoaderDeps: typeof loaderDeps = routeTree.children[0].options.loaderDeps;',
152
+ '',
153
+ "rootValidateSearch({ q: 'root' });",
154
+ "rootLoaderDeps({ search: { q: 'root' } });",
155
+ "childValidateSearch({ q: 'child' });",
156
+ "childLoaderDeps({ search: { q: 'child' } });",
157
+ ].join('\n'),
158
+ );
159
+ await writeFile(
160
+ path.join(tempDir, 'tsconfig.json'),
161
+ JSON.stringify(
162
+ {
163
+ compilerOptions: {
164
+ lib: ['ESNext', 'DOM'],
165
+ module: 'Preserve',
166
+ moduleResolution: 'Bundler',
167
+ noEmit: true,
168
+ skipLibCheck: true,
169
+ strict: true,
170
+ target: 'ESNext',
171
+ types: [],
172
+ },
173
+ include: ['src/**/*.ts', 'src/**/*.d.ts'],
174
+ },
175
+ null,
176
+ 2,
177
+ ),
178
+ );
179
+
180
+ await expect(
181
+ execFileAsync('tsgo', ['--noEmit', '-p', 'tsconfig.json'], {
182
+ cwd: tempDir,
183
+ }),
184
+ ).resolves.toBeDefined();
65
185
  });
66
186
 
67
187
  test('preserves typed child trees for localized nested route aliases', async () => {