@nocobase/client-v2 2.1.0-beta.34 → 2.1.0-beta.35

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.
@@ -37,53 +37,69 @@ import * as dndKitCore from '@dnd-kit/core';
37
37
  import * as dndKitSortable from '@dnd-kit/sortable';
38
38
  import type { RequireJS } from './requirejs';
39
39
 
40
+ declare global {
41
+ interface Window {
42
+ __nocobase_app_dev__?: boolean;
43
+ __nocobase_app_dev_deps__?: Record<string, unknown>;
44
+ __nocobase_app_dev_plugins__?: Record<string, unknown>;
45
+ }
46
+ }
47
+
48
+ function defineGlobalDep(requirejs: RequireJS, name: string, value: unknown) {
49
+ requirejs.define(name, () => value);
50
+ if (window.__nocobase_app_dev__) {
51
+ window.__nocobase_app_dev_deps__ = window.__nocobase_app_dev_deps__ || {};
52
+ window.__nocobase_app_dev_deps__[name] = value;
53
+ }
54
+ }
55
+
40
56
  /**
41
57
  * @internal
42
58
  */
43
59
  export function defineGlobalDeps(requirejs: RequireJS) {
44
60
  // react
45
- requirejs.define('react', () => React);
46
- requirejs.define('react-dom', () => ReactDOM);
47
- requirejs.define('react/jsx-runtime', () => jsxRuntime);
61
+ defineGlobalDep(requirejs, 'react', React);
62
+ defineGlobalDep(requirejs, 'react-dom', ReactDOM);
63
+ defineGlobalDep(requirejs, 'react/jsx-runtime', jsxRuntime);
48
64
 
49
65
  // react-router
50
- requirejs.define('react-router', () => ReactRouter);
51
- requirejs.define('react-router-dom', () => ReactRouterDom);
66
+ defineGlobalDep(requirejs, 'react-router', ReactRouter);
67
+ defineGlobalDep(requirejs, 'react-router-dom', ReactRouterDom);
52
68
 
53
69
  // antd
54
- requirejs.define('antd', () => antd);
55
- requirejs.define('@ant-design/icons', () => antdIcons);
56
- requirejs.define('@ant-design/cssinjs', () => antdCssinjs);
57
- requirejs.define('antd-style', () => antdStyle);
70
+ defineGlobalDep(requirejs, 'antd', antd);
71
+ defineGlobalDep(requirejs, '@ant-design/icons', antdIcons);
72
+ defineGlobalDep(requirejs, '@ant-design/cssinjs', antdCssinjs);
73
+ defineGlobalDep(requirejs, 'antd-style', antdStyle);
58
74
 
59
75
  // i18next
60
- requirejs.define('i18next', () => i18next);
61
- requirejs.define('react-i18next', () => reactI18next);
76
+ defineGlobalDep(requirejs, 'i18next', i18next);
77
+ defineGlobalDep(requirejs, 'react-i18next', reactI18next);
62
78
 
63
79
  // formily
64
- requirejs.define('@formily/antd-v5', () => formilyAntdV5);
65
- requirejs.define('@formily/core', () => formilyCore);
66
- requirejs.define('@formily/react', () => formilyReact);
67
- requirejs.define('@formily/reactive', () => formilyReactive);
68
- requirejs.define('@formily/shared', () => formilyShared);
80
+ defineGlobalDep(requirejs, '@formily/antd-v5', formilyAntdV5);
81
+ defineGlobalDep(requirejs, '@formily/core', formilyCore);
82
+ defineGlobalDep(requirejs, '@formily/react', formilyReact);
83
+ defineGlobalDep(requirejs, '@formily/reactive', formilyReactive);
84
+ defineGlobalDep(requirejs, '@formily/shared', formilyShared);
69
85
 
70
86
  // nocobase
71
- requirejs.define('@nocobase/utils', () => nocobaseClientUtils);
72
- requirejs.define('@nocobase/utils/client', () => nocobaseClientUtils);
73
- requirejs.define('@nocobase/client-v2', () => nocobaseClientV2);
74
- requirejs.define('@nocobase/client-v2/client-v2', () => nocobaseClientV2);
75
- requirejs.define('@nocobase/flow-engine', () => nocobaseFlowEngine);
76
- requirejs.define('@nocobase/evaluators', () => nocobaseEvaluators);
77
- requirejs.define('@nocobase/evaluators/client', () => nocobaseEvaluators);
87
+ defineGlobalDep(requirejs, '@nocobase/utils', nocobaseClientUtils);
88
+ defineGlobalDep(requirejs, '@nocobase/utils/client', nocobaseClientUtils);
89
+ defineGlobalDep(requirejs, '@nocobase/client-v2', nocobaseClientV2);
90
+ defineGlobalDep(requirejs, '@nocobase/client-v2/client-v2', nocobaseClientV2);
91
+ defineGlobalDep(requirejs, '@nocobase/flow-engine', nocobaseFlowEngine);
92
+ defineGlobalDep(requirejs, '@nocobase/evaluators', nocobaseEvaluators);
93
+ defineGlobalDep(requirejs, '@nocobase/evaluators/client', nocobaseEvaluators);
78
94
 
79
95
  requirejs.define('@dnd-kit/core', () => dndKitCore);
80
96
  requirejs.define('@dnd-kit/sortable', () => dndKitSortable);
81
97
 
82
98
  // utils
83
- requirejs.define('ahooks', () => ahooks);
84
- requirejs.define('axios', () => axios);
85
- requirejs.define('dayjs', () => dayjs);
86
- requirejs.define('lodash', () => lodash);
87
- requirejs.define('@emotion/css', () => emotionCss);
88
- requirejs.define('file-saver', () => FileSaver);
99
+ defineGlobalDep(requirejs, 'ahooks', ahooks);
100
+ defineGlobalDep(requirejs, 'axios', axios);
101
+ defineGlobalDep(requirejs, 'dayjs', dayjs);
102
+ defineGlobalDep(requirejs, 'lodash', lodash);
103
+ defineGlobalDep(requirejs, '@emotion/css', emotionCss);
104
+ defineGlobalDep(requirejs, 'file-saver', FileSaver);
89
105
  }
