From b4f0dfd05c50d501bd5960b246b5020790688f9a Mon Sep 17 00:00:00 2001 From: chencheng Date: Fri, 14 Aug 2020 16:38:09 +0800 Subject: [PATCH] refactor: api.modifyDepInfo -> api.addDepInfo --- docs/plugins/api.md | 28 +++++++++---------- docs/plugins/api.zh-CN.md | 28 +++++++++---------- .../src/plugins/generateFiles/umi.ts | 23 ++++++++------- .../src/plugins/registerMethods.ts | 2 +- packages/types/index.d.ts | 2 +- 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/docs/plugins/api.md b/docs/plugins/api.md index bb458ea76677..991fd02f10f4 100644 --- a/docs/plugins/api.md +++ b/docs/plugins/api.md @@ -231,6 +231,20 @@ api.addBeforeMiddewares(() => { }); ``` +### addDepInfo + +添加依赖信息,包括 semver range 和别名信息。 + +```js +api.addDepInfo((memo) => { + return { + name: 'foo', + range: pkg.dependencies.foo, + alias: [pathToFooPackage], + }; +}); +``` + ### addEntryCode 在入口文件最后添加代码。 @@ -642,20 +656,6 @@ api.modifyDefaultConfig((memo) => { }); ``` -### modifyDepInfo - -添加依赖信息,包括 semver range 和别名信息。 - -```js -api.modifyDepInfo((memo) => { - memo['foo'] = { - range: pkg.dependencies.foo, - alias: [pathToFooPackage], - }; - return memo; -}); -``` - ### modifyHTML 修改 HTML,基于 [cheerio](https://github.com/cheeriojs/cheerio) 的 ast。 diff --git a/docs/plugins/api.zh-CN.md b/docs/plugins/api.zh-CN.md index e97774800cf3..e1adb9fd5653 100644 --- a/docs/plugins/api.zh-CN.md +++ b/docs/plugins/api.zh-CN.md @@ -232,6 +232,20 @@ api.addBeforeMiddewares(() => { }); ``` +### addDepInfo + +添加依赖信息,包括 semver range 和别名信息。 + +```js +api.addDepInfo((memo) => { + return { + name: 'foo', + range: pkg.dependencies.foo, + alias: [pathToFooPackage], + }; +}); +``` + ### addEntryCode 在入口文件最后添加代码。 @@ -643,20 +657,6 @@ api.modifyDefaultConfig((memo) => { }); ``` -### modifyDepInfo - -添加依赖信息,包括 semver range 和别名信息。 - -```js -api.modifyDepInfo((memo) => { - memo['foo'] = { - range: pkg.dependencies.foo, - alias: [pathToFooPackage], - }; - return memo; -}); -``` - ### modifyHTML 修改 HTML,基于 [cheerio](https://github.com/cheeriojs/cheerio) 的 ast。 diff --git a/packages/preset-built-in/src/plugins/generateFiles/umi.ts b/packages/preset-built-in/src/plugins/generateFiles/umi.ts index bf3ed21f8310..4281813814e3 100644 --- a/packages/preset-built-in/src/plugins/generateFiles/umi.ts +++ b/packages/preset-built-in/src/plugins/generateFiles/umi.ts @@ -22,16 +22,19 @@ export default function (api: IApi) { utils: { Mustache }, } = api; - api.modifyDepInfo((memo) => { - memo['@umijs/runtime'] = { - range: '3', - alias: [runtimePath], - }; - memo['@umijs/renderer-react'] = { - range: '3', - alias: [renderReactPath], - }; - return memo; + api.addDepInfo(() => { + return [ + { + name: '@umijs/runtime', + range: '3', + alias: [runtimePath], + }, + { + name: '@umijs/renderer-react', + range: '3', + alias: [renderReactPath], + }, + ]; }); api.onGenerateFiles(async (args) => { diff --git a/packages/preset-built-in/src/plugins/registerMethods.ts b/packages/preset-built-in/src/plugins/registerMethods.ts index cc9f6c2538f3..d5b913a04ff9 100644 --- a/packages/preset-built-in/src/plugins/registerMethods.ts +++ b/packages/preset-built-in/src/plugins/registerMethods.ts @@ -16,6 +16,7 @@ export default function (api: IApi) { 'onPatchRoutesBefore', 'onDevCompileDone', 'addBeforeMiddewares', + 'addDepInfo', 'addDevScripts', 'addMiddewares', 'addRuntimePlugin', @@ -42,7 +43,6 @@ export default function (api: IApi) { 'modifyBabelOpts', 'modifyBabelPresetOpts', 'modifyBundleImplementor', - 'modifyDepInfo', 'modifyHTMLChunks', 'modifyDevHTMLContent', 'modifyExportRouteMap', diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index c1caff0142df..f4f0cd9bb78e 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -191,7 +191,6 @@ export interface IApi extends PluginAPI { } >; modifyBundleImplementor: IModify; - modifyDepInfo: IModify; modifyConfig: IModify; modifyDefaultConfig: IModify; modifyHTML: IModify; @@ -217,6 +216,7 @@ export interface IApi extends PluginAPI { >; // ApplyPluginType.add + addDepInfo: IAdd; addDevScripts: IAdd; addHTMLHeadScripts: IAdd<{ route?: IRoute }, IScriptConfig>; addHTMLScripts: IAdd<{ route?: IRoute }, IScriptConfig>;