chayns-api 1.1.0-2 → 1.1.0-21

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 (92) hide show
  1. package/dist/chayns-api.js +2 -0
  2. package/dist/chayns-api.js.LICENSE.txt +41 -0
  3. package/dist/cjs/calls/dialogs/chaynsDialog.js +3 -6
  4. package/dist/cjs/calls/dialogs/date.js +29 -2
  5. package/dist/cjs/calls/dialogs/fileSelect.js +1 -2
  6. package/dist/cjs/calls/dialogs/input.js +2 -3
  7. package/dist/cjs/calls/dialogs/select.js +2 -3
  8. package/dist/cjs/calls/getUserInfo.js +1 -2
  9. package/dist/cjs/calls/index.js +93 -82
  10. package/dist/cjs/calls/toolbarChangeListener.js +1 -0
  11. package/dist/cjs/components/AppDialogWrapper.js +7 -7
  12. package/dist/cjs/components/ChaynsContext.js +2 -4
  13. package/dist/cjs/components/ChaynsProvider.js +10 -13
  14. package/dist/cjs/components/ErrorBoundary.js +31 -0
  15. package/dist/cjs/components/moduleWrapper.js +9 -0
  16. package/dist/cjs/components/withCompatMode.js +8 -7
  17. package/dist/cjs/components/withHydrationBoundary.js +54 -0
  18. package/dist/cjs/constants/hydrationContext.js +16 -0
  19. package/dist/cjs/constants/index.js +16 -0
  20. package/dist/cjs/constants/languages.js +19 -0
  21. package/dist/cjs/handler/DialogHandler.js +4 -2
  22. package/dist/cjs/hooks/useAccessToken.js +1 -0
  23. package/dist/cjs/hooks/useDialogState.js +2 -2
  24. package/dist/cjs/hooks/usePages.js +2 -2
  25. package/dist/cjs/host/ChaynsHost.js +21 -41
  26. package/dist/cjs/host/iframe/HostIframe.js +4 -6
  27. package/dist/cjs/host/iframe/utils/useUpdateData.js +1 -2
  28. package/dist/cjs/host/module/ModuleHost.js +9 -24
  29. package/dist/cjs/host/module/Test3.js +141 -0
  30. package/dist/cjs/host/module/utils/loadComponent.js +94 -49
  31. package/dist/cjs/index.js +63 -5
  32. package/dist/cjs/types/IChaynsReact.js +140 -107
  33. package/dist/cjs/util/appCall.js +3 -4
  34. package/dist/cjs/util/deviceHelper.js +18 -13
  35. package/dist/cjs/util/transferNestedFunctions.js +39 -0
  36. package/dist/cjs/util/url.js +1 -1
  37. package/dist/cjs/wrapper/AppWrapper.js +79 -16
  38. package/dist/cjs/wrapper/FrameWrapper.js +16 -5
  39. package/dist/cjs/wrapper/StaticChaynsApi.js +22 -0
  40. package/dist/esm/calls/index.js +22 -1
  41. package/dist/esm/calls/toolbarChangeListener.js +0 -0
  42. package/dist/esm/components/AppDialogWrapper.js +4 -3
  43. package/dist/esm/components/ChaynsProvider.js +6 -7
  44. package/dist/esm/components/ErrorBoundary.js +23 -0
  45. package/dist/esm/components/moduleWrapper.js +3 -0
  46. package/dist/esm/components/withCompatMode.js +5 -4
  47. package/dist/esm/components/withHydrationBoundary.js +47 -0
  48. package/dist/esm/constants/hydrationContext.js +10 -0
  49. package/dist/esm/constants/index.js +1 -0
  50. package/dist/esm/constants/languages.js +13 -0
  51. package/dist/esm/handler/DialogHandler.js +6 -4
  52. package/dist/esm/hooks/useAccessToken.js +1 -0
  53. package/dist/esm/hooks/useDialogState.js +2 -2
  54. package/dist/esm/hooks/usePages.js +1 -1
  55. package/dist/esm/host/ChaynsHost.js +18 -39
  56. package/dist/esm/host/iframe/HostIframe.js +1 -2
  57. package/dist/esm/host/module/ModuleHost.js +6 -20
  58. package/dist/esm/host/module/Test3.js +135 -0
  59. package/dist/esm/host/module/utils/loadComponent.js +95 -49
  60. package/dist/esm/index.js +7 -1
  61. package/dist/esm/types/IChaynsReact.js +142 -87
  62. package/dist/esm/util/appCall.js +2 -2
  63. package/dist/esm/util/deviceHelper.js +17 -11
  64. package/dist/esm/util/transferNestedFunctions.js +35 -0
  65. package/dist/esm/util/url.js +2 -2
  66. package/dist/esm/wrapper/AppWrapper.js +80 -16
  67. package/dist/esm/wrapper/FrameWrapper.js +16 -5
  68. package/dist/esm/wrapper/StaticChaynsApi.js +20 -0
  69. package/dist/types/calls/index.d.ts +19 -5
  70. package/dist/types/calls/toolbarChangeListener.d.ts +0 -0
  71. package/dist/types/components/AppDialogWrapper.d.ts +2 -2
  72. package/dist/types/components/ChaynsProvider.d.ts +1 -4
  73. package/dist/types/components/ErrorBoundary.d.ts +13 -0
  74. package/dist/types/components/moduleWrapper.d.ts +4 -0
  75. package/dist/types/components/withHydrationBoundary.d.ts +17 -0
  76. package/dist/types/constants/hydrationContext.d.ts +9 -0
  77. package/dist/types/constants/index.d.ts +1 -0
  78. package/dist/types/constants/languages.d.ts +12 -0
  79. package/dist/types/handler/DialogHandler.d.ts +0 -1
  80. package/dist/types/hooks/useAccessToken.d.ts +1 -0
  81. package/dist/types/hooks/useCurrentPage.d.ts +4 -1
  82. package/dist/types/host/ChaynsHost.d.ts +0 -1
  83. package/dist/types/host/module/ModuleHost.d.ts +2 -0
  84. package/dist/types/host/module/Test3.d.ts +8 -0
  85. package/dist/types/host/module/utils/loadComponent.d.ts +3 -1
  86. package/dist/types/index.d.ts +7 -1
  87. package/dist/types/types/IChaynsReact.d.ts +136 -28
  88. package/dist/types/util/deviceHelper.d.ts +2 -2
  89. package/dist/types/util/transferNestedFunctions.d.ts +1 -0
  90. package/dist/types/wrapper/StaticChaynsApi.d.ts +9 -0
  91. package/package.json +33 -22
  92. package/toolkit.config.js +20 -0
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { startTransition, useEffect, useState } from 'react';
2
2
  import HostIframe from './iframe/HostIframe';
