@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.9 → 3.2.0-ultramodern.91

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 (48) 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 +146 -58
  4. package/dist/cjs/runtime/index.js +38 -6
  5. package/dist/cjs/runtime/plugin.js +6 -5
  6. package/dist/cjs/runtime/plugin.node.js +27 -10
  7. package/dist/cjs/runtime/plugin.worker.js +49 -0
  8. package/dist/cjs/runtime/routeTree.js +55 -4
  9. package/dist/cjs/runtime/types.js +27 -1
  10. package/dist/esm/cli/index.mjs +4 -1
  11. package/dist/esm/cli/routeSplitting.mjs +43 -0
  12. package/dist/esm/cli/tanstackTypes.mjs +146 -58
  13. package/dist/esm/runtime/index.mjs +2 -1
  14. package/dist/esm/runtime/plugin.mjs +10 -9
  15. package/dist/esm/runtime/plugin.node.mjs +28 -11
  16. package/dist/esm/runtime/plugin.worker.mjs +1 -0
  17. package/dist/esm/runtime/routeTree.mjs +55 -4
  18. package/dist/esm/runtime/types.mjs +7 -0
  19. package/dist/esm-node/cli/index.mjs +4 -1
  20. package/dist/esm-node/cli/routeSplitting.mjs +44 -0
  21. package/dist/esm-node/cli/tanstackTypes.mjs +146 -58
  22. package/dist/esm-node/runtime/index.mjs +2 -1
  23. package/dist/esm-node/runtime/plugin.mjs +10 -9
  24. package/dist/esm-node/runtime/plugin.node.mjs +28 -11
  25. package/dist/esm-node/runtime/plugin.worker.mjs +2 -0
  26. package/dist/esm-node/runtime/routeTree.mjs +55 -4
  27. package/dist/esm-node/runtime/types.mjs +7 -0
  28. package/dist/types/cli/index.d.ts +4 -0
  29. package/dist/types/cli/routeSplitting.d.ts +29 -0
  30. package/dist/types/runtime/index.d.ts +3 -1
  31. package/dist/types/runtime/plugin.d.ts +1 -1
  32. package/dist/types/runtime/plugin.node.d.ts +1 -1
  33. package/dist/types/runtime/plugin.worker.d.ts +1 -0
  34. package/dist/types/runtime/types.d.ts +7 -0
  35. package/package.json +14 -14
  36. package/src/cli/index.ts +17 -0
  37. package/src/cli/routeSplitting.ts +81 -0
  38. package/src/cli/tanstackTypes.ts +216 -67
  39. package/src/runtime/index.tsx +13 -1
  40. package/src/runtime/plugin.node.tsx +54 -7
  41. package/src/runtime/plugin.tsx +8 -5
  42. package/src/runtime/plugin.worker.tsx +4 -0
  43. package/src/runtime/routeTree.ts +125 -8
  44. package/src/runtime/types.ts +13 -0
  45. package/tests/router/cli.test.ts +239 -0
  46. package/tests/router/fastDefaults.test.ts +25 -0
  47. package/tests/router/routeTree.test.ts +193 -1
  48. package/tests/router/tanstackTypes.test.ts +184 -0
@@ -1,7 +1,10 @@
1
1
  import type { RouteObject } from '@modern-js/runtime-utils/router';
2
2
  import type { NestedRoute } from '@modern-js/types';
3
3
  import { createMemoryHistory } from '@tanstack/history';
