feat: improve code

This commit is contained in:
chenos 2022-03-19 19:28:53 +08:00
parent 019d182af2
commit 6a7aa22718
5 changed files with 27 additions and 3 deletions

View File

@ -41,7 +41,6 @@ const plugins = [
'@nocobase/plugin-collection-manager', '@nocobase/plugin-collection-manager',
'@nocobase/plugin-ui-schema-storage', '@nocobase/plugin-ui-schema-storage',
'@nocobase/plugin-ui-routes-storage', '@nocobase/plugin-ui-routes-storage',
'@nocobase/plugin-client',
'@nocobase/plugin-file-manager', '@nocobase/plugin-file-manager',
'@nocobase/plugin-system-settings', '@nocobase/plugin-system-settings',
'@nocobase/plugin-users', '@nocobase/plugin-users',
@ -53,6 +52,10 @@ for (const plugin of plugins) {
api.plugin(require(plugin).default); api.plugin(require(plugin).default);
} }
api.plugin(require('@nocobase/plugin-client').default, {
dist: resolve(__dirname, '../../app/dist'),
});
if (process.argv.length < 3) { if (process.argv.length < 3) {
// @ts-ignore // @ts-ignore
process.argv.push('start', '--port', process.env.API_PORT || 12302); process.argv.push('start', '--port', process.env.API_PORT || 12302);

View File

@ -13,6 +13,7 @@ const umiConfig = getUmiConfig();
export default defineConfig({ export default defineConfig({
define: { define: {
'process.env.NOCOBASE_ENV': process.env.NOCOBASE_ENV,
...umiConfig.define, ...umiConfig.define,
}, },
// only proxy when using `umi dev` // only proxy when using `umi dev`

View File

@ -35,6 +35,8 @@ import { I18nextProvider } from 'react-i18next';
import { Link, NavLink } from 'react-router-dom'; import { Link, NavLink } from 'react-router-dom';
import apiClient from './apiClient'; import apiClient from './apiClient';
console.log = (...args) => null;
apiClient.axios.interceptors.response.use( apiClient.axios.interceptors.response.use(
(response) => response, (response) => response,
(error) => { (error) => {

View File

@ -1,6 +1,4 @@
import debug from 'debug';
import './global.less'; import './global.less';
debug.log = console.log.bind(console);
export * from './acl'; export * from './acl';
export * from './antd-config-provider'; export * from './antd-config-provider';

View File

@ -1,5 +1,8 @@
import { skip } from '@nocobase/acl'; import { skip } from '@nocobase/acl';
import { Plugin } from '@nocobase/server'; import { Plugin } from '@nocobase/server';
import send from 'koa-send';
import serve from 'koa-static';
import { resolve } from 'path';
export class ClientPlugin extends Plugin { export class ClientPlugin extends Plugin {
async beforeLoad() { 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 });
}
});
} }
} }