Skip to content

Commit 5f1e3b0

Browse files
committed
feat(types): definePlugin supports declare theme config type
Motivation: If you're developing a theme's internal plugins, you'll want the `definePlugin` helper support declare theme config type by second generic type.
1 parent e402b57 commit 5f1e3b0

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/@vuepress/types/index.d.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
PluginOptions,
55
DefaultThemeConfig,
66
Theme,
7-
Plugin,
8-
} from './src'
7+
Plugin
8+
} from "./src";
99

10-
export * from './src'
10+
export * from "./src";
1111

1212
/**
1313
* A helper function to define VuePress config file.
@@ -30,11 +30,16 @@ export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
3030
*
3131
* @see https://vuepress.vuejs.org/theme/option-api.html
3232
*/
33-
export function defineTheme<T extends ThemeConfig = ThemeConfig>(config: Theme<T>): void;
33+
export function defineTheme<T extends ThemeConfig = ThemeConfig>(
34+
config: Theme<T>
35+
): void;
3436

3537
/**
3638
* A helper function to define VuePress theme entry file.
3739
*
3840
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
3941
*/
40-
export function definePlugin<T extends PluginOptions = PluginOptions>(config: Plugin<T>): void;
42+
export function definePlugin<
43+
T extends PluginOptions = PluginOptions,
44+
U extends ThemeConfig = ThemeConfig
45+
>(config: Plugin<T, U>): void;

packages/@vuepress/types/src/plugin-api.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { UserPlugins, PluginOptions } from "./plugin";
44
import { ChainWebpack, Hook, AsyncHook, PluginObject } from "./shared";
55
import { Page, Context } from "./context";
66
import { ExtendMarkdown } from "./markdown";
7+
import { ThemeConfig } from "./theme";
8+
import { Config } from "./config";
79

810
export type PlainObjectWithStringValue = Record<string, string>;
911

@@ -82,6 +84,7 @@ export type PluginEntryOptions = {
8284
enhanceAppFiles?:
8385
| string
8486
| string[]
87+
| Hook<[], FileDescriptor | FileDescriptor[]>
8588
| AsyncHook<[], FileDescriptor | FileDescriptor[]>;
8689
/**
8790
* Generate some client modules at compile time.
@@ -94,7 +97,9 @@ export type PluginEntryOptions = {
9497
*
9598
* @see https://vuepress.vuejs.org/plugin/option-api.html#extendpagedata
9699
*/
97-
extendPageData?: <T extends PluginObject = PluginObject>(page: Page & T) => void;
100+
extendPageData?: <T extends PluginObject = PluginObject>(
101+
page: Page & T
102+
) => void;
98103
/**
99104
* A path to the mixin file which allows you to control the lifecycle of root component.
100105
*
@@ -161,6 +166,7 @@ export type PluginEntry = PluginEntryOptions & {
161166
*
162167
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
163168
*/
164-
export type Plugin<T extends PluginOptions = PluginOptions> =
165-
| PluginEntry
166-
| ((options: T, ctx: Context) => PluginEntry);
169+
export type Plugin<
170+
T extends PluginOptions = PluginOptions,
171+
U extends ThemeConfig = ThemeConfig
172+
> = PluginEntry | ((options: T, ctx: Context<U, Config<U>>) => PluginEntry);

0 commit comments

Comments
 (0)