3
3
  import ModuleHost from './module/ModuleHost';
4
4
  const ChaynsHost = _ref => {
@@ -9,7 +9,6 @@ const ChaynsHost = _ref => {
9
9
  src,
10
10
  iFrameRef = undefined,
11
11
  loadingComponent = undefined,
12
- children = undefined,
13
12
  system,
14
13
  // shallow data
15
14
  pages,
@@ -25,8 +24,23 @@ const ChaynsHost = _ref => {
25
24
  preventStagingReplacement,
26
25
  dialog
27
26
  } = _ref;
27
+ const [isVisible, setIsVisible] = useState(type === 'server-iframe' || type === 'server-module' && (system === null || system === void 0 ? void 0 : system.serverUrl));
28
+ useEffect(() => {
29
+ if (isVisible) return;
30
+ if (typeof startTransition === 'function') {
31
+ startTransition(() => {
32
+ setIsVisible(true);
33
+ });
34
+ } else {
35
+ setIsVisible(true);
36
+ }
37
+ }, []);
38
+ if (!isVisible) {
39
+ return null;
40
+ }
28
41
  switch (type) {
29
42
  case 'client-iframe':
43
+ case 'server-iframe':
30
44
  return /*#__PURE__*/React.createElement(HostIframe, {
31
45
  iFrameRef: iFrameRef,
32
46
  iFrameProps: iFrameProps,
@@ -38,6 +52,7 @@ const ChaynsHost = _ref => {
38
52
  currentPage: currentPage,
39
53
  functions: functions,
40
54
  src: src,
55
+ postForm: type === 'server-iframe',
41
56
  language: language,
42
57
  parameters: parameters,
43
58
  environment: environment,
@@ -46,6 +61,7 @@ const ChaynsHost = _ref => {
46
61
  dialog: dialog
47
62
  });
48
63
  case 'client-module':
64
+ case 'server-module':
49
65
  return /*#__PURE__*/React.createElement(ModuleHost, {
50
66
  system: system,
51
67
  pages: pages,
@@ -63,43 +79,6 @@ const ChaynsHost = _ref => {
63
79
  preventStagingReplacement: preventStagingReplacement,
64
80
  dialog: dialog
65
81
  });
66
- case 'server-iframe':
67
- return /*#__PURE__*/React.createElement(HostIframe, {
68
- iFrameRef: iFrameRef,
69
- iFrameProps: iFrameProps,
70
- pages: pages,
71
- isAdminModeActive: isAdminModeActive,
72
- site: site,
73
- user: user,
74
- device: device,
75
- currentPage: currentPage,
76
- functions: functions,
77
- src: src,
78
- postForm: true,
79
- language: language,
80
- parameters: parameters,
81
- environment: environment,
82
- customData: customData,
83
- preventStagingReplacement: preventStagingReplacement,
84
- dialog: dialog
85
- });
86
- case 'server-module':
87
- return /*#__PURE__*/React.createElement(ModuleHost, {
88
- system: system,
89
- pages: pages,
90
- isAdminModeActive: isAdminModeActive,
91
- site: site,
92
- user: user,
93
- device: device,
94
- currentPage: currentPage,
95
- functions: functions,
96
- language: language,
97
- parameters: parameters,
98
- customData: customData,
99
- environment: environment,
100
- preventStagingReplacement: preventStagingReplacement,
101
- dialog: dialog
102
- }, children);
103
82
  default:
104
83
  return null;
105
84
  }
@@ -31,10 +31,9 @@ const HostIframe = _ref => {
31
31
  if (!eventTarget.current) {
32
32
  eventTarget.current = global.document ? document.createElement('div') : undefined; // global.EventTarget ? new EventTarget() : undefined
33
33
  }
34
-
35
34
  const setHeight = value => {
36
35
  if (ref.current) {
37
- ref.current.height = `${value}px`;
36
+ ref.current.style.height = `${value}px`;
38
37
  }
39
38
  };
40
39
 
@@ -1,5 +1,4 @@
1
1
  import React, { useMemo } from 'react';
2
- import useDynamicScript from './utils/useDynamicScript';
3
2
  import loadComponent from './utils/loadComponent';
4
3
  import { replaceStagingUrl } from "../../util/url";
5
4
  const System = _ref => {
@@ -8,25 +7,10 @@ const System = _ref => {
8
7
  fallback,
9
8
  ...props
10
9
  } = _ref;
11
- const {
12
- ready,
13
- failed
14
- } = useDynamicScript({
15
- url: system === null || system === void 0 ? void 0 : system.url,
16
- scope: system === null || system === void 0 ? void 0 : system.scope
17
- });
18
- const Component = useMemo(() => {
19
- // maybe return waitcursor instead
20
- if (!system || !ready || failed) {
21
- return null;
22
- }
23
- return /*#__PURE__*/React.lazy(loadComponent(system.scope, system.module, system.url));
24
-
25
- /* eslint-disable react-hooks/exhaustive-deps */
26
- }, [system === null || system === void 0 ? void 0 : system.scope, ready, system === null || system === void 0 ? void 0 : system.url]);
27
- return Component ? /*#__PURE__*/React.createElement(React.Suspense, {
10
+ const Component = useMemo(() => loadComponent(system.scope, system.module, globalThis.window ? system.url : system.serverUrl, undefined, system.preventSingleton), [system.scope, system.module, system.url, system.serverUrl, system.preventSingleton]);
11
+ return /*#__PURE__*/React.createElement(React.Suspense, {
28
12
  fallback: fallback || ''
29
- }, /*#__PURE__*/React.createElement(Component, props)) : fallback;
13
+ }, /*#__PURE__*/React.createElement(Component, props));
30
14
  };
31
15
  const ModuleHost = _ref2 => {
32
16
  let {
@@ -73,7 +57,9 @@ const ModuleHost = _ref2 => {
73
57
  system: {
74
58
  scope: system.scope,
75
59
  url: replaceStagingUrl(preventStagingReplacement, system.url, environment.buildEnvironment),
76
- module: system.module
60
+ serverUrl: replaceStagingUrl(preventStagingReplacement, system.serverUrl, environment.buildEnvironment),
61
+ module: system.module,
62
+ preventSingleton: system.preventSingleton
77
63
  },
78
64
  data: initialData,
79
65
  functions: functions,
@@ -0,0 +1,135 @@
1
+ import { semaphore } from "./utils/useDynamicScript";
2
+ import Semaphore from "semaphore-async-await";
3
+ import loadComponent from "./utils/loadComponent";
4
+ import React from "react";
5
+ const Test = () => {
6
+ return /*#__PURE__*/React.createElement("div", null, "Test12");
7
+ };
8
+ const Test2 = _ref => {
9
+ let {
10
+ fallback
11
+ } = _ref;
12
+ return /*#__PURE__*/React.createElement("div", null, fallback);
13
+ };
14
+ let status = 'pending';
15
+ let error = false;
16
+ function wrapPromise(promise) {
17
+ console.log("nice2222");
18
+ let response;
19
+ const suspender = promise.then(res => {
20
+ console.log("settttttt");
21
+ status = 'success';
22
+ response = res;
23
+ }, err => {
24
+ console.error(err);
25
+ status = 'error';
26
+ response = err;
27
+ });
28
+ const read = () => {
29
+ console.log("status", status);
30
+ switch (status) {
31
+ case 'pending':
32
+ throw suspender;
33
+ case 'error':
34
+ throw response;
35
+ default:
36
+ return response;
37
+ }
38
+ };
39
+ console.log("nice33333");
40
+ return {
41
+ read
42
+ };
43
+ }
44
+ export function fetchData(url) {
45
+ console.log("ahhh");
46
+ const promise = fetch(url).then(async res => {
47
+ var t = await res.json();
48
+ console.log("tttt", t);
49
+ return {
50
+ test: 1
51
+ };
52
+ });
53
+ return wrapPromise(promise);
54
+ }
55
+ let Raw = Test;
56
+ const Component = _ref2 => {
57
+ let {
58
+ fallback,
59
+ system
60
+ } = _ref2;
61
+ //
62
+ // return <div>Nice</div>;
63
+
64
+ const p = new Promise(async res => {
65
+ if (!global.window) {
66
+ Raw = Test2;
67
+ res(true);
68
+ return;
69
+ }
70
+ const args = {
71
+ url: system.url,
72
+ scope: system === null || system === void 0 ? void 0 : system.scope
73
+ };
74
+ if (!args.url) {
75
+ return undefined;
76
+ }
77
+ const element = document.createElement('script');
78
+ element.src = args.url;
79
+ element.type = 'text/javascript';
80
+ element.async = true;
81
+ if (!(args.scope in semaphore)) {
82
+ semaphore[args.scope] = new Semaphore(1);
83
+ }
84
+ (async () => {
85
+ await semaphore[args.scope].acquire();
86
+ element.onload = async () => {
87
+ await new Promise(r => setTimeout(r, 3000));
88
+ console.log('dsajkhdkjashdkjashdkashjkdhsjakhdkjsa');
89
+ const listKey = args.scope + "_list";
90
+ if (!window[listKey]) window[listKey] = [];
91
+ window[listKey].push({
92
+ url: args.url,
93
+ container: window[args.scope]
94
+ });
95
+ window[args.scope] = null;
96
+ const tt = () => new Promise(async r2 => {
97
+ var test1 = await (await loadComponent(system.scope, system.module, system.url, undefined, system.preventSingleton))();
98
+ console.log("ööööö", test1.default);
99
+ r2(test1.default);
100
+ });
101
+
102
+ // return React.lazy(tt);
103
+ Raw = await tt();
104
+ res(true);
105
+ console.log("Raw", Raw);
106
+ };
107
+ element.onerror = () => {
108
+ console.log("errorrrrrrrrrrrr");
109
+ res(false);
110
+ };
111
+ document.head.appendChild(element);
112
+ })();
113
+ });
114
+ const l = wrapPromise(p);
115
+ console.error("l.read", status);
116
+ l.read();
117
+ console.error("l.read2", error, Raw);
118
+ // !system || !ready || failed
119
+
120
+ if (error) {
121
+ return /*#__PURE__*/React.createElement(Test, null);
122
+ }
123
+ if (global.window) {
124
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Raw, {
125
+ fallback: fallback
126
+ }));
127
+ }
128
+ return /*#__PURE__*/React.createElement(Raw, {
129
+ fallback: fallback
130
+ });
131
+
132
+ /* eslint-disable react-hooks/exhaustive-deps */
133
+ };
134
+
135
+ export default Component;
@@ -1,55 +1,101 @@
1
- /* eslint-disable */
2
- // @ts-nocheck
3
-
1
+ import ReactDOM from 'react-dom';
4
2
  import semver from 'semver';
5
3
  import React from 'react';
6
- import { semaphore } from './useDynamicScript';
7
- let instances = {};
8
- export default function loadComponent(scope, module, url) {
9
- let skipCompatMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
10
- return async () => {
11
- // Initializes the shared scope. Fills it with known provided modules from this build and all remotes
12
- // eslint-disable-next-line no-undef
13
- await __webpack_init_sharing__('default');
14
- const {
15
- container
16
- } = window[scope + "_list"].find(x => x.url === url); // or get the container somewhere else
17
- // Initialize the container, it may provide shared modules
18
- // eslint-disable-next-line no-undef
19
- await container.init(__webpack_share_scopes__.default);
20
- const factory = await container.get(module);
21
- semaphore[scope].release();
22
- let ModuleMap = instances[`${scope}__${module}`];
23
- let Module;
24
- if (!ModuleMap) {
25
- ModuleMap = {};
26
- instances[`${scope}__${module}`] = ModuleMap;
4
+ import { loadRemote, registerRemotes, loadShareSync, init } from '@module-federation/runtime';
5
+ const registeredScopes = {};
6
+ const moduleMap = {};
7
+ const componentMap = {};
8
+ init({
9
+ // @ts-expect-error will be set by chayns-toolkit via DefinePlugin
10
+ name: process.env.__PACKAGE_NAME__,
11
+ remotes: [],
12
+ shared: {
13
+ react: {
14
+ version: React.version,
15
+ scope: 'default',
16
+ lib: () => React
17
+ },
18
+ 'react-dom': {
19
+ version: ReactDOM.version,
20
+ scope: 'default',
21
+ lib: () => ReactDOM
27
22
  }
28
- if (Object.keys(ModuleMap).length > 0) {
29
- const newModule = factory();
30
- Module = ModuleMap[`${newModule.default.buildEnv}__${newModule.default.appVersion}`];
31
- if (!Module) {
32
- Module = newModule;
33
- ModuleMap[`${newModule.default.buildEnv}__${newModule.default.appVersion}`] = newModule;
34
- }
35
- } else {
36
- Module = factory();
37
- ModuleMap[`${Module.default.buildEnv}__${Module.default.appVersion}`] = Module;
23
+ }
24
+ });
25
+ export const loadModule = function (scope, module, url) {
26
+ let preventSingleton = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
27
+ if (registeredScopes[scope] !== url || preventSingleton) {
28
+ if (scope in registeredScopes) {
29
+ console.error(`[chayns-api] call registerRemote with force for scope ${scope}. url: ${url}`);
38
30
  }
39
- if (skipCompatMode) return Module;
40
- const hostVersion = semver.minVersion(React.version);
41
- const {
42
- requiredVersion,
43
- environment
44
- } = Module.default;
45
- const matchReactVersion = requiredVersion && semver.satisfies(hostVersion, requiredVersion);
46
- if (!matchReactVersion || environment !== 'production') {
31
+ registerRemotes([{
32
+ name: scope,
33
+ entry: url,
34
+ alias: scope
35
+ }], {
36
+ force: scope in registeredScopes
37
+ });
38
+ registeredScopes[scope] = url;
39
+ moduleMap[scope] = {};
40
+ componentMap[scope] = {};
41
+ }
42
+ if (!(module in moduleMap[scope])) {
43
+ const path = `${scope}/${module.replace(/^\.\//, '')}`;
44
+ const promise = loadRemote(path);
45
+ promise.catch(e => {
46
+ console.error("[chayns-api] Failed to load module", scope, url, e);
47
+ // causes registerRemote with force = true on next attempt to load the component which tries to load the component again
48
+ registeredScopes[scope] = '';
49
+ });
50
+ return promise;
51
+ }
52
+ return moduleMap[scope][module];
53
+ };
54
+ const loadComponent = function (scope, module, url) {
55
+ let skipCompatMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
56
+ let preventSingleton = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
57
+ if (skipCompatMode) {
58
+ console.warn('[chayns-api] skipCompatMode-option is deprecated and is set automatically now');
59
+ }
60
+ if (!componentMap[scope]) {
61
+ componentMap[scope] = {};
62
+ }
63
+ if (!(module in componentMap[scope])) {
64
+ const promise = loadModule(scope, module, url, preventSingleton).then(async Module => {
65
+ if (typeof Module.default === 'function') {
66
+ return Module;
67
+ }
68
+ const hostVersion = semver.minVersion(React.version);
69
+ const {
70
+ requiredVersion,
71
+ environment
72
+ } = Module.default;
73
+ const shareScopes = await new Promise(resolve => {
74
+ loadShareSync('react', {
75
+ resolver: shareOptions => {
76
+ resolve(shareOptions);
77
+ return shareOptions[0];
78
+ }
79
+ });
80
+ });
81
+ const matchReactVersion = requiredVersion && semver.satisfies(hostVersion, requiredVersion) && !shareScopes.some(t => {
82
+ const {
83
+ version,
84
+ from
85
+ } = t;
86
+ return semver.gt(version, hostVersion) && semver.satisfies(version, requiredVersion) || scope === from.split('-').join('_');
87
+ });
88
+ if (!matchReactVersion || environment !== 'production' || process.env.NODE_ENV === 'development') {
89
+ return {
90
+ default: Module.default.CompatComponent
91
+ };
92
+ }
47
93
  return {
48
- default: Module.default.CompatComponent
94
+ default: Module.default.Component
49
95
  };
50
- }
51
- return {
52
- default: Module.default.Component
53
- };
54
- };
55
- }
96
+ });
97
+ componentMap[scope][module] = /*#__PURE__*/React.lazy(() => promise);
98
+ }
99
+ return componentMap[scope][module];
100
+ };
101
+ export default loadComponent;
package/dist/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
+ import './util/transferNestedFunctions';
1
2
  export { default as ChaynsProvider } from './components/ChaynsProvider';
2
- export { default as getDeviceInfo, getScreenSize } from './util/deviceHelper';
3
+ export { default as getDeviceInfo, getScreenSize, getClientDeviceInfo } from './util/deviceHelper';
3
4
  export { default as ChaynsHost } from './host/ChaynsHost';
4
5
  export { withCompatMode } from './components/withCompatMode';
5
6
  export * from './calls';
@@ -7,6 +8,11 @@ export * from './hooks';
7
8
  export * from './components/WaitUntil';
8
9
  export * from './types/IChaynsReact';
9
10
  export * from './components/withCompatMode';
11
+ export * from './constants';
12
+ export { default as withHydrationBoundary } from './components/withHydrationBoundary';
13
+ export { default as StaticChaynsApi } from './wrapper/StaticChaynsApi';
14
+ export { default as loadComponent, loadModule } from './host/module/utils/loadComponent';
15
+ export { default as DialogHandler } from './handler/DialogHandler';
10
16
  import * as _dialog from './calls/dialogs/index';
11
17
  export { _dialog as dialog };
12
18
  export * from './types/IChaynsReact';