piral-core 1.0.2 → 1.1.0-beta.5756

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 (72) hide show
  1. package/app.codegen.d.ts +1 -1
  2. package/dist/codegen.js +60 -24
  3. package/esm/Piral.js +1 -2
  4. package/esm/Piral.js.map +1 -1
  5. package/esm/components/PiralRoutes.js +10 -1
  6. package/esm/components/PiralRoutes.js.map +1 -1
  7. package/esm/components/wrapComponent.js +1 -2
  8. package/esm/components/wrapComponent.js.map +1 -1
  9. package/esm/defaults/DefaultErrorInfo.js +2 -1
  10. package/esm/defaults/DefaultErrorInfo.js.map +1 -1
  11. package/esm/defaults/navigator_none.d.ts +1 -1
  12. package/esm/defaults/navigator_none.js +2 -1
  13. package/esm/defaults/navigator_none.js.map +1 -1
  14. package/esm/defaults/navigator_v5.d.ts +1 -1
  15. package/esm/defaults/navigator_v5.js +2 -1
  16. package/esm/defaults/navigator_v5.js.map +1 -1
  17. package/esm/defaults/navigator_v6.d.ts +1 -1
  18. package/esm/defaults/navigator_v6.js +2 -1
  19. package/esm/defaults/navigator_v6.js.map +1 -1
  20. package/esm/hooks/index.d.ts +0 -1
  21. package/esm/hooks/index.js +0 -1
  22. package/esm/hooks/index.js.map +1 -1
  23. package/esm/state/createActions.js +2 -2
  24. package/esm/state/createActions.js.map +1 -1
  25. package/esm/tools/codegen.js +51 -13
  26. package/esm/tools/codegen.js.map +1 -1
  27. package/esm/types/navigation.d.ts +4 -0
  28. package/lib/Piral.js +1 -2
  29. package/lib/Piral.js.map +1 -1
  30. package/lib/components/PiralRoutes.js +10 -1
  31. package/lib/components/PiralRoutes.js.map +1 -1
  32. package/lib/components/wrapComponent.js +1 -2
  33. package/lib/components/wrapComponent.js.map +1 -1
  34. package/lib/defaults/DefaultErrorInfo.js +3 -2
  35. package/lib/defaults/DefaultErrorInfo.js.map +1 -1
  36. package/lib/defaults/navigator_none.d.ts +1 -1
  37. package/lib/defaults/navigator_none.js +2 -1
  38. package/lib/defaults/navigator_none.js.map +1 -1
  39. package/lib/defaults/navigator_v5.d.ts +1 -1
  40. package/lib/defaults/navigator_v5.js +2 -1
  41. package/lib/defaults/navigator_v5.js.map +1 -1
  42. package/lib/defaults/navigator_v6.d.ts +1 -1
  43. package/lib/defaults/navigator_v6.js +2 -1
  44. package/lib/defaults/navigator_v6.js.map +1 -1
  45. package/lib/hooks/index.d.ts +0 -1
  46. package/lib/hooks/index.js +0 -1
  47. package/lib/hooks/index.js.map +1 -1
  48. package/lib/state/createActions.js +1 -1
  49. package/lib/state/createActions.js.map +1 -1
  50. package/lib/tools/codegen.js +51 -13
  51. package/lib/tools/codegen.js.map +1 -1
  52. package/lib/types/navigation.d.ts +4 -0
  53. package/package.json +4 -4
  54. package/src/Piral.tsx +1 -2
  55. package/src/components/PiralRoutes.test.tsx +13 -22
  56. package/src/components/PiralRoutes.tsx +14 -2
  57. package/src/components/wrapComponent.tsx +1 -2
  58. package/src/defaults/DefaultErrorInfo.tsx +2 -1
  59. package/src/defaults/navigator_none.tsx +2 -1
  60. package/src/defaults/navigator_v5.tsx +2 -1
  61. package/src/defaults/navigator_v6.tsx +2 -1
  62. package/src/hooks/index.ts +0 -1
  63. package/src/state/createActions.ts +2 -2
  64. package/src/tools/codegen.ts +59 -13
  65. package/src/types/navigation.ts +4 -0
  66. package/esm/hooks/routes.d.ts +0 -2
  67. package/esm/hooks/routes.js +0 -11
  68. package/esm/hooks/routes.js.map +0 -1
  69. package/lib/hooks/routes.d.ts +0 -2
  70. package/lib/hooks/routes.js +0 -15
  71. package/lib/hooks/routes.js.map +0 -1
  72. package/src/hooks/routes.ts +0 -14
