Skip to content

Commit

Permalink
feat: joi schema generate config types (#4325)
Browse files Browse the repository at this point in the history
* feat: joi schema generate config types

* fix: types

* fix: deps

* fix: lodash

* fix: schema validate

* fix: add config types

* feat: support types comments

* fix: types from @umijs/types

* fix: config types

* fix: types

* feat: routes types

* fix: additionalProperties

* fix: joi2types version

* fix: auto generate types, no need to define

* fix: joi spell

* fix: types
  • Loading branch information
信鑫-King committed Apr 7, 2020
1 parent 0bc87c2 commit 443e8c2
Show file tree
Hide file tree
Showing 10 changed files with 889 additions and 1,138 deletions.
2 changes: 2 additions & 0 deletions packages/preset-built-in/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"access": "public"
},
"dependencies": {
"@hapi/joi": "16.1.8",
"@types/multer": "1.4.2",
"@types/react-router-config": "5.0.1",
"@umijs/babel-preset-umi": "3.0.18",
Expand All @@ -38,6 +39,7 @@
"es5-imcompatible-versions": "^0.1.58",
"fork-ts-checker-webpack-plugin": "4.1.2",
"history-with-query": "4.10.3",
"joi2types": "~1.0.0",
"multer": "1.4.2",
"path-to-regexp": "^1.7.0",
"react": "^16.13.1",
Expand Down
1 change: 1 addition & 0 deletions packages/preset-built-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function () {
require.resolve('./plugins/generateFiles/core/polyfill'),
require.resolve('./plugins/generateFiles/core/routes'),
require.resolve('./plugins/generateFiles/core/umiExports'),
require.resolve('./plugins/generateFiles/core/configTypes'),
require.resolve('./plugins/generateFiles/umi'),

// bundle configs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (api: IApi) {
config: {
schema(joi) {
return joi.object().keys({
exclude: joi.array().items(joi.string()),
exclude: joi.array().items(joi.string()).description('exclude files not parse mock'),
});
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default (api: IApi) => {
flexbox: 'no-2009',
},
schema(joi) {
return joi.object();
return joi.object().description('postcss autoprefixer, default flexbox: no-2009');
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-built-in/src/plugins/features/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export default (api: IApi) => {
schema(joi) {
return joi
.object({
port: joi.number(),
port: joi.number().description('devServer port, default 8000'),
host: joi.string(),
https: joi.alternatives(joi.object(), joi.boolean()),
http2: joi.boolean(),
headers: joi.object(),
})
}).description('devServer configs')
.unknown(true);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default (api: IApi) => {
config: {
schema(joi) {
return joi.object({
loading: joi.string(),
});
loading: joi.string().description('loading the component before loaded'),
}).description('Code splitting for performance optimization');
},
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IApi } from '@umijs/types';
import joi2Types from 'joi2types';
import joi from '@hapi/joi';

export default (api: IApi) => {
api.onGenerateFiles(async () => {
const { service } = api;
const properties = Object.keys(service.plugins).map(plugin => {
const { config, key } = service.plugins[plugin];
// recognize as key if have schema config
if (!config?.schema) return;
const schema = config.schema(joi);
if (!joi.isSchema(schema)) {
return;
}
return {
[key]: schema,
};
})
.reduce((acc, curr) => ({
...acc,
...curr,
}), {});
const content = await joi2Types(joi.object(properties).unknown(), {
interfaceName: 'IConfigFromPlugins',
bannerComment: '/** Created by Umi Plugin **/',
});
api.writeTmpFile({
path: 'core/pluginConfig.d.ts',
content,
})
})
};
9 changes: 8 additions & 1 deletion packages/preset-built-in/src/plugins/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ export default function (api: IApi) {
key: 'routes',
config: {
schema(joi) {
return joi.array().items(joi.object());
return joi.array().items(joi.object({
path: joi.string().description('Any valid URL path'),
component: joi.alternatives(joi.string(), joi.function()).description('A React component to render only when the location matches.'),
wrappers: joi.array().items(joi.string()),
redirect: joi.string().description('navigate to a new location'),
exact: joi.boolean().description('When true, the active class/style will only be applied if the location is matched exactly.'),
routes: joi.array().items(joi.link('...')),
}).unknown());
},
onChange: api.ConfigChangeType.regenerateTmpFiles,
},
Expand Down
5 changes: 4 additions & 1 deletion packages/umi/src/defineConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { IConfig } from '@umijs/types';
// @ts-ignore
import { IConfigFromPlugins } from '@@/core/pluginConfig';

export function defineConfig(config: IConfig) {
// IConfig types is prior to IConfigFromPlugins in the same key.
export function defineConfig(config: IConfigFromPlugins | IConfig): IConfigFromPlugins | IConfig {
return config;
}
Loading

0 comments on commit 443e8c2

Please sign in to comment.