Skip to content

Commit

Permalink
feat: handleSetupResult
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Feb 21, 2025
1 parent 479be73 commit 47cbe16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/server/core/src/plugins/compat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,36 @@ export function getHookRunners(
},
};
}

export function transformHookRunner(hookRunnerName: string) {
switch (hookRunnerName) {
case 'config':
return 'modifyConfig';
case 'prepare':
return 'onPrepare';
case 'reset':
return 'onReset';
default:
return hookRunnerName;
}
}
export function handleSetupResult(
setupResult: Record<string, (...args: any) => any>,
api: Record<string, any>,
) {
if (!setupResult) {
return;
}
Object.keys(setupResult).forEach(key => {
const fn = setupResult[key];
if (typeof fn === 'function') {
const newAPI = transformHookRunner(key);
if (api[newAPI]) {
api[newAPI]((...params: any) => {
const res = fn(...params);
return res;
});
}
}
});
}
2 changes: 2 additions & 0 deletions packages/server/core/src/serverBase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Plugin } from '@modern-js/plugin-v2';
import { type ServerCreateOptions, server } from '@modern-js/plugin-v2/server';
import { Hono, type MiddlewareHandler } from 'hono';
import { handleSetupResult } from './plugins/compat/hooks';
import type {
Env,
ServerConfig,
Expand Down Expand Up @@ -55,6 +56,7 @@ export class ServerBase<E extends Env = any> {
plugins: this.plugins as Plugin[],
options: this.options,
config: mergedConfig,
handleSetupResult,
});
serverContext.serverBase = this;
serverContext.middlewares = [];
Expand Down

0 comments on commit 47cbe16

Please sign in to comment.