@@ -1,12 +1,42 @@
1
1
  // this file is bundled, so the references here will not be at runtime (i.e., for a user)
2
2
  import { getModulePath } from 'piral-cli/src/external/resolve';
3
+ import { readFileSync, existsSync } from 'fs';
4
+ import { resolve, dirname } from 'path';
5
+
6
+ function findPackagePath(moduleDir: string) {
7
+ const packageJson = 'package.json';
8
+ const packagePath = resolve(moduleDir, packageJson);
9
+
10
+ if (existsSync(packagePath)) {
11
+ return packagePath;
12
+ }
13
+
14
+ const newDir = resolve(moduleDir, '..');
15
+
16
+ if (newDir !== moduleDir) {
17
+ return findPackagePath(newDir);
18
+ }
19
+
20
+ return undefined;
21
+ }
22
+
23
+ function getPackageJson(root: string, packageName: string) {
24
+ const moduleDir = dirname(getModulePath(root, packageName));
25
+
26
+ try {
27
+ const packagePath = findPackagePath(moduleDir);
28
+ const content = readFileSync(packagePath, 'utf8');
29
+ return JSON.parse(content) || {};
30
+ } catch {
31
+ return {};
32
+ }
33
+ }
3
34
 
4
35
  function getRouterVersion(root: string) {
5
36
  const router = 'react-router';
6
37
 
7
38
  try {
8
- const modulePath = getModulePath(root, `${router}/package.json`);
9
- const { version } = require(modulePath);
39
+ const { version } = getPackageJson(root, router);
10
40
  const [major] = version.split('.');
11
41
  return parseInt(major, 10);
12
42
  } catch {
@@ -16,17 +46,15 @@ function getRouterVersion(root: string) {
16
46
  }
17
47
 
18
48
  function getIdentifiers(root: string, packageName: string) {
19
- const packageJson = `${packageName}/package.json`;
20
49
  const identifiers = [packageName];
21
50
 
22
51
  try {
23
- const modulePath = getModulePath(root, packageJson);
24
- const details = require(modulePath);
52
+ const details = getPackageJson(root, packageName);
25
53
 
26
54
  if (details.version) {
27
55
  identifiers.push(`${packageName}@${details.version}`);
28
56
 
29
- if (details.name !== packageName) {
57
+ if (details.name && details.name !== packageName) {
30
58
  identifiers.push(`${details.name}@${details.version}`);
31
59
  }
32
60
  }
@@ -63,22 +91,40 @@ interface CodegenOptions {
63
91
  export function createDependencies(imports: Array<string>, exports: Array<string>, opts: CodegenOptions) {
64
92
  const { root, appName, externals } = opts;
65
93
  const assignments: Array<string> = [];
94
+ const asyncAssignments: Array<string> = [];
66
95
 
67
96
  if (appName) {
68
97
  assignments.push(`deps['${appName}']={}`);
69
98
  }
70
99
 
71
- for (const name of externals) {
72
- const identifiers = getIdentifiers(root, name);
73
- const path = getModulePathOrDefault(root, name);
74
- const ref = `_${imports.length}`;
75
- imports.push(`import * as ${ref} from ${JSON.stringify(path)}`);
100
+ for (const external of externals) {
101
+ if (external.endsWith('?')) {
102
+ const name = external.replace(/\?+$/, '');
103
+ const identifiers = getIdentifiers(root, name);
104
+ const path = getModulePathOrDefault(root, name);
76
105
 
77
- for (const id of identifiers) {
78
- assignments.push(`deps[${JSON.stringify(id)}]=${ref}`);
106
+ for (const id of identifiers) {
107
+ asyncAssignments.push(`registerModule(${JSON.stringify(id)}, () => import(${JSON.stringify(path)}))`);
108
+ }
109
+ } else {
110
+ const name = external;
111
+ const identifiers = getIdentifiers(root, name);
112
+ const path = getModulePathOrDefault(root, name);
113
+ const ref = `_${imports.length}`;
114
+ imports.push(`import * as ${ref} from ${JSON.stringify(path)}`);
115
+
116
+ for (const id of identifiers) {
117
+ assignments.push(`deps[${JSON.stringify(id)}]=${ref}`);
118
+ }
79
119
  }
80
120
  }
81
121
 
122
+ if (asyncAssignments.length) {
123
+ imports.push(`import { registerModule } from 'piral-base'`);
124
+ assignments.push(...asyncAssignments);
125
+
126
+ }
127
+
82
128
  exports.push(`
83
129
  export function fillDependencies(deps) {
84
130
  ${assignments.join(';')}
@@ -98,4 +98,8 @@ export interface NavigationApi {
98
98
  * as the implementation is router specific and may change over time.
99
99
  */
100
100
  router: any;
101
+ /**
102
+ * Gets the public path of the application.
103
+ */
104
+ publicPath: string;
101
105
  }
@@ -1,2 +0,0 @@
1
- import { AppPath } from '../types';
2
- export declare function useRoutes(): AppPath[];
@@ -1,11 +0,0 @@
1
- import { useGlobalState } from './globalState';
2
- import { useRouteFilter } from '../../app.codegen';
3
- export function useRoutes() {
4
- const routes = useGlobalState((s) => s.routes);
5
- const pages = useGlobalState((s) => s.registry.pages);
6
- const paths = [];
7
- Object.keys(routes).map((path) => paths.push({ path, Component: routes[path] }));
8
- Object.keys(pages).map((path) => paths.push({ path, Component: pages[path].component }));
9
- return useRouteFilter(paths);
10
- }
11
- //# sourceMappingURL=routes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/hooks/routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGnD,MAAM,UAAU,SAAS;IACvB,MAAM,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACtD,MAAM,KAAK,GAAmB,EAAE,CAAC;IAEjC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACjF,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IAEzF,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC"}
@@ -1,2 +0,0 @@
1
- import { AppPath } from '../types';
2
- export declare function useRoutes(): AppPath[];
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRoutes = void 0;
4
- const globalState_1 = require("./globalState");
5
- const app_codegen_1 = require("../../app.codegen");
6
- function useRoutes() {
7
- const routes = (0, globalState_1.useGlobalState)((s) => s.routes);
8
- const pages = (0, globalState_1.useGlobalState)((s) => s.registry.pages);
9
- const paths = [];
10
- Object.keys(routes).map((path) => paths.push({ path, Component: routes[path] }));
11
- Object.keys(pages).map((path) => paths.push({ path, Component: pages[path].component }));
12
- return (0, app_codegen_1.useRouteFilter)(paths);
13
- }
14
- exports.useRoutes = useRoutes;
15
- //# sourceMappingURL=routes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/hooks/routes.ts"],"names":[],"mappings":";;;AAAA,+CAA+C;AAC/C,mDAAmD;AAGnD,SAAgB,SAAS;IACvB,MAAM,MAAM,GAAG,IAAA,4BAAc,EAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,KAAK,GAAG,IAAA,4BAAc,EAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACtD,MAAM,KAAK,GAAmB,EAAE,CAAC;IAEjC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACjF,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;IAEzF,OAAO,IAAA,4BAAc,EAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AATD,8BASC"}
@@ -1,14 +0,0 @@
1
- import { useGlobalState } from './globalState';
2
- import { useRouteFilter } from '../../app.codegen';
3
- import { AppPath } from '../types';
4
-
5
- export function useRoutes() {
6
- const routes = useGlobalState((s) => s.routes);
7
- const pages = useGlobalState((s) => s.registry.pages);
8
- const paths: Array<AppPath> = [];
9
-
10
- Object.keys(routes).map((path) => paths.push({ path, Component: routes[path] }));
11
- Object.keys(pages).map((path) => paths.push({ path, Component: pages[path].component }));
12
-
13
- return useRouteFilter(paths);
14
- }