@@ -12,10 +12,22 @@ import type { PluginClass } from '../PluginManager';
12
12
  import type { PluginData } from '../PluginManager';
13
13
  import type { RequireJS } from './requirejs';
14
14
 
15
+ type RemotePluginModule = PluginClass | ({ default?: PluginClass } & Record<string, unknown>);
16
+
15
17
  function getClientV2ModuleId(packageName: string) {
16
18
  return `${packageName}/client-v2`;
17
19
  }
18
20
 
21
+ function getPluginClass(pluginModule: RemotePluginModule): PluginClass {
22
+ const defaultPlugin = 'default' in pluginModule ? pluginModule.default : undefined;
23
+ return defaultPlugin || (pluginModule as PluginClass);
24
+ }
25
+
26
+ function defineAppDevPluginModule(moduleId: string, pluginModule: RemotePluginModule) {
27
+ window.__nocobase_app_dev_plugins__ = window.__nocobase_app_dev_plugins__ || {};
28
+ window.__nocobase_app_dev_plugins__[moduleId] = pluginModule;
29
+ }
30
+
19
31
  /**
20
32
  * @internal
21
33
  */
@@ -25,6 +37,14 @@ export function defineDevPlugins(plugins: Record<string, PluginClass>) {
25
37
  });
26
38
  }
27
39
 
40
+ function defineDevPluginModules(plugins: Record<string, RemotePluginModule>) {
41
+ Object.entries(plugins).forEach(([packageName, pluginModule]) => {
42
+ const moduleId = getClientV2ModuleId(packageName);
43
+ window.define(moduleId, () => pluginModule);
44
+ defineAppDevPluginModule(moduleId, pluginModule);
45
+ });
46
+ }
47
+
28
48
  /**
29
49
  * @internal
30
50
  */
@@ -43,6 +63,12 @@ export function configRequirejs(requirejs: any, pluginData: PluginData[]) {
43
63
  */
44
64
  export function processRemotePlugins(pluginData: PluginData[], resolve: (plugins: [string, PluginClass][]) => void) {
45
65
  return (...pluginModules: (PluginClass & { default?: PluginClass })[]) => {
66
+ pluginModules.forEach((item, index) => {
67
+ if (item) {
68
+ defineAppDevPluginModule(getClientV2ModuleId(pluginData[index].packageName), item);
69
+ }
70
+ });
71
+
46
72
  const res: [string, PluginClass][] = pluginModules
47
73
  .map<[string, PluginClass]>((item, index) => [pluginData[index].name, item?.default || item])
48
74
  .filter((item) => item[1]);
@@ -75,6 +101,77 @@ export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []):
75
101
  });
