@nocobase/client-v2 2.0.0-alpha.20

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 (70) hide show
  1. package/LICENSE.txt +172 -0
  2. package/lib/Application.d.ts +124 -0
  3. package/lib/Application.js +489 -0
  4. package/lib/MockApplication.d.ts +16 -0
  5. package/lib/MockApplication.js +96 -0
  6. package/lib/Plugin.d.ts +33 -0
  7. package/lib/Plugin.js +89 -0
  8. package/lib/PluginManager.d.ts +46 -0
  9. package/lib/PluginManager.js +114 -0
  10. package/lib/PluginSettingsManager.d.ts +67 -0
  11. package/lib/PluginSettingsManager.js +148 -0
  12. package/lib/RouterManager.d.ts +61 -0
  13. package/lib/RouterManager.js +198 -0
  14. package/lib/WebSocketClient.d.ts +45 -0
  15. package/lib/WebSocketClient.js +217 -0
  16. package/lib/components/BlankComponent.d.ts +12 -0
  17. package/lib/components/BlankComponent.js +48 -0
  18. package/lib/components/MainComponent.d.ts +10 -0
  19. package/lib/components/MainComponent.js +54 -0
  20. package/lib/components/RouterBridge.d.ts +13 -0
  21. package/lib/components/RouterBridge.js +66 -0
  22. package/lib/components/RouterContextCleaner.d.ts +12 -0
  23. package/lib/components/RouterContextCleaner.js +61 -0
  24. package/lib/components/index.d.ts +10 -0
  25. package/lib/components/index.js +32 -0
  26. package/lib/context.d.ts +11 -0
  27. package/lib/context.js +38 -0
  28. package/lib/hooks/index.d.ts +11 -0
  29. package/lib/hooks/index.js +34 -0
  30. package/lib/hooks/useApp.d.ts +10 -0
  31. package/lib/hooks/useApp.js +41 -0
  32. package/lib/hooks/usePlugin.d.ts +11 -0
  33. package/lib/hooks/usePlugin.js +42 -0
  34. package/lib/hooks/useRouter.d.ts +9 -0
  35. package/lib/hooks/useRouter.js +41 -0
  36. package/lib/index.d.ts +14 -0
  37. package/lib/index.js +40 -0
  38. package/lib/utils/index.d.ts +11 -0
  39. package/lib/utils/index.js +79 -0
  40. package/lib/utils/remotePlugins.d.ts +44 -0
  41. package/lib/utils/remotePlugins.js +131 -0
  42. package/lib/utils/requirejs.d.ts +18 -0
  43. package/lib/utils/requirejs.js +1361 -0
  44. package/lib/utils/types.d.ts +330 -0
  45. package/lib/utils/types.js +28 -0
  46. package/package.json +16 -0
  47. package/src/Application.tsx +539 -0
  48. package/src/MockApplication.tsx +53 -0
  49. package/src/Plugin.ts +78 -0
  50. package/src/PluginManager.ts +114 -0
  51. package/src/PluginSettingsManager.ts +182 -0
  52. package/src/RouterManager.tsx +239 -0
  53. package/src/WebSocketClient.ts +220 -0
  54. package/src/__tests__/app.test.tsx +141 -0
  55. package/src/components/BlankComponent.tsx +12 -0
  56. package/src/components/MainComponent.tsx +20 -0
  57. package/src/components/RouterBridge.tsx +38 -0
  58. package/src/components/RouterContextCleaner.tsx +26 -0
  59. package/src/components/index.ts +11 -0
  60. package/src/context.ts +14 -0
  61. package/src/hooks/index.ts +12 -0
  62. package/src/hooks/useApp.ts +16 -0
  63. package/src/hooks/usePlugin.ts +17 -0
  64. package/src/hooks/useRouter.ts +15 -0
  65. package/src/index.ts +15 -0
  66. package/src/utils/index.tsx +48 -0
  67. package/src/utils/remotePlugins.ts +140 -0
  68. package/src/utils/requirejs.ts +2164 -0
  69. package/src/utils/types.ts +375 -0
  70. package/tsconfig.json +7 -0
