From 836b34a6a47a59c6eefc01cf948dd2d47c9b8491 Mon Sep 17 00:00:00 2001 From: jack zhang <1098626505@qq.com> Date: Sat, 23 Sep 2023 10:52:14 +0800 Subject: [PATCH] fix: gen tsconfig.paths.json bug (#2708) * fix: gen tsconfig.paths.json bug * fix: load remote plugin error * fix: bug --- packages/core/cli/src/util.js | 7 +-- .../client/src/application/PluginManager.ts | 1 - .../src/application/utils/remotePlugins.ts | 49 +++++++++++-------- .../plugin-api-doc/src/client/Document.tsx | 2 +- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/core/cli/src/util.js b/packages/core/cli/src/util.js index b31299edf..84cd0aaf6 100644 --- a/packages/core/cli/src/util.js +++ b/packages/core/cli/src/util.js @@ -190,13 +190,8 @@ exports.genTsConfigPaths = function genTsConfigPaths() { const packageJsonName = require(packageFile).name; const packageDir = dirname(packageFile); const relativePath = packageDir.slice(cwdLength + 1).replace(/\\/, '/'); - const hasClient = fg.sync(['client', 'client.ts', 'client.tsx', 'client.js', 'client.jsx'], { - cwd: join(packageDir, 'src'), - }).length; - if (hasClient) { - paths[`${packageJsonName}/client`] = [`${relativePath}/src/client`]; - } paths[packageJsonName] = [`${relativePath}/src`]; + paths[`${packageJsonName}/client`] = [`${relativePath}/src/client`]; }); const tsConfigJsonPath = join(cwd, './tsconfig.paths.json'); diff --git a/packages/core/client/src/application/PluginManager.ts b/packages/core/client/src/application/PluginManager.ts index 4255a8cc0..3e37b466a 100644 --- a/packages/core/client/src/application/PluginManager.ts +++ b/packages/core/client/src/application/PluginManager.ts @@ -45,7 +45,6 @@ export class PluginManager { const plugins = await getPlugins({ requirejs: this.app.requirejs, pluginData: pluginList, - baseURL: this.app.apiClient.axios?.defaults?.baseURL, devDynamicImport: this.app.devDynamicImport, }); for await (const plugin of plugins) { diff --git a/packages/core/client/src/application/utils/remotePlugins.ts b/packages/core/client/src/application/utils/remotePlugins.ts index 2b1f94b30..ae2ada08e 100644 --- a/packages/core/client/src/application/utils/remotePlugins.ts +++ b/packages/core/client/src/application/utils/remotePlugins.ts @@ -9,30 +9,40 @@ export function defineDevPlugins(plugins: Record) { }); } -export function getRemotePlugins( - requirejs: any, - pluginData: PluginData[] = [], - baseURL = '', -): Promise> { - if (baseURL.endsWith('/')) { - baseURL = baseURL.slice(0, -1); - } - if (baseURL.endsWith('/api')) { - baseURL = baseURL.slice(0, -4); - } +export function definePluginClient(packageName: string) { + window.define(`${packageName}/client`, ['exports', packageName], function (_exports: any, _plugin: any) { + Object.defineProperty(_exports, '__esModule', { + value: true, + }); + Object.keys(_plugin).forEach(function (key) { + if (key === 'default' || key === '__esModule') return; + if (key in _exports && _exports[key] === _plugin[key]) return; + Object.defineProperty(_exports, key, { + enumerable: true, + get: function () { + return _plugin[key]; + }, + }); + }); + }); +} +export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []): Promise> { requirejs.requirejs.config({ waitSeconds: 120, - paths: pluginData.reduce>((memo, item) => { - memo[item.packageName] = `${baseURL}${item.url}?noExt`; - memo[`${item.packageName}/client`] = `${baseURL}${item.url}?noExtAndIsClient`; - return memo; + paths: pluginData.reduce>((acc, cur) => { + acc[cur.packageName] = `${cur.url}?noExt`; + return acc; }, {}), }); + const packageNames = pluginData.map((item) => item.packageName); + packageNames.forEach((packageName) => { + definePluginClient(packageName); + }); return new Promise((resolve, reject) => { requirejs.requirejs( - pluginData.map((item) => item.packageName), + packageNames, (...plugins: (typeof Plugin & { default?: typeof Plugin })[]) => { const res = plugins.filter((item) => item).map((item) => item.default || item); resolve(res); @@ -56,12 +66,11 @@ export function getRemotePlugins( interface GetPluginsOption { requirejs: RequireJS; pluginData: PluginData[]; - baseURL?: string; devDynamicImport?: DevDynamicImport; } export async function getPlugins(options: GetPluginsOption): Promise> { - const { requirejs, pluginData, baseURL, devDynamicImport } = options; + const { requirejs, pluginData, devDynamicImport } = options; if (pluginData.length === 0) return []; @@ -87,10 +96,10 @@ export async function getPlugins(options: GetPluginsOption): Promise