Skip to content

Commit

Permalink
feat: support to configure extra plugins or presets
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Oct 24, 2022
1 parent 771f020 commit 119b6a0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
51 changes: 51 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,54 @@ export default {

1. 预打包的所有目标依赖,并自动 external 到输出目录
2. 当前项目 `package.json` 中声明的 `dependencies`

## 其他配置

### plugins

- 类型:`string[]`
- 默认值:`undefined`

配置额外的 father 插件,可以是插件的路径或者 NPM 包名,如果是相对路径则会从项目根目录开始找。

插件编写方式与 Umi 插件类似,可以在插件函数体中接收 `api` 参数来控制 father 的行为,例如写一个插件修改默认配置:

```ts
// plugin.ts
import type { IApi } from 'father';

export default (api: IApi) => {
api.modifyConfig((memo) => {
// 修改 father 配置
return memo;
});
};

// .fatherrc.ts
import { defineConfig } from 'father';

export default defineConfig({
plugins: ['./plugin.ts'],
});
```

### presets

- 类型:`string[]`
- 默认值:`undefined`

配置额外的 father 插件集,可以是插件集的路径或者 NPM 包名,如果是相对路径则会从项目根目录开始找。

插件集的编写方式与 Umi 插件集类似,可以在插件集函数中返回插件配置,例如:

```ts
// preset.ts
import type { IApi } from 'father';

export default (api: IApi) => {
return {
presets: [require.resolve('./other-preset')],
plugins: [require.resolve('./plugin-a'), require.resolve('./plugin-b')],
};
};
```
2 changes: 1 addition & 1 deletion src/doctor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default async (api: IApi): Promise<IDoctorReport> => {
[[], []],
);
const preBundleConfig = getPreBundleConfig({
userConfig: api.config.prebundle || {},
userConfig: api.config.prebundle || { deps: [] },
pkg: api.pkg,
cwd: api.cwd,
});
Expand Down
2 changes: 2 additions & 0 deletions src/features/configPlugins/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
.pattern(Joi.string(), Joi.string())
.optional(),
}),
plugins: (Joi) => Joi.array().items(Joi.string()),
presets: (Joi) => Joi.array().items(Joi.string()),
};
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { defineConfig } from './defineConfig';
export type {
IApi,
IBundlessLoader,
IFatherConfig,
IJSTransformer,
} from './types';
29 changes: 27 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Compiler } from '@umijs/bundler-webpack';
import type Autoprefixer from '@umijs/bundler-webpack/compiled/autoprefixer';
import type IWebpackChain from '@umijs/bundler-webpack/compiled/webpack-5-chain';
import type { IConfig as IBundlerWebpackConfig } from '@umijs/bundler-webpack/dist/types';
import type { IAdd, IServicePluginAPI, PluginAPI } from '@umijs/core';
import type { IAdd, IModify, IServicePluginAPI, PluginAPI } from '@umijs/core';
import type { ITransformerItem } from './builder/bundless/loaders/javascript';
import type { IBundleConfig, IBundlessConfig } from './builder/config';
import type { IDoctorReport } from './doctor';
Expand All @@ -15,7 +15,10 @@ export type {
} from './builder/bundless/loaders/types';

export type IApi = PluginAPI &
IServicePluginAPI & {
Omit<
IServicePluginAPI,
'modifyConfig' | 'modifyDefaultConfig' | 'config' | 'userConfig'
> & {
/**
* add bundless js transformer
*/
Expand Down Expand Up @@ -48,6 +51,18 @@ export type IApi = PluginAPI &
},
IDoctorReport | IDoctorReport[0] | void
>;

/**
* config modify methods definition
*/
modifyConfig: IModify<Omit<IFatherConfig, 'extends'>, null>;
modifyDefaultConfig: IModify<Omit<IFatherConfig, 'extends'>, null>;

/**
* config definition
*/
config: Omit<IFatherConfig, 'extends'>;
userConfig: IFatherConfig;
};

export enum IFatherBuildTypes {
Expand Down Expand Up @@ -250,4 +265,14 @@ export interface IFatherConfig extends IFatherBaseConfig {
* deps pre-bundle config
*/
prebundle?: IFatherPreBundleConfig;

/**
* extra plugins
*/
plugins?: string[];

/**
* extra presets
*/
presets?: string[];
}

0 comments on commit 119b6a0

Please sign in to comment.