Skip to content

Commit

Permalink
feat: support modifyRouteProps in runtime (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Oct 25, 2018
1 parent 3793b2f commit 275ee45
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/umi-build-dev/src/FilesGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions packages/umi/src/renderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ export default function renderRoutes(
...props,
...extraProps,
});
const newProps = window.g_plugins.apply('modifyRouteProps', {
initialValue: {
...props,
...extraProps,
...compatProps,
},
args: { route },
});
return (
<route.component
{...props}
{...extraProps}
{...compatProps}
route={route}
>
<route.component {...newProps} route={route}>
{childRoutes}
</route.component>
);
Expand Down
36 changes: 35 additions & 1 deletion packages/umi/src/renderRoutes.test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -99,6 +103,10 @@ describe('renderRoutes', () => {
);
}

function ShowFoo(props) {
return <h1>foo: {props.foo || 'null'}</h1>;
}

const routes = [
{
path: '/',
Expand Down Expand Up @@ -129,6 +137,7 @@ describe('renderRoutes', () => {
{ path: '/pass-props-from-layout', component: PassPropsRouteComponent },
],
},
{ path: '/g_plugins', component: ShowFoo },
{ component: Fallback },
];

Expand Down Expand Up @@ -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(
<MemoryRouter initialEntries={['/g_plugins']}>
{renderRoutes(routes)}
</MemoryRouter>,
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['foo: ', 'bar'],
});
window.g_plugins = oldGPlugin;
});
});
4 changes: 2 additions & 2 deletions packages/umi/src/runtimePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 275ee45

Please sign in to comment.