From 6a7aa22718dd4cd537fe7243fbe93f080e1d31d6 Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 19 Mar 2022 19:28:53 +0800 Subject: [PATCH] feat: improve code --- packages/api/src/index.ts | 5 ++++- packages/app/.umirc.ts | 1 + packages/app/src/pages/index.tsx | 2 ++ packages/client/src/index.tsx | 2 -- packages/plugin-client/src/plugin.ts | 20 ++++++++++++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index d6aadecfd..b963ba172 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -41,7 +41,6 @@ const plugins = [ '@nocobase/plugin-collection-manager', '@nocobase/plugin-ui-schema-storage', '@nocobase/plugin-ui-routes-storage', - '@nocobase/plugin-client', '@nocobase/plugin-file-manager', '@nocobase/plugin-system-settings', '@nocobase/plugin-users', @@ -53,6 +52,10 @@ for (const plugin of plugins) { api.plugin(require(plugin).default); } +api.plugin(require('@nocobase/plugin-client').default, { + dist: resolve(__dirname, '../../app/dist'), +}); + if (process.argv.length < 3) { // @ts-ignore process.argv.push('start', '--port', process.env.API_PORT || 12302); diff --git a/packages/app/.umirc.ts b/packages/app/.umirc.ts index e14479f15..f16f7cc5c 100644 --- a/packages/app/.umirc.ts +++ b/packages/app/.umirc.ts @@ -13,6 +13,7 @@ const umiConfig = getUmiConfig(); export default defineConfig({ define: { + 'process.env.NOCOBASE_ENV': process.env.NOCOBASE_ENV, ...umiConfig.define, }, // only proxy when using `umi dev` diff --git a/packages/app/src/pages/index.tsx b/packages/app/src/pages/index.tsx index 0332f8a56..d7f71112f 100644 --- a/packages/app/src/pages/index.tsx +++ b/packages/app/src/pages/index.tsx @@ -35,6 +35,8 @@ import { I18nextProvider } from 'react-i18next'; import { Link, NavLink } from 'react-router-dom'; import apiClient from './apiClient'; +console.log = (...args) => null; + apiClient.axios.interceptors.response.use( (response) => response, (error) => { diff --git a/packages/client/src/index.tsx b/packages/client/src/index.tsx index 57b80efd9..51eb7f64b 100644 --- a/packages/client/src/index.tsx +++ b/packages/client/src/index.tsx @@ -1,6 +1,4 @@ -import debug from 'debug'; import './global.less'; -debug.log = console.log.bind(console); export * from './acl'; export * from './antd-config-provider'; diff --git a/packages/plugin-client/src/plugin.ts b/packages/plugin-client/src/plugin.ts index 1f4686481..b1e2f9dee 100644 --- a/packages/plugin-client/src/plugin.ts +++ b/packages/plugin-client/src/plugin.ts @@ -1,5 +1,8 @@ import { skip } from '@nocobase/acl'; import { Plugin } from '@nocobase/server'; +import send from 'koa-send'; +import serve from 'koa-static'; +import { resolve } from 'path'; export class ClientPlugin extends Plugin { async beforeLoad() { @@ -36,6 +39,23 @@ export class ClientPlugin extends Plugin { }, }, }); + let root = this.options.dist; + if (root && !root.startsWith('/')) { + root = resolve(process.cwd(), root); + } + this.app.middleware.unshift(async (ctx, next) => { + if (process.env.NOCOBASE_ENV === 'production') { + return next(); + } + if (!root) { + return next(); + } + await serve(root)(ctx, next); + // console.log('koa-send', root, ctx.status); + if (ctx.status == 404) { + return send(ctx, 'index.html', { root }); + } + }); } }