-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
232 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,53 @@ | ||
import type { CLIPluginAPI } from '@modern-js/plugin-v2'; | ||
import type { | ||
CLIPluginAPI, | ||
PluginHookTap, | ||
TransformFunction, | ||
} from '@modern-js/plugin-v2'; | ||
import type { | ||
Entrypoint, | ||
NestedRouteForCli, | ||
PageRoute, | ||
RouteLegacy, | ||
ServerPlugin, | ||
ServerRoute, | ||
} from '@modern-js/types'; | ||
import type { | ||
AppToolsNormalizedConfig, | ||
AppToolsUserConfig, | ||
} from '../../types/config'; | ||
import type { RuntimePlugin } from '../../types/hooks'; | ||
import type { Bundler } from '../../types/utils'; | ||
|
||
export type InternalRuntimePluginsFn = TransformFunction<{ | ||
entrypoint: Entrypoint; | ||
plugins: RuntimePlugin[]; | ||
}>; | ||
export type InternalServerPluginsFn = TransformFunction<{ | ||
plugins: ServerPlugin[]; | ||
}>; | ||
export type CheckEntryPointFn = TransformFunction<{ | ||
path: string; | ||
entry: false | string; | ||
}>; | ||
export type ModifyEntrypointsFn = TransformFunction<Entrypoint[]>; | ||
export type ModifyFileSystemRoutesFn = TransformFunction<{ | ||
entrypoint: Entrypoint; | ||
routes: RouteLegacy[] | (NestedRouteForCli | PageRoute)[]; | ||
}>; | ||
export type ModifyServerRoutesFn = TransformFunction<{ routes: ServerRoute[] }>; | ||
export type DeplpoyFn = () => Promise<void> | void; | ||
|
||
export interface AppTools<B extends Bundler> | ||
extends CLIPluginAPI< | ||
AppToolsUserConfig<B>, | ||
AppToolsNormalizedConfig<AppToolsUserConfig<'shared'>> | ||
> {} | ||
> { | ||
_internalRuntimePlugins: PluginHookTap<InternalRuntimePluginsFn>; | ||
_internalServerPlugins: PluginHookTap<InternalServerPluginsFn>; | ||
checkEntryPoint: PluginHookTap<CheckEntryPointFn>; | ||
modifyEntrypoints: PluginHookTap<ModifyEntrypointsFn>; | ||
modifyFileSystemRoutes: PluginHookTap<ModifyFileSystemRoutesFn>; | ||
modifyServerRoutes: PluginHookTap<ModifyServerRoutesFn>; | ||
|
||
deploy: PluginHookTap<DeplpoyFn>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { AsyncHook, CollectAsyncHook } from './types/hooks'; | ||
|
||
export function createAsyncHook< | ||
Callback extends (...args: any[]) => any, | ||
>(): AsyncHook<Callback> { | ||
const callbacks: Callback[] = []; | ||
|
||
const tap = (cb: Callback) => { | ||
callbacks.push(cb); | ||
}; | ||
|
||
const call = async (...params: Parameters<Callback>) => { | ||
for (const callback of callbacks) { | ||
const result = await callback(...params); | ||
|
||
if (result !== undefined) { | ||
params[0] = result; | ||
} | ||
} | ||
|
||
return params; | ||
}; | ||
|
||
return { | ||
tap, | ||
call, | ||
}; | ||
} | ||
|
||
export function createCollectAsyncHook< | ||
Callback extends (...args: any[]) => any, | ||
>(): CollectAsyncHook<Callback> { | ||
const callbacks: Callback[] = []; | ||
|
||
const tap = (cb: Callback) => { | ||
callbacks.push(cb); | ||
}; | ||
|
||
const call = async (...params: Parameters<Callback>) => { | ||
const results: ReturnType<Callback>[] = []; | ||
for (const callback of callbacks) { | ||
const result = await callback(...params); | ||
|
||
if (result !== undefined) { | ||
params[0] = result; | ||
results.push(result); | ||
} | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
return { | ||
tap, | ||
call, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { createPluginManager } from './manager'; | ||
export { createAsyncHook, createCollectAsyncHook } from './hooks'; | ||
export * from './cli'; | ||
|
||
export * from './types'; |
Oops, something went wrong.