Skip to content

Commit

Permalink
Merge pull request #222 from umijs/feat/umi-plugin-routes
Browse files Browse the repository at this point in the history
feat: add umi-plugin-routes
  • Loading branch information
sorrycc authored Mar 15, 2018
2 parents f3d58ea + 312a56a commit 254f6e9
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 36 deletions.
5 changes: 0 additions & 5 deletions packages/af-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"scripts": {
"test": "ruban test --timeout 200000",
"debug": "ruban debug --timeout 200000",
"coveralls": "ruban coveralls"
},
"devDependencies": {
"glob": "^7.1.2",
"node-sass": "^4.7.2",
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-preset-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"scripts": {
"test": "ruban test",
"debug": "ruban debug",
"coveralls": "ruban coveralls"
},
"dependencies": {
"@babel/plugin-proposal-async-generator-functions": "^7.0.0-beta.31",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.31",
Expand Down
5 changes: 0 additions & 5 deletions packages/eslint-config-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"scripts": {
"test": "ruban test",
"debug": "ruban debug",
"coveralls": "ruban coveralls"
},
"dependencies": {
"eslint-config-react-app": "^2.0.0"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/umi-build-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
"bugs": {
"url": "http://github.com/umijs/umi/issues"
},
"scripts": {
"test": "ruban test",
"debug": "ruban debug",
"coveralls": "ruban coveralls"
},
"devDependencies": {
"glob": "^7.1.2"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/umi-build-dev/src/FilesGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ if (process.env.NODE_ENV === 'production') {
generateRouterJS() {
const { paths, config } = this.service;
const { absRouterJSPath } = paths;
const routes = getRouteConfig(paths, config);
const routes = this.service.applyPlugins('modifyRoutes', {
initialValue: getRouteConfig(paths, config),
});

this.service.setRoutes(routes);

Expand Down
8 changes: 2 additions & 6 deletions packages/umi-plugin-dva/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"name": "umi-plugin-dva",
"version": "0.3.3",
"main": "./lib/index.js",
"scripts": {
"test": "ruban test",
"debug": "ruban debug"
},
"dependencies": {
"dva": "^2.1.0",
"dva-loading": "^1.0.4",
Expand All @@ -14,9 +10,9 @@
},
"repository": {
"type": "git",
"url": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-yunfengdie"
"url": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-dva"
},
"homepage": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-yunfengdie",
"homepage": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-dva",
"authors": [
"chencheng <[email protected]> (https://github.com/sorrycc)"
],
Expand Down
23 changes: 23 additions & 0 deletions packages/umi-plugin-routes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "umi-plugin-routes",
"version": "0.1.0",
"main": "./lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-routes"
},
"homepage": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-routes",
"authors": [
"chencheng <[email protected]> (https://github.com/sorrycc)"
],
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"files": [
"lib",
"src"
],
"dependencies": {
"is-plain-object": "^2.0.4"
}
}
51 changes: 51 additions & 0 deletions packages/umi-plugin-routes/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import assert from 'assert';
import isPlainObject from 'is-plain-object';

function optsToArray(item) {
if (!item) return [];
if (Array.isArray(item)) {
return item;
} else {
return [item];
}
}

export default function(api, opts) {
api.register('modifyRoutes', ({ memo }) => {
// opts.exclude
memo = memo.filter(route => {
for (const exclude of optsToArray(opts.exclude)) {
if (typeof exclude === 'function' && exclude(route)) {
return false;
}
if (exclude instanceof RegExp && exclude.test(route.component)) {
return false;
}
}
return true;
});

// opts.include
for (const include of optsToArray(opts.include)) {
if (isPlainObject(include)) {
memo = [...memo, include];
}
if (typeof include === 'string') {
throw new Error(
'opts.include with string not support, please wait for next version.',
);
}
}

// opts.update
if (opts.update) {
assert(
typeof opts.update === 'function',
`opts.update should be function, but got ${opts.update}`,
);
memo = opts.update(memo);
}

return memo;
});
}
4 changes: 0 additions & 4 deletions packages/umi-plugin-yunfengdie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"name": "umi-plugin-yunfengdie",
"version": "0.4.1",
"main": "./lib/index.js",
"scripts": {
"test": "ruban test",
"debug": "ruban debug"
},
"dependencies": {
"debug": "^3.1.0"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"scripts": {
"test": "ruban test --timeout 200000",
"debug": "DISABLE_TRANSFORM_RUNTIME=1 ruban debug --timeout 200000",
"coveralls": "ruban coveralls"
},
"files": [
"lib",
"src",
Expand Down

0 comments on commit 254f6e9

Please sign in to comment.