diff --git a/.changeset/long-cycles-admire.md b/.changeset/long-cycles-admire.md new file mode 100644 index 000000000000..016282f85ecf --- /dev/null +++ b/.changeset/long-cycles-admire.md @@ -0,0 +1,8 @@ +--- +'@modern-js/app-tools': patch +'@modern-js/main-doc': patch +--- + +feat: deprecate beforeConfig hook + +feat: 废弃 beforeConfig Hook diff --git a/packages/document/main-doc/docs/en/plugin/plugin-system/hook-list.mdx b/packages/document/main-doc/docs/en/plugin/plugin-system/hook-list.mdx index 422c45821a6a..9ac79744acb5 100644 --- a/packages/document/main-doc/docs/en/plugin/plugin-system/hook-list.mdx +++ b/packages/document/main-doc/docs/en/plugin/plugin-system/hook-list.mdx @@ -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` -- Example: - -```ts -import type { CliPlugin } from '@modern-js/core'; - -export const myPlugin = (): CliPlugin => ({ - setup(api) { - return { - beforeConfig: () => { - // do something - }, - }; - }, -}); -``` - ### `config` - Functionality: Collect configuration diff --git a/packages/document/main-doc/docs/zh/plugin/plugin-system/hook-list.mdx b/packages/document/main-doc/docs/zh/plugin/plugin-system/hook-list.mdx index 4d73474db7ca..8974f5594cdd 100644 --- a/packages/document/main-doc/docs/zh/plugin/plugin-system/hook-list.mdx +++ b/packages/document/main-doc/docs/zh/plugin/plugin-system/hook-list.mdx @@ -12,28 +12,6 @@ Modern.js 工程体系中包含三类插件:CLI、Runtime、Server,每一类 以下是通用的 CLI Hooks,可以在 Modern.js Framework 以及 Modern.js Module 中使用。 -### `beforeConfig` - -- 功能:运行收集配置前的任务 -- 执行阶段:收集配置前 -- Hook 模型:`AsyncWorkflow` -- 类型:`AsyncWorkflow` -- 使用示例: - -```ts -import type { CliPlugin } from '@modern-js/core'; - -export const myPlugin = (): CliPlugin => ({ - setup(api) { - return { - beforeConfig: () => { - // do something - }, - }; - }, -}); -``` - ### `config` - 功能:收集配置 diff --git a/packages/solutions/app-tools/src/compat/hooks.ts b/packages/solutions/app-tools/src/compat/hooks.ts index f2e9b74b0248..6020fd15665e 100644 --- a/packages/solutions/app-tools/src/compat/hooks.ts +++ b/packages/solutions/app-tools/src/compat/hooks.ts @@ -28,9 +28,6 @@ export function getHookRunners( /** * app tools hooks */ - beforeConfig: async () => { - return hooks.onBeforeConfig.call(); - }, afterPrepare: async () => { return hooks.onAfterPrepare.call(); }, @@ -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)); + } + }); + } } } }); diff --git a/packages/solutions/app-tools/src/compat/utils.ts b/packages/solutions/app-tools/src/compat/utils.ts index 0ac75ddacab0..52e3544b845d 100644 --- a/packages/solutions/app-tools/src/compat/utils.ts +++ b/packages/solutions/app-tools/src/compat/utils.ts @@ -6,7 +6,10 @@ import { getModifyHtmlPartials } from '../plugins/analyze/getHtmlTemplate'; export function transformHookRunner(hookRunnerName: string) { switch (hookRunnerName) { case 'beforeConfig': - return 'onBeforeConfig'; + console.error( + 'The `beforeConfig` hook has been deprecated. Please define your code directly in the setup function instead.', + ); + return undefined; case 'prepare': return 'onPrepare'; case 'afterPrepare': diff --git a/packages/solutions/app-tools/src/index.ts b/packages/solutions/app-tools/src/index.ts index 5be38e12bd05..cf0ad18da201 100644 --- a/packages/solutions/app-tools/src/index.ts +++ b/packages/solutions/app-tools/src/index.ts @@ -31,7 +31,6 @@ import type { AppTools, AppToolsOptions, CliPluginFuture } from './types'; import type { AddRuntimeExportsFn, AfterPrepareFn, - BeforeConfigFn, BeforeGenerateRoutesFn, BeforePrintInstructionsFn, CheckEntryPointFn, @@ -88,7 +87,6 @@ export const appTools = ( '@modern-js/plugin-polyfill', ], registryHooks: { - onBeforeConfig: createAsyncHook(), onAfterPrepare: createAsyncHook(), deploy: createAsyncHook(), _internalRuntimePlugins: createAsyncHook(), diff --git a/packages/solutions/app-tools/src/types/new.ts b/packages/solutions/app-tools/src/types/new.ts index dd72414103e8..c46b8ada8fc8 100644 --- a/packages/solutions/app-tools/src/types/new.ts +++ b/packages/solutions/app-tools/src/types/new.ts @@ -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; export type AfterPrepareFn = () => Promise | void; export type InternalRuntimePluginsFn = TransformFunction<{ entrypoint: Entrypoint; @@ -64,7 +63,6 @@ export type RegisterBuildPlatformFn = () => export type AddRuntimeExportsFn = () => Promise | void; export interface AppToolsExtendAPI { - onBeforeConfig: PluginHookTap; onAfterPrepare: PluginHookTap; deploy: PluginHookTap; @@ -116,7 +114,6 @@ export interface AppToolsExtendAPI { export interface AppToolsExtendHooks extends Record any>> { - onBeforeConfig: AsyncHook; onAfterPrepare: AsyncHook; deploy: AsyncHook; _internalRuntimePlugins: AsyncHook;