Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collect async hook support params and fix garfish test #6566

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/solutions/app-tools/src/new/compat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export function getHookRunners(
beforePrintInstructions: async (params: { instructions: string }) => {
return hooks.onBeforePrintInstructions.call(params);
},
// garfish plugin hooks
appendEntryCode: async (params: {
entrypoint: Entrypoint;
code: string;
}) => {
const result = await (hooks as any)?.appendEntryCode.call(params);
return result;
},

/**
* common hooks
Expand Down
10 changes: 10 additions & 0 deletions packages/solutions/app-tools/src/new/compat/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { createCollectAsyncHook } from '@modern-js/plugin-v2';
import type { InternalContext, Plugin } from '@modern-js/plugin-v2/types';
import type { Entrypoint } from '@modern-js/types';
import type {
AppToolsNormalizedConfig,
AppToolsUserConfig,
} from '../../types/config';
import type { AppTools, AppToolsExtendAPIName } from '../types';
import { getHookRunners } from './hooks';

type AppendEntryCodeFn = (params: {
entrypoint: Entrypoint;
code: string;
}) => string | Promise<string>;

export const compatPlugin = (): Plugin<
AppTools<'shared'>,
InternalContext<
Expand Down Expand Up @@ -38,5 +45,8 @@ export const compatPlugin = (): Plugin<
},
};
},
registryHooks: {
appendEntryCode: createCollectAsyncHook<AppendEntryCodeFn>(),
},
setup: _api => {},
});
6 changes: 3 additions & 3 deletions packages/toolkit/plugin-v2/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ export function createAsyncHook<
}

export function createCollectAsyncHook<
Callback extends () => any,
Callback extends (...params: any[]) => any,
>(): CollectAsyncHook<Callback> {
const callbacks: Callback[] = [];

const tap = (cb: Callback) => {
callbacks.push(cb);
};

const call = async () => {
const call = async (...params: Parameters<Callback>) => {
const results: UnwrapPromise<ReturnType<Callback>>[] = [];
for (const callback of callbacks) {
const result = await callback();
const result = await callback(params);

if (result !== undefined) {
results.push(result);
Expand Down
6 changes: 4 additions & 2 deletions packages/toolkit/plugin-v2/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export type AsyncHook<Callback extends (...args: any[]) => any> = {
call: (...args: Parameters<Callback>) => Promise<Parameters<Callback>>;
};

export type CollectAsyncHook<Callback extends () => any> = {
export type CollectAsyncHook<Callback extends (...params: any[]) => any> = {
tap: (cb: Callback) => void;
call: () => Promise<UnwrapPromise<ReturnType<Callback>>[]>;
call: (
...params: Parameters<Callback>
) => Promise<UnwrapPromise<ReturnType<Callback>>[]>;
};

export type PluginHook<Callback extends (...args: any[]) => any> =
Expand Down
Loading