From 275ee45929ec41f275ac0df7e2301bed6b3f6a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?chencheng=20=28=E4=BA=91=E8=B0=A6=29?= Date: Thu, 25 Oct 2018 16:06:51 +0800 Subject: [PATCH] feat: support modifyRouteProps in runtime (#1324) --- packages/umi-build-dev/src/FilesGenerator.js | 7 +++- packages/umi/src/renderRoutes.js | 15 ++++---- packages/umi/src/renderRoutes.test.js | 36 +++++++++++++++++++- packages/umi/src/runtimePlugin.js | 4 +-- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/packages/umi-build-dev/src/FilesGenerator.js b/packages/umi-build-dev/src/FilesGenerator.js index d76d70a533eb..4d9311416d17 100644 --- a/packages/umi-build-dev/src/FilesGenerator.js +++ b/packages/umi-build-dev/src/FilesGenerator.js @@ -158,7 +158,12 @@ export default class FilesGenerator { plugins.push('@/app'); } const validKeys = this.service.applyPlugins('addRuntimePluginKey', { - initialValue: ['patchRoutes', 'render', 'rootContainer'], + initialValue: [ + 'patchRoutes', + 'render', + 'rootContainer', + 'modifyRouteProps', + ], }); assert( uniq(validKeys).length === validKeys.length, diff --git a/packages/umi/src/renderRoutes.js b/packages/umi/src/renderRoutes.js index 0c8562d10b68..85582f189c0b 100644 --- a/packages/umi/src/renderRoutes.js +++ b/packages/umi/src/renderRoutes.js @@ -118,13 +118,16 @@ export default function renderRoutes( ...props, ...extraProps, }); + const newProps = window.g_plugins.apply('modifyRouteProps', { + initialValue: { + ...props, + ...extraProps, + ...compatProps, + }, + args: { route }, + }); return ( - + {childRoutes} ); diff --git a/packages/umi/src/renderRoutes.test.js b/packages/umi/src/renderRoutes.test.js index 1412b69d0180..377c97c92632 100644 --- a/packages/umi/src/renderRoutes.test.js +++ b/packages/umi/src/renderRoutes.test.js @@ -1,11 +1,15 @@ import React from 'react'; import TestRenderer from 'react-test-renderer'; import { MemoryRouter } from 'react-router-dom'; -import { mount, shallow } from 'enzyme'; import renderRoutes from './renderRoutes'; window.__UMI_BIGFISH_COMPAT = false; +window.g_plugins = { + apply(key, { initialValue }) { + return initialValue; + }, +}; describe('renderRoutes', () => { function Layout(props) { @@ -99,6 +103,10 @@ describe('renderRoutes', () => { ); } + function ShowFoo(props) { + return

foo: {props.foo || 'null'}

; + } + const routes = [ { path: '/', @@ -129,6 +137,7 @@ describe('renderRoutes', () => { { path: '/pass-props-from-layout', component: PassPropsRouteComponent }, ], }, + { path: '/g_plugins', component: ShowFoo }, { component: Fallback }, ]; @@ -249,4 +258,29 @@ describe('renderRoutes', () => { children: ['Fallback'], }); }); + + test('patch with g_plugins', () => { + const oldGPlugin = window.g_plugins; + window.g_plugins = { + apply(key, { initialValue }) { + if (key === 'modifyRouteProps') { + return { + ...initialValue, + foo: 'bar', + }; + } + }, + }; + const tr = TestRenderer.create( + + {renderRoutes(routes)} + , + ); + expect(tr.toJSON()).toEqual({ + type: 'h1', + props: {}, + children: ['foo: ', 'bar'], + }); + window.g_plugins = oldGPlugin; + }); }); diff --git a/packages/umi/src/runtimePlugin.js b/packages/umi/src/runtimePlugin.js index be32e2b6e868..4e5aca521e7e 100644 --- a/packages/umi/src/runtimePlugin.js +++ b/packages/umi/src/runtimePlugin.js @@ -41,12 +41,12 @@ export function compose(item, { initialValue }) { }; } -export function apply(item, { initialValue }) { +export function apply(item, { initialValue, args }) { if (typeof item === 'string') item = getItem(item); assert(Array.isArray(item), `item must be Array`); return item.reduce((memo, fn) => { assert(typeof fn === 'function', `applied item must be function`); - return fn(memo); + return fn(memo, args); }, initialValue); }