Skip to content

Commit

Permalink
fix: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Jan 17, 2025
1 parent f224ec0 commit 6786225
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/runtime/plugin-garfish/src/cli/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import type { CollectAsyncHook } from '@modern-js/plugin-v2';
import type { Entrypoint } from '@modern-js/types';
import { fs } from '@modern-js/utils';
import type { AppendEntryCodeFn } from '.';
import type { AppendEntryCodeFn } from './hooks';
import * as template from './template';
import { generateAsyncEntryCode } from './utils';

Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/plugin-garfish/src/cli/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Entrypoint } from '@modern-js/types';

export type AppendEntryCodeFn = (params: {
entrypoint: Entrypoint;
code: string;
}) => string | Promise<string>;
7 changes: 1 addition & 6 deletions packages/runtime/plugin-garfish/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {
} from '@modern-js/app-tools';
import type { CliHookCallbacks, useConfigContext } from '@modern-js/core';
import { createCollectAsyncHook } from '@modern-js/plugin-v2';
import type { Entrypoint } from '@modern-js/types';
import { createRuntimeExportsUtils, getEntryOptions } from '@modern-js/utils';
import { logger } from '../util';
import { generateCode } from './code';
import type { AppendEntryCodeFn } from './hooks';
import { getRuntimeConfig, setRuntimeConfig } from './utils';

export type UseConfig = ReturnType<typeof useConfigContext>;
Expand Down Expand Up @@ -39,11 +39,6 @@ export function getDefaultMicroFrontedConfig(
};
}

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

