fix: gen tsconfig.paths.json bug (#2708)

* fix: gen tsconfig.paths.json  bug

* fix: load remote plugin error

* fix: bug
This commit is contained in:
jack zhang 2023-09-23 10:52:14 +08:00 committed by GitHub
parent 8fe4640f79
commit 836b34a6a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 28 deletions

View File

@ -190,13 +190,8 @@ exports.genTsConfigPaths = function genTsConfigPaths() {
const packageJsonName = require(packageFile).name; const packageJsonName = require(packageFile).name;
const packageDir = dirname(packageFile); const packageDir = dirname(packageFile);
const relativePath = packageDir.slice(cwdLength + 1).replace(/\\/, '/'); 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] = [`${relativePath}/src`];
paths[`${packageJsonName}/client`] = [`${relativePath}/src/client`];
}); });
const tsConfigJsonPath = join(cwd, './tsconfig.paths.json'); const tsConfigJsonPath = join(cwd, './tsconfig.paths.json');

View File

@ -45,7 +45,6 @@ export class PluginManager {
const plugins = await getPlugins({ const plugins = await getPlugins({
requirejs: this.app.requirejs, requirejs: this.app.requirejs,
pluginData: pluginList, pluginData: pluginList,
baseURL: this.app.apiClient.axios?.defaults?.baseURL,
devDynamicImport: this.app.devDynamicImport, devDynamicImport: this.app.devDynamicImport,
}); });
for await (const plugin of plugins) { for await (const plugin of plugins) {

View File

@ -9,30 +9,40 @@ export function defineDevPlugins(plugins: Record<string, typeof Plugin>) {
}); });
} }
export function getRemotePlugins( export function definePluginClient(packageName: string) {
requirejs: any, window.define(`${packageName}/client`, ['exports', packageName], function (_exports: any, _plugin: any) {
pluginData: PluginData[] = [], Object.defineProperty(_exports, '__esModule', {
baseURL = '', value: true,
): Promise<Array<typeof Plugin>> { });
if (baseURL.endsWith('/')) { Object.keys(_plugin).forEach(function (key) {
baseURL = baseURL.slice(0, -1); if (key === 'default' || key === '__esModule') return;
} if (key in _exports && _exports[key] === _plugin[key]) return;
if (baseURL.endsWith('/api')) { Object.defineProperty(_exports, key, {
baseURL = baseURL.slice(0, -4); enumerable: true,
} get: function () {
return _plugin[key];
},
});
});
});
}
export function getRemotePlugins(requirejs: any, pluginData: PluginData[] = []): Promise<Array<typeof Plugin>> {
requirejs.requirejs.config({ requirejs.requirejs.config({
waitSeconds: 120, waitSeconds: 120,
paths: pluginData.reduce<Record<string, string>>((memo, item) => { paths: pluginData.reduce<Record<string, string>>((acc, cur) => {
memo[item.packageName] = `${baseURL}${item.url}?noExt`; acc[cur.packageName] = `${cur.url}?noExt`;
memo[`${item.packageName}/client`] = `${baseURL}${item.url}?noExtAndIsClient`; return acc;
return memo;
}, {}), }, {}),
}); });
const packageNames = pluginData.map((item) => item.packageName);
packageNames.forEach((packageName) => {
definePluginClient(packageName);
});
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
requirejs.requirejs( requirejs.requirejs(
pluginData.map((item) => item.packageName), packageNames,
(...plugins: (typeof Plugin & { default?: typeof Plugin })[]) => { (...plugins: (typeof Plugin & { default?: typeof Plugin })[]) => {
const res = plugins.filter((item) => item).map((item) => item.default || item); const res = plugins.filter((item) => item).map((item) => item.default || item);
resolve(res); resolve(res);
@ -56,12 +66,11 @@ export function getRemotePlugins(
interface GetPluginsOption { interface GetPluginsOption {
requirejs: RequireJS; requirejs: RequireJS;
pluginData: PluginData[]; pluginData: PluginData[];
baseURL?: string;
devDynamicImport?: DevDynamicImport; devDynamicImport?: DevDynamicImport;
} }
export async function getPlugins(options: GetPluginsOption): Promise<Array<typeof Plugin>> { export async function getPlugins(options: GetPluginsOption): Promise<Array<typeof Plugin>> {
const { requirejs, pluginData, baseURL, devDynamicImport } = options; const { requirejs, pluginData, devDynamicImport } = options;
if (pluginData.length === 0) return []; if (pluginData.length === 0) return [];
@ -87,10 +96,10 @@ export async function getPlugins(options: GetPluginsOption): Promise<Array<typeo
return plugins; return plugins;
} }
const remotePluginList = await getRemotePlugins(requirejs, remotePlugins, baseURL); const remotePluginList = await getRemotePlugins(requirejs, remotePlugins);
plugins.push(...remotePluginList); plugins.push(...remotePluginList);
return plugins; return plugins;
} }
return getRemotePlugins(requirejs, pluginData, baseURL); return getRemotePlugins(requirejs, pluginData);
} }

View File

@ -1,7 +1,7 @@
import { css, useAPIClient, useRequest } from '@nocobase/client'; import { css, useAPIClient, useRequest } from '@nocobase/client';
import { Select, Space, Spin, Typography } from 'antd'; import { Select, Space, Spin, Typography } from 'antd';
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { SwaggerUIBundle } from 'swagger-ui-dist'; import SwaggerUIBundle from 'swagger-ui-dist/swagger-ui-bundle';
import 'swagger-ui-dist/swagger-ui.css'; import 'swagger-ui-dist/swagger-ui.css';
import { useTranslation } from '../locale'; import { useTranslation } from '../locale';