Skip to content

Commit

Permalink
feat(types): definePlugin supports declare theme config type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ulivz committed Dec 25, 2021
1 parent e402b57 commit 5f1e3b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
15 changes: 10 additions & 5 deletions packages/@vuepress/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
PluginOptions,
DefaultThemeConfig,
Theme,
Plugin,
} from './src'
Plugin
} from "./src";

export * from './src'
export * from "./src";

/**
* A helper function to define VuePress config file.
Expand All @@ -30,11 +30,16 @@ export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export function defineTheme<T extends ThemeConfig = ThemeConfig>(config: Theme<T>): void;
export function defineTheme<T extends ThemeConfig = ThemeConfig>(
config: Theme<T>
): void;

/**
* A helper function to define VuePress theme entry file.
*
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
*/
export function definePlugin<T extends PluginOptions = PluginOptions>(config: Plugin<T>): void;
export function definePlugin<
T extends PluginOptions = PluginOptions,
U extends ThemeConfig = ThemeConfig
>(config: Plugin<T, U>): void;
14 changes: 10 additions & 4 deletions packages/@vuepress/types/src/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { UserPlugins, PluginOptions } from "./plugin";
import { ChainWebpack, Hook, AsyncHook, PluginObject } from "./shared";
import { Page, Context } from "./context";
import { ExtendMarkdown } from "./markdown";
import { ThemeConfig } from "./theme";
import { Config } from "./config";

export type PlainObjectWithStringValue = Record<string, string>;

Expand Down Expand Up @@ -82,6 +84,7 @@ export type PluginEntryOptions = {
enhanceAppFiles?:
| string
| string[]
| Hook<[], FileDescriptor | FileDescriptor[]>
| AsyncHook<[], FileDescriptor | FileDescriptor[]>;
/**
* Generate some client modules at compile time.
Expand All @@ -94,7 +97,9 @@ export type PluginEntryOptions = {
*
* @see https://vuepress.vuejs.org/plugin/option-api.html#extendpagedata
*/
extendPageData?: <T extends PluginObject = PluginObject>(page: Page & T) => void;
extendPageData?: <T extends PluginObject = PluginObject>(
page: Page & T
) => void;
/**
* A path to the mixin file which allows you to control the lifecycle of root component.
*
Expand Down Expand Up @@ -161,6 +166,7 @@ export type PluginEntry = PluginEntryOptions & {
*
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
*/
export type Plugin<T extends PluginOptions = PluginOptions> =
| PluginEntry
| ((options: T, ctx: Context) => PluginEntry);
export type Plugin<
T extends PluginOptions = PluginOptions,
U extends ThemeConfig = ThemeConfig
> = PluginEntry | ((options: T, ctx: Context<U, Config<U>>) => PluginEntry);

0 comments on commit 5f1e3b0

Please sign in to comment.