export const garfishPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
name: '@modern-js/plugin-garfish',
pre: ['@modern-js/runtime'],
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/plugin-runtime/src/core/browser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export async function render(
const context: RuntimeContext = getInitialContext();
const runBeforeRender = async (context: RuntimeContext) => {
const internalRuntimeContext = getGlobalInternalRuntimeContext();
const api = internalRuntimeContext?.pluginAPI;
api?.updateRuntimeContext(context);
const hooks = internalRuntimeContext?.hooks;
const api = internalRuntimeContext!.pluginAPI;
api!.updateRuntimeContext(context);
const hooks = internalRuntimeContext!.hooks;
await hooks.onBeforeRender.call(context);
const init = getGlobalAppInit();
return init?.(context);
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-runtime/src/core/compatible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const bootstrap: BootStrap = async (
const hooks = internalRuntimeContext.hooks;

const context: RuntimeContext = getInitialContext();
api?.updateRuntimeContext(context);
api!.updateRuntimeContext(context);

const runBeforeRender = async (context: RuntimeContext) => {
await hooks.onBeforeRender.call(context);
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/plugin-runtime/src/core/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { InternalRuntimeContext, Plugin } from '@modern-js/plugin-v2';
import type { Plugin } from '@modern-js/plugin-v2';
import type { InternalRuntimeContext } from '@modern-js/plugin-v2/runtime';
import { runtime } from '@modern-js/plugin-v2/runtime';
import { merge } from '@modern-js/runtime-utils/merge';
import { compatPlugin } from '../compat';
Expand Down
4 changes: 1 addition & 3 deletions packages/runtime/plugin-runtime/src/router/runtime/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import type { RuntimeContext } from '../../core';
// only for inhouse use
const modifyRoutes = createSyncHook<(routes: RouteObject[]) => RouteObject[]>();
const onBeforeCreateRoutes =
createAsyncInterruptHook<
(context: RuntimeContext, interrupt: (info: any) => any) => void
>();
createSyncHook<(context: RuntimeContext) => void>();

export { modifyRoutes, onBeforeCreateRoutes };
1 change: 0 additions & 1 deletion packages/toolkit/plugin-v2/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function createAsyncInterruptHook<
let interruptResult: any;

const interrupt = (info: any) => {
console.log('===interrupt');
interrupted = true;
interruptResult = info;
};
Expand Down
1 change: 0 additions & 1 deletion packages/toolkit/plugin-v2/src/runtime/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function initPluginAPI<Extends extends RuntimePluginExtends>({
}

return {
getRuntimeContext,
updateRuntimeContext,
getHooks,
getRuntimeConfig,
Expand Down
10 changes: 5 additions & 5 deletions packages/toolkit/plugin-v2/src/runtime/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import {
createSyncHook,
} from '../hooks';
import type {
Hooks,
ModifyRuntimeConfigFn,
OnBeforeRenderFn,
PickContextFn,
WrapRootFn,
} from '../types/runtime/hooks';

export function initHooks<RuntimeConfig, RuntimeContext>() {
export function initHooks<RuntimeConfig, RuntimeContext>(): Hooks<
RuntimeConfig,
RuntimeContext
> {
return {
onBeforeRender:
createAsyncInterruptHook<OnBeforeRenderFn<RuntimeContext>>(),
Expand All @@ -20,7 +24,3 @@ export function initHooks<RuntimeConfig, RuntimeContext>() {
createCollectSyncHook<ModifyRuntimeConfigFn<RuntimeConfig>>(),
};
}

export type Hooks<RuntimeConfig, RuntimeContext> = ReturnType<
typeof initHooks<RuntimeConfig, RuntimeContext>
>;
10 changes: 9 additions & 1 deletion packages/toolkit/plugin-v2/src/runtime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export { initPluginAPI } from './api';
export { initRuntimeContext, createRuntimeContext } from './context';
export { initHooks, type Hooks } from './hooks';
export { initHooks } from './hooks';
export { runtime } from './run';
export type {
RuntimePluginAPI,
RuntimeContext,
InternalRuntimeContext,
RuntimePlugin,
RuntimePluginExtends,
Hooks,
} from '../types/runtime';
2 changes: 0 additions & 2 deletions packages/toolkit/plugin-v2/src/runtime/run/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export const createRuntime = <Extends extends RuntimePluginExtends>() => {
}

return {
init,
run,
getPrevInitOptions: () => initOptions,
};
};
3 changes: 1 addition & 2 deletions packages/toolkit/plugin-v2/src/types/runtime/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hooks } from '../../runtime';
import type { PluginHookTap } from '../hooks';
import type { DeepPartial } from '../utils';
import type { RuntimeContext } from './context';
import type { Hooks } from './hooks';
import type {
ModifyRuntimeConfigFn,
OnBeforeRenderFn,
Expand All @@ -11,7 +11,6 @@ import type {
import type { RuntimePluginExtends } from './plugin';

export type RuntimePluginAPI<Extends extends RuntimePluginExtends> = Readonly<{
getRuntimeContext: () => Readonly<RuntimeContext & Extends['extendContext']>;
updateRuntimeContext: (updateContext: DeepPartial<RuntimeContext>) => void;
getHooks: () => Readonly<
Hooks<Extends['config'], RuntimeContext & Extends['extendContext']> &
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/plugin-v2/src/types/runtime/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hooks } from '../../runtime/hooks';
import type { RuntimePluginAPI } from './api';
import type { Hooks } from './hooks';
import type { RuntimePluginExtends } from './plugin';

export type RuntimeContext = {};
Expand Down
8 changes: 8 additions & 0 deletions packages/toolkit/plugin-v2/src/types/runtime/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type React from 'react';
import type { AsyncInterruptHook, CollectSyncHook, SyncHook } from '../hooks';

export type OnBeforeRenderFn<RuntimeContext> = (
context: RuntimeContext,
Expand All @@ -16,3 +17,10 @@ export type PickContextFn<RuntimeContext> = (
export type ModifyRuntimeConfigFn<RuntimeConfig> = (
config: RuntimeConfig,
) => RuntimeConfig;

export type Hooks<RuntimeConfig, RuntimeContext> = {
onBeforeRender: AsyncInterruptHook<OnBeforeRenderFn<RuntimeContext>>;
wrapRoot: SyncHook<WrapRootFn>;
pickContext: SyncHook<PickContextFn<RuntimeContext>>;
modifyRuntimeConfig: CollectSyncHook<ModifyRuntimeConfigFn<RuntimeConfig>>;
};
1 change: 1 addition & 0 deletions packages/toolkit/plugin-v2/src/types/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { RuntimePluginAPI } from './api';
export type { RuntimeContext, InternalRuntimeContext } from './context';
export type { RuntimePlugin, RuntimePluginExtends } from './plugin';
export type { Hooks } from './hooks';

0 comments on commit 6786225

Please sign in to comment.