Skip to content

Commit

Permalink
feat: compat plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Feb 21, 2025
1 parent e7ec73a commit 479be73
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 12 deletions.
10 changes: 0 additions & 10 deletions packages/server/core/src/plugins/compat.ts

This file was deleted.

52 changes: 52 additions & 0 deletions packages/server/core/src/plugins/compat/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { InternalServerContext, ResetEvent } from '@modern-js/plugin-v2';
import type {
AfterMatchContext,
AfterRenderContext,
AfterStreamingRenderContext,
} from '@modern-js/types';
import type {
APIServerStartInput,
FallbackInput,
ServerConfig,
ServerPluginExtends,
WebServerStartInput,
} from '../../types';
/**
* old plugin useHookRunners function result
*/
export function getHookRunners(
context: InternalServerContext<ServerPluginExtends>,
): Record<string, any> {
const { hooks } = context;
return {
config: (params: ServerConfig) => {
return hooks.modifyConfig.call(params);
},
prepare: () => {
return hooks.onPrepare.call();
},
reset: (params: {
event: ResetEvent;
}) => {
return hooks.onReset.call(params);
},
fallback: (input: FallbackInput) => {
return hooks.fallback.call(input);
},
prepareWebServer: (input: WebServerStartInput) => {
return hooks.prepareWebServer.call(input);
},
prepareApiServer: (input: APIServerStartInput) => {
return hooks.prepareApiServer.call(input);
},
afterMatch: (ctx: AfterMatchContext) => {
return hooks.afterMatch.call(ctx);
},
afterRender: (ctx: AfterRenderContext) => {
return hooks.afterRender.call(ctx);
},
afterStreamingRender: (ctx: AfterStreamingRenderContext) => {
return hooks.afterStreamingRender.call(ctx);
},
};
}
45 changes: 45 additions & 0 deletions packages/server/core/src/plugins/compat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createAsyncHook, createAsyncPipelineHook } from '@modern-js/plugin-v2';
import type {
AfterMatchFn,
AfterRenderFn,
AfterStreamingRenderContextFn,
FallbackFn,
PrepareApiServerFn,
PrepareWebServerFn,
ServerPluginFurure,
} from '../../types';
import { getHookRunners } from './hooks';

export const compatPlugin = (): ServerPluginFurure => ({
name: '@modern-js/server-compat',
registryHooks: {
fallback: createAsyncHook<FallbackFn>(),
prepareWebServer: createAsyncPipelineHook<PrepareWebServerFn>(),
prepareApiServer: createAsyncPipelineHook<PrepareApiServerFn>(),
afterMatch: createAsyncPipelineHook<AfterMatchFn>(),
afterRender: createAsyncPipelineHook<AfterRenderFn>(),
createAfterStreamingRender:
createAsyncPipelineHook<AfterStreamingRenderContextFn>(),
},
_registryApi: (getServerContext, updateServerContext) => {
const getInternalContext = () => {
return getServerContext()._internalContext!;
};
return {
useConfigContext: () => {
return getInternalContext().config;
},
useAppContext: () => {
const { _internalContext, ...serverContext } = getServerContext();
return serverContext;
},
setAppContext: context => {
return updateServerContext(context);
},
useHookRunners: () => {
return getHookRunners(getInternalContext());
},
};
},
setup: () => {},
});
1 change: 1 addition & 0 deletions packages/server/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {
createDefaultPlugins,
type CreateDefaultPluginsOptions,
} from './default';
export { compatPlugin } from './compat';
4 changes: 3 additions & 1 deletion packages/server/core/src/serverBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export class ServerBase<E extends Env = any> {
options: this.options,
config: mergedConfig,
});
serverContext.serverBase = this;
serverContext.middlewares = [];
this.serverContext = serverContext as unknown as ServerContext;
this.#applyMiddlewares();

return this;
}

addPlugins(plugins: ServerPlugin[]) {
addPlugins(plugins: (ServerPlugin | ServerPluginFurure)[]) {
this.plugins.push(...plugins);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/server/core/src/types/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export type {
NodeRequest,
NodeResponse,
FileChangeEvent,
FallbackInput,
WebServerStartInput,
APIServerStartInput,
} from './base';
4 changes: 3 additions & 1 deletion packages/server/prod-server/src/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ErrorDigest,
type NodeServer,
type ServerBase,
compatPlugin,
createDefaultPlugins,
createErrorHtml,
faviconPlugin,
Expand All @@ -15,7 +16,7 @@ import {
loadCacheConfig,
serverStaticPlugin,
} from '@modern-js/server-core/node';
import { Logger, createLogger, isProd } from '@modern-js/utils';
import { createLogger, isProd } from '@modern-js/utils';
import type { ProdServerOptions } from './types';

// Now we not use logger options, it can be implemented in the future
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function applyPlugins(

const loggerOptions = config.server.logger;
const plugins = [
compatPlugin(),
...(nodeServer ? [injectNodeSeverPlugin({ nodeServer })] : []),
...createDefaultPlugins({
cacheConfig,
Expand Down

0 comments on commit 479be73

Please sign in to comment.