4
- import { createRouter } from '@tanstack/react-router';
4
+ import { createRouter, Outlet, RouterProvider } from '@tanstack/react-router';
5
+ import type { ComponentType } from 'react';
6
+ import { createElement, lazy } from 'react';
7
+ import { renderToStaticMarkup } from 'react-dom/server';
5
8
  import {
6
9
  createRouteTreeFromModernRoutes,
7
10
  createRouteTreeFromRouteObjects,
@@ -23,6 +26,9 @@ type TestRouteObject = RouteObject & {
23
26
  hasLoader?: boolean;
24
27
  inValidSSRRoute?: boolean;
25
28
  isClientComponent?: boolean;
29
+ lazyImport?: () => Promise<unknown>;
30
+ loaderDeps?: unknown;
31
+ validateSearch?: unknown;
26
32
  };
27
33
 
28
34
  type TestNestedRoute = NestedRoute & {
@@ -30,6 +36,8 @@ type TestNestedRoute = NestedRoute & {
30
36
  hasAction?: boolean;
31
37
  hasClientLoader?: boolean;
32
38
  hasLoader?: boolean;
39
+ loaderDeps?: unknown;
40
+ validateSearch?: unknown;
33
41
  };
34
42
 
35
43
  type ShouldRevalidateArgs = {
@@ -51,6 +59,8 @@ type TestRoute = {
51
59
  shouldReload?: (args: ShouldReloadArgs) => boolean | undefined;
52
60
  ssr?: boolean;
53
61
  staticData: Record<string, unknown>;
62
+ loaderDeps?: unknown;
63
+ validateSearch?: unknown;
54
64
  };
55
65
  };
56
66
 
@@ -145,6 +155,46 @@ describe('tanstack route tree from RouteObject[]', () => {
145
155
  expect(userMatch?.loaderData).toEqual({ id: '123' });
146
156
  });
147
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
+
148
198
  test('uses TanStack route ids when loading RSC payload route data', async () => {
149
199
  const rootLoader = rstest.fn(() => ({ source: 'modern-root' }));
150
200
  const userLoader = rstest.fn(() => ({ source: 'modern-user' }));
@@ -234,6 +284,107 @@ describe('tanstack route tree from RouteObject[]', () => {
234
284
  expect(splatParamValue).toBe('a/b/c');
235
285
  });
236
286
 
287
+ test('preloads lazy Modern route components for server rendering', async () => {
288
+ const LazyRouteComponent = () =>
289
+ createElement('main', null, 'Lazy route ready');
290
+ const lazyImport = rstest.fn(async () => ({
291
+ default: LazyRouteComponent,
292
+ }));
293
+ const routes: TestRouteObject[] = [
294
+ {
295
+ id: 'root',
296
+ path: '/',
297
+ Component: () => null,
298
+ children: [
299
+ {
300
+ id: 'lazy',
301
+ path: 'lazy',
302
+ Component: lazy(lazyImport),
303
+ lazyImport,
304
+ },
305
+ ],
306
+ },
307
+ ];
308
+
309
+ const routeTree = createRouteTreeFromRouteObjects(routes);
310
+ const router = await loadRouteTree(routeTree, '/lazy');
311
+ const lazyRoute = getLooseRoute(router, '/lazy');
312
+ const component = lazyRoute.options.component as ComponentType & {
313
+ preload?: () => Promise<unknown>;
314
+ };
315
+
316
+ await component.preload?.();
317
+
318
+ expect(renderToStaticMarkup(createElement(component))).toContain(
319
+ 'Lazy route ready',
320
+ );
321
+ expect(lazyImport).toHaveBeenCalled();
322
+ });
323
+
324
+ test('renders preloaded lazy child routes through TanStack router SSR', async () => {
325
+ const LazyRouteComponent = () =>
326
+ createElement('main', null, 'Lazy child route ready');
327
+ const lazyImport = rstest.fn(async () => ({
328
+ default: LazyRouteComponent,
329
+ }));
330
+ const routes: TestRouteObject[] = [
331
+ {
332
+ id: 'root',
333
+ path: '/',
334
+ Component: () => createElement('section', null, createElement(Outlet)),
335
+ children: [
336
+ {
337
+ id: 'lazy',
338
+ path: 'lazy',
339
+ Component: lazy(lazyImport),
340
+ lazyImport,
341
+ },
342
+ ],
343
+ },
344
+ ];
345
+
346
+ const routeTree = createRouteTreeFromRouteObjects(routes);
347
+ const router = await loadRouteTree(routeTree, '/lazy');
348
+
349
+ expect(
350
+ renderToStaticMarkup(createElement(RouterProvider, { router } as never)),
351
+ ).toContain('Lazy child route ready');
352
+ });
353
+
354
+ test('unwraps nested ESM route module defaults before server rendering', async () => {
355
+ const LazyRouteComponent = () =>
356
+ createElement('main', null, 'Nested lazy child route ready');
357
+ const lazyImport = rstest.fn(async () => ({
358
+ default: {
359
+ default: LazyRouteComponent,
360
+ },
361
+ }));
362
+ const routes: TestRouteObject[] = [
363
+ {
364
+ id: 'root',
365
+ path: '/',
366
+ Component: () => createElement('section', null, createElement(Outlet)),
367
+ children: [
368
+ {
369
+ id: 'lazy',
370
+ path: 'lazy',
371
+ Component: lazy(
372
+ lazyImport as () => Promise<{ default: ComponentType }>,
373
+ ),
374
+ lazyImport,
375
+ },
376
+ ],
377
+ },
378
+ ];
379
+
380
+ const routeTree = createRouteTreeFromRouteObjects(routes);
381
+ const router = await loadRouteTree(routeTree, '/lazy');
382
+
383
+ expect(
384
+ renderToStaticMarkup(createElement(RouterProvider, { router } as never)),
385
+ ).toContain('Nested lazy child route ready');
386
+ });
387
+
237
388
  test('preserves route handle and maps shouldRevalidate to shouldReload', async () => {
238
389
  const shouldRevalidate = rstest.fn(({ nextUrl }: ShouldRevalidateArgs) =>
239
390
  nextUrl.pathname.endsWith('/456'),
@@ -417,6 +568,47 @@ describe('tanstack route tree from RouteObject[]', () => {
417
568
  });
418
569
  });
419
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
+
420
612
  test('preserves Modern generated client route metadata', () => {
421
613
  const modernRoutes: TestNestedRoute[] = [
422
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
 
@@ -55,8 +59,188 @@ describe('tanstack router type generation', () => {
55
59
  );
56
60
  expect(routerGenTs).toContain('modernRouteLoader: loader_0');
57
61
  expect(routerGenTs).toContain('modernRouteAction: action_0');
62
+ expect(routerGenTs).toContain('modernRouteId?: string;');
63
+ expect(routerGenTs).not.toContain(
64
+ 'return Object.keys(staticData).length > 0 ? staticData : undefined;',
65
+ );
58
66
  expect(routerGenTs).toContain(
59
67
  "} from '@modern-js/plugin-tanstack/runtime';",
60
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();
185
+ });
186
+
187
+ test('preserves typed child trees for localized nested route aliases', async () => {
188
+ tempDir = await mkdtemp(path.join(tmpdir(), 'modern-tanstack-types-'));
189
+ const srcDirectory = path.join(tempDir, 'src');
190
+
191
+ const { routerGenTs } = await generateTanstackRouterTypesSourceForEntry({
192
+ appContext: {
193
+ srcDirectory,
194
+ internalSrcAlias: '@/_',
195
+ } as any,
196
+ entryName: 'index',
197
+ routes: [
198
+ {
199
+ type: 'nested',
200
+ id: 'layout',
201
+ isRoot: true,
202
+ children: [
203
+ {
204
+ type: 'nested',
205
+ id: '(lang)/layout',
206
+ path: ':lang',
207
+ children: [
208
+ {
209
+ type: 'nested',
210
+ id: '(lang)/products/(slug)/page',
211
+ path: 'products/:slug',
212
+ },
213
+ {
214
+ type: 'nested',
215
+ id: '(lang)/products/(slug)/page__localised_produkty_slug',
216
+ path: 'produkty/:slug',
217
+ },
218
+ {
219
+ type: 'nested',
220
+ id: '(lang)/optional/(slug$)/page__localised_volitelne_slug',
221
+ path: 'volitelne/:slug?',
222
+ },
223
+ ],
224
+ },
225
+ ],
226
+ },
227
+ ] as any,
228
+ });
229
+
230
+ expect(routerGenTs).toContain(
231
+ 'const route__lang__layout__base = createRoute({',
232
+ );
233
+ expect(routerGenTs).toContain(
234
+ 'getParentRoute: () => route__lang__layout__base,',
235
+ );
236
+ expect(routerGenTs).toContain('path: "produkty/$slug",');
237
+ expect(routerGenTs).toContain('path: "volitelne/{-$slug}",');
238
+ expect(routerGenTs).toContain(
239
+ 'const route__lang__layout = route__lang__layout__base.addChildren([route__lang__products__slug__page, route__lang__products__slug__page__localised_produkty_slug, route__lang__optional__slug$__page__localised_volitelne_slug]);',
240
+ );
241
+ expect(routerGenTs).toContain(
242
+ 'export const routeTree = rootRoute.addChildren([route__lang__layout]);',
243
+ );
244
+ expect(routerGenTs).not.toContain('route__lang__layout.addChildren([');
61
245
  });
62
246
  });