Skip to content

Commit

Permalink
feat: deprecate beforeConfig hook
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Jan 7, 2025
1 parent fccc80d commit e7c42a4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 63 deletions.
8 changes: 8 additions & 0 deletions .changeset/long-cycles-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/app-tools': patch
'@modern-js/main-doc': patch
---

feat: deprecate beforeConfig hook

feat: 废弃 beforeConfig Hook
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@ In this chapter, all available Hooks are listed, and you can use the correspondi

The following are the common CLI Hooks that can be used in both Modern.js Framework and Modern.js Module.

### `beforeConfig`

- Functionality: Running tasks before the config process
- Execution phase: Before the config process
- Hook model: `AsyncWorkflow`
- Type: `AsyncWorkflow<void, void>`
- Example:

```ts
import type { CliPlugin } from '@modern-js/core';

export const myPlugin = (): CliPlugin => ({
setup(api) {
return {
beforeConfig: () => {
// do something
},
};
},
});
```

### `config`

- Functionality: Collect configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@ Modern.js 工程体系中包含三类插件:CLI、Runtime、Server,每一类

以下是通用的 CLI Hooks,可以在 Modern.js Framework 以及 Modern.js Module 中使用。

### `beforeConfig`

- 功能:运行收集配置前的任务
- 执行阶段:收集配置前
- Hook 模型:`AsyncWorkflow`
- 类型:`AsyncWorkflow<void, void>`
- 使用示例:

```ts
import type { CliPlugin } from '@modern-js/core';

export const myPlugin = (): CliPlugin => ({
setup(api) {
return {
beforeConfig: () => {
// do something
},
};
},
});
```

### `config`

- 功能:收集配置
Expand Down
29 changes: 14 additions & 15 deletions packages/solutions/app-tools/src/compat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ export function getHookRunners(
/**
* app tools hooks
*/
beforeConfig: async () => {
return hooks.onBeforeConfig.call();
},
afterPrepare: async () => {
return hooks.onAfterPrepare.call();
},
Expand Down Expand Up @@ -202,18 +199,20 @@ export function handleSetupResult(
const fn = setupResult[key];
if (typeof fn === 'function') {
const newAPI = transformHookRunner(key);
if (api[newAPI]) {
api[newAPI](async (...params: any) => {
const { isMultiple, params: transformParams } = transformHookParams(
key,
params,
);
if (isMultiple) {
return transformHookResult(key, await fn(...transformParams));
} else {
return transformHookResult(key, await fn(transformParams));
}
});
if (newAPI) {
if (api[newAPI]) {
api[newAPI](async (...params: any) => {
const { isMultiple, params: transformParams } = transformHookParams(
key,
params,
);
if (isMultiple) {
return transformHookResult(key, await fn(...transformParams));
} else {
return transformHookResult(key, await fn(transformParams));
}
});
}
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/solutions/app-tools/src/compat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getModifyHtmlPartials } from '../plugins/analyze/getHtmlTemplate';
export function transformHookRunner(hookRunnerName: string) {
switch (hookRunnerName) {
case 'beforeConfig':
return 'onBeforeConfig';
console.error('BeforeConfig Hook has deprecated');
return undefined;
case 'prepare':
return 'onPrepare';
case 'afterPrepare':
Expand Down
3 changes: 0 additions & 3 deletions packages/solutions/app-tools/src/types/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type { AppToolsNormalizedConfig, AppToolsUserConfig } from './config';
import type { RuntimePlugin } from './hooks';
import type { Bundler } from './utils';

export type BeforeConfigFn = () => Promise<void> | void;
export type AfterPrepareFn = () => Promise<void> | void;
export type InternalRuntimePluginsFn = TransformFunction<{
entrypoint: Entrypoint;
Expand Down Expand Up @@ -64,7 +63,6 @@ export type RegisterBuildPlatformFn = () =>
export type AddRuntimeExportsFn = () => Promise<void> | void;

export interface AppToolsExtendAPI<B extends Bundler = 'webpack'> {
onBeforeConfig: PluginHookTap<BeforeConfigFn>;
onAfterPrepare: PluginHookTap<AfterPrepareFn>;
deploy: PluginHookTap<DeplpoyFn>;

Expand Down Expand Up @@ -116,7 +114,6 @@ export interface AppToolsExtendAPI<B extends Bundler = 'webpack'> {

export interface AppToolsExtendHooks
extends Record<string, PluginHook<(...args: any[]) => any>> {
onBeforeConfig: AsyncHook<BeforeConfigFn>;
onAfterPrepare: AsyncHook<AfterPrepareFn>;
deploy: AsyncHook<DeplpoyFn>;
_internalRuntimePlugins: AsyncHook<InternalRuntimePluginsFn>;
Expand Down

0 comments on commit e7c42a4

Please sign in to comment.