Skip to content

Commit

Permalink
feat: new plugin api, api.modifyDepInfo (#5207)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Aug 12, 2020
1 parent bae9097 commit 1e57afc
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 19 deletions.
14 changes: 14 additions & 0 deletions docs/plugins/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ 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。
Expand Down
14 changes: 14 additions & 0 deletions docs/plugins/api.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,20 @@ 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。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { winPath } from '@umijs/utils';
import { dirname } from 'path';

export const runtimePath = winPath(
dirname(require.resolve('@umijs/runtime/package.json')),
);
export const renderReactPath = winPath(
require.resolve('@umijs/renderer-react'),
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { join } from 'path';
import { IApi } from '@umijs/types';
import { winPath } from '@umijs/utils';
import { runtimePath } from '../constants';

export default function (api: IApi) {
const {
Expand Down Expand Up @@ -53,9 +53,7 @@ export default function (api: IApi) {
null,
2,
),
runtimePath: winPath(
dirname(require.resolve('@umijs/runtime/package.json')),
),
runtimePath,
}),
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { join } from 'path';
import { IApi } from '@umijs/types';
import { getFile, winPath } from '@umijs/utils';
import { runtimePath } from '../constants';

export default function (api: IApi) {
const {
Expand Down Expand Up @@ -38,9 +39,7 @@ export default function (api: IApi) {
readFileSync(join(__dirname, 'plugin.tpl'), 'utf-8'),
{
validKeys,
runtimePath: winPath(
dirname(require.resolve('@umijs/runtime/package.json')),
),
runtimePath,
},
),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { IApi, IConfig } from '@umijs/types';
import { join } from 'path';
import { IApi } from '@umijs/types';
import { Route } from '@umijs/core';
import { winPath } from '@umijs/utils';
import { runtimePath } from '../constants';

export default function (api: IApi) {
const {
Expand All @@ -17,9 +17,7 @@ export default function (api: IApi) {
path: 'core/routes.ts',
content: Mustache.render(routesTpl, {
routes: new Route().getJSON({ routes, config: api.config, cwd }),
runtimePath: winPath(
dirname(require.resolve('@umijs/runtime/package.json')),
),
runtimePath,
config: api.config,
}),
});
Expand Down
19 changes: 15 additions & 4 deletions packages/preset-built-in/src/plugins/generateFiles/umi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { IApi } from '@umijs/types';
import { winPath } from '@umijs/utils';
import { runtimePath, renderReactPath } from './constants';

export function importsToStr(
imports: { source: string; specifier?: string }[],
Expand All @@ -21,12 +22,24 @@ 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.onGenerateFiles(async (args) => {
const umiTpl = readFileSync(join(__dirname, 'umi.tpl'), 'utf-8');
const rendererPath = await api.applyPlugins({
key: 'modifyRendererPath',
type: api.ApplyPluginsType.modify,
initialValue: require.resolve('@umijs/renderer-react'),
initialValue: renderReactPath,
});
api.writeTmpFile({
path: 'umi.ts',
Expand All @@ -35,9 +48,7 @@ export default function (api: IApi) {
enableTitle: api.config.title !== false,
defaultTitle: api.config.title || '',
rendererPath: winPath(rendererPath),
runtimePath: winPath(
dirname(require.resolve('@umijs/runtime/package.json')),
),
runtimePath,
rootElement: api.config.mountElementId,
enableSSR: !!api.config.ssr,
dynamicImport: !!api.config.dynamicImport,
Expand Down
1 change: 1 addition & 0 deletions packages/preset-built-in/src/plugins/registerMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function (api: IApi) {
'modifyBabelOpts',
'modifyBabelPresetOpts',
'modifyBundleImplementor',
'modifyDepInfo',
'modifyHTMLChunks',
'modifyDevHTMLContent',
'modifyExportRouteMap',
Expand Down
1 change: 1 addition & 0 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export interface IApi extends PluginAPI {
}
>;
modifyBundleImplementor: IModify<any, {}>;
modifyDepInfo: IModify<any, {}>;
modifyConfig: IModify<IConfig, {}>;
modifyDefaultConfig: IModify<IConfig, {}>;
modifyHTML: IModify<CheerioStatic, { route: IRoute }>;
Expand Down

0 comments on commit 1e57afc

Please sign in to comment.