@@ -0,0 +1,140 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import type { DevDynamicImport } from '../Application';
11
+ import type { Plugin } from '../Plugin';
12
+ import type { PluginData } from '../PluginManager';
13
+ import type { RequireJS } from './requirejs';
14
+
15
+ /**
16
+ * @internal
17
+ */
18
+ export function defineDevPlugins(plugins: Record<string, typeof Plugin>) {
19
+ Object.entries(plugins).forEach(([packageName, plugin]) => {
20
+ window.define(`${packageName}/client`, () => plugin);
21
+ });
22
+ }
23
+
24
+ /**
25
+ * @internal
26
+ */
27
+ export function definePluginClient(packageName: string) {
28
+ window.define(`${packageName}/client`, ['exports', packageName], function (_exports: any, _pluginExports: any) {
29
+ Object.defineProperty(_exports, '__esModule', {
30
+ value: true,
31
+ });
32
+ Object.keys(_pluginExports).forEach(function (key) {
33
+ if (key === '__esModule') return;
34
+ if (key in _exports && _exports[key] === _pluginExports[key]) return;
35
+ Object.defineProperty(_exports, key, {
36
+ enumerable: true,
37
+ get: function () {
38
+ return _pluginExports[key];
39
+ },
40
+ });
41
+ });
42
+ });
43
+ }
44
+
45
+ /**
46
+ * @internal
47
+ */
48
+ export function configRequirejs(requirejs: any, pluginData: PluginData[]) {
49
+ requirejs.requirejs.config({
50
+ waitSeconds: 120,
51
+ paths: pluginData.reduce<Record<string, string>>((acc, cur) => {
52
+ acc[cur.packageName] = cur.url;
53
+ return acc;
54
+ }, {}),
55
+ });
56
+ }
57
+
58
+ /**
59
+ * @internal
60
+ */
61
+ export function processRemotePlugins(pluginData: PluginData[], resolve: (plugins: [string, typeof Plugin][]) => void) {
62
+ return (...pluginModules: (typeof Plugin & { default?: typeof Plugin })[]) => {
63
+ const res: [string, typeof Plugin][] = pluginModules
64
+ .map<[string, typeof Plugin]>((item, index) => [pluginData[index].name, item?.default || item])
65
+ .filter((item) => item[1]);
66
+ resolve(res);
67
+
68
+ const emptyPlugins = pluginModules
69
+ .map((item, index) => (!item ? index : null))
70
+ .filter((i) => i !== null)
71
+ .map((i) => pluginData[i].packageName);
72
+
73
+ if (emptyPlugins.length > 0) {
74
+ console.error(
75
+ '[nocobase load plugin error]: These plugins do not have an `export.default` exported content or there is an error in the plugins. error plugins: \r\n%s',
76
+ emptyPlugins.join(', \r\n'),
77
+ );
78
+ }
79
+ };
80
+ }
81
+
82
+ /**
83
+ * @internal
84
+ */
85
+ export function getRemotePlugins(
86
+ requirejs: any,
87
+ pluginData: PluginData[] = [],
88
+ ): Promise<Array<[string, typeof Plugin]>> {
89
+ configRequirejs(requirejs, pluginData);
90
+
91
+ const packageNames = pluginData.map((item) => item.packageName);
92
+ packageNames.forEach((packageName) => {
93
+ definePluginClient(packageName);
94
+ });
95
+
96
+ return new Promise((resolve, reject) => {
97
+ requirejs.requirejs(packageNames, processRemotePlugins(pluginData, resolve), reject);
98
+ });
99
+ }
100
+
101
+ interface GetPluginsOption {
102
+ requirejs: RequireJS;
103
+ pluginData: PluginData[];
104
+ devDynamicImport?: DevDynamicImport;
105
+ }
106
+
107
+ /**
108
+ * @internal
109
+ */
110
+ export async function getPlugins(options: GetPluginsOption): Promise<Array<[string, typeof Plugin]>> {
111
+ const { requirejs, pluginData, devDynamicImport } = options;
112
+ if (pluginData.length === 0) return [];
113
+
114
+ const res: Array<[string, typeof Plugin]> = [];
115
+
116
+ const resolveDevPlugins: Record<string, typeof Plugin> = {};
117
+ if (devDynamicImport) {
118
+ for await (const plugin of pluginData) {
119
+ const pluginModule = await devDynamicImport(plugin.packageName);
120
+ if (pluginModule) {
121
+ res.push([plugin.name, pluginModule.default]);
122
+ resolveDevPlugins[plugin.packageName] = pluginModule.default;
123
+ }
124
+ }
125
+ defineDevPlugins(resolveDevPlugins);
126
+ }
127
+
128
+ const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.packageName]);
129
+
130
+ if (remotePlugins.length === 0) {
131
+ return res;
132
+ }
133
+
134
+ if (res.length === 0) {
135
+ const remotePluginList = await getRemotePlugins(requirejs, remotePlugins);
136
+ res.push(...remotePluginList);
137
+ }
138
+
139
+ return res;
140
+ }