@vaadin/hilla-file-router 24.4.0-alpha13 → 24.4.0-alpha15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/hilla-file-router",
3
- "version": "24.4.0-alpha13",
3
+ "version": "24.4.0-alpha15",
4
4
  "description": "Hilla file-based router",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -80,7 +80,7 @@
80
80
  "type-fest": "^4.9.0"
81
81
  },
82
82
  "dependencies": {
83
- "@vaadin/hilla-generator-utils": "24.4.0-alpha13",
83
+ "@vaadin/hilla-generator-utils": "24.4.0-alpha15",
84
84
  "react": "^18.2.0",
85
85
  "rollup": "^4.12.0",
86
86
  "typescript": "5.3.2"
@@ -1,4 +1,4 @@
1
- import type { AgnosticRoute } from '../types.js';
1
+ import type { AgnosticRoute, Module } from '../types.js';
2
2
  /**
3
3
  * Create a single framework-agnostic route object. Later, it can be transformed into a framework-specific route object,
4
4
  * e.g., the one used by React Router.
@@ -8,6 +8,6 @@ import type { AgnosticRoute } from '../types.js';
8
8
  *
9
9
  * @returns A framework-agnostic route object.
10
10
  */
11
- export declare function createRoute<C = unknown>(path: string, children?: ReadonlyArray<AgnosticRoute<C>>): AgnosticRoute<C>;
12
- export declare function createRoute<C = unknown>(path: string, module: Readonly<Record<string, unknown>>, children?: ReadonlyArray<AgnosticRoute<C>>): AgnosticRoute<C>;
11
+ export declare function createRoute(path: string, children?: readonly AgnosticRoute[]): AgnosticRoute;
12
+ export declare function createRoute(path: string, module: Module, children?: readonly AgnosticRoute[]): AgnosticRoute;
13
13
  //# sourceMappingURL=createRoute.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createRoute.d.ts","sourceRoot":"","sources":["../src/runtime/createRoute.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAe,MAAM,aAAa,CAAC;AAE9D;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AACrH,wBAAgB,WAAW,CAAC,CAAC,GAAG,OAAO,EACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EACzC,QAAQ,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GACzC,aAAa,CAAC,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"createRoute.d.ts","sourceRoot":"","sources":["../src/runtime/createRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEzD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,SAAS,aAAa,EAAE,GAAG,aAAa,CAAC;AAC9F,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,SAAS,aAAa,EAAE,GAAG,aAAa,CAAC"}
@@ -6,7 +6,7 @@ function createRoute(path, moduleOrChildren, children) {
6
6
  module = moduleOrChildren;
7
7
  }
8
8
  return {
9
- path: module?.config?.route ?? path,
9
+ path,
10
10
  module,
11
11
  children
12
12
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/runtime/createRoute.ts"],
4
- "sourcesContent": ["/* eslint-disable no-param-reassign */\nimport type { AgnosticRoute, RouteModule } from '../types.js';\n\n/**\n * Create a single framework-agnostic route object. Later, it can be transformed into a framework-specific route object,\n * e.g., the one used by React Router.\n *\n * @param path - A route path segment.\n * @param children - An array of child routes.\n *\n * @returns A framework-agnostic route object.\n */\nexport function createRoute<C = unknown>(path: string, children?: ReadonlyArray<AgnosticRoute<C>>): AgnosticRoute<C>;\nexport function createRoute<C = unknown>(\n path: string,\n module: Readonly<Record<string, unknown>>,\n children?: ReadonlyArray<AgnosticRoute<C>>,\n): AgnosticRoute<C>;\nexport function createRoute<C = unknown>(\n path: string,\n moduleOrChildren?: Readonly<Record<string, unknown>> | ReadonlyArray<AgnosticRoute<C>>,\n children?: ReadonlyArray<AgnosticRoute<C>>,\n): AgnosticRoute<C> {\n let module: RouteModule<C> | undefined;\n if (Array.isArray(moduleOrChildren)) {\n children = moduleOrChildren;\n } else {\n module = moduleOrChildren as RouteModule<C> | undefined;\n }\n\n return {\n path: module?.config?.route ?? path,\n module,\n children,\n };\n}\n"],
5
- "mappings": "AAkBO,SAAS,YACd,MACA,kBACA,UACkB;AAClB,MAAI;AACJ,MAAI,MAAM,QAAQ,gBAAgB,GAAG;AACnC,eAAW;AAAA,EACb,OAAO;AACL,aAAS;AAAA,EACX;AAEA,SAAO;AAAA,IACL,MAAM,QAAQ,QAAQ,SAAS;AAAA,IAC/B;AAAA,IACA;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { AgnosticRoute, Module } from '../types.js';\n\n/**\n * Create a single framework-agnostic route object. Later, it can be transformed into a framework-specific route object,\n * e.g., the one used by React Router.\n *\n * @param path - A route path segment.\n * @param children - An array of child routes.\n *\n * @returns A framework-agnostic route object.\n */\nexport function createRoute(path: string, children?: readonly AgnosticRoute[]): AgnosticRoute;\nexport function createRoute(path: string, module: Module, children?: readonly AgnosticRoute[]): AgnosticRoute;\nexport function createRoute(\n path: string,\n moduleOrChildren?: Module | readonly AgnosticRoute[],\n children?: readonly AgnosticRoute[],\n): AgnosticRoute {\n let module: Module | undefined;\n if (Array.isArray(moduleOrChildren)) {\n // eslint-disable-next-line no-param-reassign\n children = moduleOrChildren;\n } else {\n module = moduleOrChildren as Module | undefined;\n }\n\n return {\n path,\n module,\n children,\n };\n}\n"],
5
+ "mappings": "AAaO,SAAS,YACd,MACA,kBACA,UACe;AACf,MAAI;AACJ,MAAI,MAAM,QAAQ,gBAAgB,GAAG;AAEnC,eAAW;AAAA,EACb,OAAO;AACL,aAAS;AAAA,EACX;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -1,5 +1,4 @@
1
- import { type ComponentType } from 'react';
2
- import type { RouteObject } from 'react-router-dom';
1
+ import type { RouteObject as ReactRouteObject } from 'react-router-dom';
3
2
  import type { AgnosticRoute } from '../types.js';
4
3
  /**
5
4
  * Transforms framework-agnostic route tree into a format that can be used by React Router.
@@ -8,5 +7,5 @@ import type { AgnosticRoute } from '../types.js';
8
7
  *
9
8
  * @returns The React Router tree.
10
9
  */
11
- export declare function toReactRouter(routes: AgnosticRoute<ComponentType>): RouteObject;
10
+ export declare function toReactRouter(routes: AgnosticRoute): ReactRouteObject;
12
11
  //# sourceMappingURL=toReactRouter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toReactRouter.d.ts","sourceRoot":"","sources":["../src/runtime/toReactRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAiB,MAAM,OAAO,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAGjD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,CAAC,aAAa,CAAC,GAAG,WAAW,CAkB/E"}
1
+ {"version":3,"file":"toReactRouter.d.ts","sourceRoot":"","sources":["../src/runtime/toReactRouter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAExE,OAAO,KAAK,EAAE,aAAa,EAAuB,MAAM,aAAa,CAAC;AAOtE;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,gBAAgB,CAsBrE"}
@@ -1,14 +1,20 @@
1
1
  import { createElement } from "react";
2
2
  import { convertComponentNameToTitle } from "../shared/convertComponentNameToTitle.js";
3
3
  import { transformRoute } from "./utils.js";
4
+ function isReactRouteModule(module) {
5
+ return module ? "default" in module && typeof module.default === "function" : true;
6
+ }
4
7
  function toReactRouter(routes) {
5
8
  return transformRoute(
6
9
  routes,
7
10
  (route) => route.children?.values(),
8
11
  ({ path, module }, children) => {
12
+ if (!isReactRouteModule(module)) {
13
+ throw new Error(`The module for the "${path}" section doesn't have the React component exported by default`);
14
+ }
9
15
  const title = module?.config?.title ?? convertComponentNameToTitle(module?.default);
10
16
  return {
11
- path,
17
+ path: module?.config?.route ?? path,
12
18
  element: module?.default ? createElement(module.default) : void 0,
13
19
  children: children.length > 0 ? children : void 0,
14
20
  handle: {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/runtime/toReactRouter.ts"],
4
- "sourcesContent": ["import { type ComponentType, createElement } from 'react';\nimport type { RouteObject } from 'react-router-dom';\nimport { convertComponentNameToTitle } from '../shared/convertComponentNameToTitle.js';\nimport type { AgnosticRoute } from '../types.js';\nimport { transformRoute } from './utils.js';\n\n/**\n * Transforms framework-agnostic route tree into a format that can be used by React Router.\n *\n * @param routes - Generated routes\n *\n * @returns The React Router tree.\n */\nexport function toReactRouter(routes: AgnosticRoute<ComponentType>): RouteObject {\n return transformRoute(\n routes,\n (route) => route.children?.values(),\n ({ path, module }, children) => {\n const title = module?.config?.title ?? convertComponentNameToTitle(module?.default);\n\n return {\n path,\n element: module?.default ? createElement(module.default) : undefined,\n children: children.length > 0 ? (children as RouteObject[]) : undefined,\n handle: {\n ...module?.config,\n title,\n },\n } satisfies RouteObject;\n },\n );\n}\n"],
5
- "mappings": "AAAA,SAA6B,qBAAqB;AAElD,SAAS,mCAAmC;AAE5C,SAAS,sBAAsB;AASxB,SAAS,cAAc,QAAmD;AAC/E,SAAO;AAAA,IACL;AAAA,IACA,CAAC,UAAU,MAAM,UAAU,OAAO;AAAA,IAClC,CAAC,EAAE,MAAM,OAAO,GAAG,aAAa;AAC9B,YAAM,QAAQ,QAAQ,QAAQ,SAAS,4BAA4B,QAAQ,OAAO;AAElF,aAAO;AAAA,QACL;AAAA,QACA,SAAS,QAAQ,UAAU,cAAc,OAAO,OAAO,IAAI;AAAA,QAC3D,UAAU,SAAS,SAAS,IAAK,WAA6B;AAAA,QAC9D,QAAQ;AAAA,UACN,GAAG,QAAQ;AAAA,UACX;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { type ComponentType, createElement } from 'react';\nimport type { RouteObject as ReactRouteObject } from 'react-router-dom';\nimport { convertComponentNameToTitle } from '../shared/convertComponentNameToTitle.js';\nimport type { AgnosticRoute, Module, RouteModule } from '../types.js';\nimport { transformRoute } from './utils.js';\n\nfunction isReactRouteModule(module?: Module): module is RouteModule<ComponentType> | undefined {\n return module ? 'default' in module && typeof module.default === 'function' : true;\n}\n\n/**\n * Transforms framework-agnostic route tree into a format that can be used by React Router.\n *\n * @param routes - Generated routes\n *\n * @returns The React Router tree.\n */\nexport function toReactRouter(routes: AgnosticRoute): ReactRouteObject {\n return transformRoute(\n routes,\n (route) => route.children?.values(),\n ({ path, module }, children) => {\n if (!isReactRouteModule(module)) {\n throw new Error(`The module for the \"${path}\" section doesn't have the React component exported by default`);\n }\n\n const title = module?.config?.title ?? convertComponentNameToTitle(module?.default);\n\n return {\n path: module?.config?.route ?? path,\n element: module?.default ? createElement(module.default) : undefined,\n children: children.length > 0 ? (children as ReactRouteObject[]) : undefined,\n handle: {\n ...module?.config,\n title,\n },\n } satisfies ReactRouteObject;\n },\n );\n}\n"],
5
+ "mappings": "AAAA,SAA6B,qBAAqB;AAElD,SAAS,mCAAmC;AAE5C,SAAS,sBAAsB;AAE/B,SAAS,mBAAmB,QAAmE;AAC7F,SAAO,SAAS,aAAa,UAAU,OAAO,OAAO,YAAY,aAAa;AAChF;AASO,SAAS,cAAc,QAAyC;AACrE,SAAO;AAAA,IACL;AAAA,IACA,CAAC,UAAU,MAAM,UAAU,OAAO;AAAA,IAClC,CAAC,EAAE,MAAM,OAAO,GAAG,aAAa;AAC9B,UAAI,CAAC,mBAAmB,MAAM,GAAG;AAC/B,cAAM,IAAI,MAAM,uBAAuB,IAAI,gEAAgE;AAAA,MAC7G;AAEA,YAAM,QAAQ,QAAQ,QAAQ,SAAS,4BAA4B,QAAQ,OAAO;AAElF,aAAO;AAAA,QACL,MAAM,QAAQ,QAAQ,SAAS;AAAA,QAC/B,SAAS,QAAQ,UAAU,cAAc,OAAO,OAAO,IAAI;AAAA,QAC3D,UAAU,SAAS,SAAS,IAAK,WAAkC;AAAA,QACnE,QAAQ;AAAA,UACN,GAAG,QAAQ;AAAA,UACX;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
package/types.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import type { RouteParamType } from './shared/routeParamType.js';
2
-
3
1
  export type ViewConfig = Readonly<{
4
2
  /**
5
3
  * View title used in the main layout header, as <title> and as the default
@@ -45,20 +43,26 @@ export type ViewConfig = Readonly<{
45
43
  }>;
46
44
  }>;
47
45
 
46
+ /**
47
+ * An unknown JavaScript module.
48
+ */
49
+ export type Module = Readonly<Record<string, unknown>>;
50
+
48
51
  /**
49
52
  * A module that exports a component and an optional view configuration.
50
53
  */
51
- export type RouteModule<C = unknown> = Readonly<{
52
- default: C;
53
- config?: ViewConfig;
54
- }>;
54
+ export type RouteModule<C = unknown> = Module &
55
+ Readonly<{
56
+ default: C;
57
+ config?: ViewConfig;
58
+ }>;
55
59
 
56
60
  /**
57
61
  * A framework-agnostic object generated from the file-based route.
58
62
  */
59
- export type AgnosticRoute<C = unknown> = Readonly<{
63
+ export type AgnosticRoute = Readonly<{
60
64
  path: string;
61
- module?: RouteModule<C>;
65
+ module?: Module;
62
66
  children?: ReadonlyArray<AgnosticRoute<T>>;
63
67
  }>;
64
68