76
102
  }
77
103
 
104
+ async function getEsmDevPlugins(pluginData: PluginData[] = []): Promise<Array<[string, PluginClass]>> {
105
+ const plugins: Array<[string, PluginClass]> = [];
106
+ for (const plugin of sortPluginsByAppDevDependencies(pluginData)) {
107
+ const pluginModule: RemotePluginModule = await import(/* webpackIgnore: true */ plugin.url);
108
+ const pluginClass = getPluginClass(pluginModule);
109
+ if (pluginClass) {
110
+ plugins.push([plugin.name, pluginClass]);
111
+ defineDevPluginModules({ [plugin.packageName]: pluginModule });
112
+ }
113
+ }
114
+ return plugins;
115
+ }
116
+
117
+ function sortPluginsByAppDevDependencies(pluginData: PluginData[] = []) {
118
+ const pluginMap = new Map(pluginData.map((plugin) => [plugin.packageName, plugin]));
119
+ const sorted: PluginData[] = [];
120
+ const visiting = new Set<string>();
121
+ const visited = new Set<string>();
122
+
123
+ const visit = (plugin: PluginData) => {
124
+ if (visited.has(plugin.packageName)) {
125
+ return;
126
+ }
127
+ if (visiting.has(plugin.packageName)) {
128
+ return;
129
+ }
130
+ visiting.add(plugin.packageName);
131
+ for (const dep of plugin.appDevDependencies || []) {
132
+ const depPlugin = pluginMap.get(dep);
133
+ if (depPlugin) {
134
+ visit(depPlugin);
135
+ }
136
+ }
137
+ visiting.delete(plugin.packageName);
138
+ visited.add(plugin.packageName);
139
+ sorted.push(plugin);
140
+ };
141
+
142
+ pluginData.forEach(visit);
143
+ return sorted;
144
+ }
145
+
146
+ async function getMixedRemotePluginsInOrder(
147
+ requirejs: RequireJS,
148
+ pluginData: PluginData[] = [],
149
+ ): Promise<Array<[string, PluginClass]>> {
150
+ const plugins: Array<[string, PluginClass]> = [];
151
+ let requirejsPlugins: PluginData[] = [];
152
+ const flushRequirejsPlugins = async () => {
153
+ if (requirejsPlugins.length === 0) {
154
+ return;
155
+ }
156
+ const remotePluginList = await getRemotePlugins(requirejs, requirejsPlugins);
157
+ plugins.push(...remotePluginList);
158
+ requirejsPlugins = [];
159
+ };
160
+
161
+ for (const plugin of sortPluginsByAppDevDependencies(pluginData)) {
162
+ if (plugin.devMode === 'esm') {
163
+ await flushRequirejsPlugins();
164
+ const esmPluginList = await getEsmDevPlugins([plugin]);
165
+ plugins.push(...esmPluginList);
166
+ continue;
167
+ }
168
+ requirejsPlugins.push(plugin);
169
+ }
170
+
171
+ await flushRequirejsPlugins();
172
+ return plugins;
173
+ }
174
+
78
175
  interface GetPluginsOption {
79
176
  requirejs: RequireJS;
80
177
  pluginData: PluginData[];
@@ -103,15 +200,19 @@ export async function getPlugins(options: GetPluginsOption): Promise<Array<[stri
103
200
  }
104
201
 
105
202
  const remotePlugins = pluginData.filter((item) => !resolveDevPlugins[item.packageName]);
203
+ const esmDevPlugins = remotePlugins.filter((item) => item.devMode === 'esm');
204
+ const requirejsPlugins = remotePlugins.filter((item) => item.devMode !== 'esm');
106
205
 
107
- if (remotePlugins.length === 0) {
108
- return res;
109
- }
110
-
111
- if (res.length === 0) {
112
- const remotePluginList = await getRemotePlugins(requirejs, remotePlugins);
206
+ if (esmDevPlugins.length === 0) {
207
+ if (requirejsPlugins.length === 0) {
208
+ return res;
209
+ }
210
+ const remotePluginList = await getRemotePlugins(requirejs, requirejsPlugins);
113
211
  res.push(...remotePluginList);
212
+ return res;
114
213
  }
115
214
 
215
+ const mixedPluginList = await getMixedRemotePluginsInOrder(requirejs, remotePlugins);
216
+ res.push(...mixedPluginList);
116
217
  return res;
117
218
  }