Skip to content

Commit

Permalink
feat(types): defineThemeEntry helper to infer type for theme entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Dec 23, 2021
1 parent 8c4b5bc commit 84250cc
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/@vuepress/theme-default/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require('path')

// Theme API.
/**
* @type {import('@vuepress/types'.UserThemeEntry)}
*/
module.exports = (options, ctx) => {
const { themeConfig, siteConfig } = ctx

Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/theme-default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@vuepress/plugin-active-header-links": "1.9.1",
"@vuepress/plugin-nprogress": "1.9.1",
"@vuepress/plugin-search": "1.9.1",
"@vuepress/types": "1.9.1",
"docsearch.js": "^2.5.2",
"lodash": "^4.17.15",
"stylus": "^0.54.8",
Expand Down
12 changes: 10 additions & 2 deletions packages/@vuepress/types/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { PostCssLoaderOptions } from "./style";
import { MarkdownConfig } from "./markdown";
import { LocaleConfig } from "./locale";
import { ThemeConfig } from "./theme";
import { PluginTuple, PluginObject } from "./plugin";
import { Plugins } from "./plugin";
import { Context } from "./context";

/**
* HTML tag name
Expand Down Expand Up @@ -123,7 +124,7 @@ export interface Config<T extends ThemeConfig> {
*
* @see https://vuepress.vuejs.org/config/#plugins
*/
plugins?: PluginObject | Array<PluginTuple>;
plugins?: Plugins;
/**
* Markdown options.
*
Expand Down Expand Up @@ -187,3 +188,10 @@ export interface Config<T extends ThemeConfig> {
*/
evergreen?: boolean;
}

/**
* Expose `VuePress` config with function support
*/
export type UserConfig<T extends ThemeConfig> =
| Config<T>
| ((ctx: Context<T, Config<T>>) => Config<T>);
16 changes: 15 additions & 1 deletion packages/@vuepress/types/lib/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ThemeConfig } from "./theme";
import { Config } from "./config";

/**
* Page instance.
*
Expand All @@ -24,7 +27,10 @@ export interface Page {
*
* @see https://vuepress.vuejs.org/plugin/context-api.html
*/
export interface Context {
export interface Context<
T extends ThemeConfig = ThemeConfig,
C extends Config<T> = Config<ThemeConfig>
> {
/**
* Whether VuePress run in production environment mode.
*/
Expand Down Expand Up @@ -53,4 +59,12 @@ export interface Context {
* A utility for writing temporary files to tempPath.
*/
writeTemp(filename: string, content: string): Promise<void>;
/**
* Current theme config.
*/
themeConfig: T;
/**
* VuePress Config.
*/
siteConfig: C;
}
1 change: 1 addition & 0 deletions packages/@vuepress/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './markdown'
export * from './style'
export * from './context'
export * from './theme-default'
export * from './theme'
Empty file.
6 changes: 6 additions & 0 deletions packages/@vuepress/types/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ export type PluginObject = Partial<PluginConfigMap>
[k: string]: Record<string, any>;
}

/**
* Specify plugins.
*
* @see https://vuepress.vuejs.org/config/#plugins
*/
export type Plugins= PluginObject | Array<PluginTuple>;
51 changes: 51 additions & 0 deletions packages/@vuepress/types/lib/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
import { Plugins } from "./plugin";
import { Context } from "./context";
import { Config } from "./config";

/**
* Default theme config type
*/
export type ThemeConfig = any;

/**
* Export type of theme entry
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export type ThemeEntry = {
/**
* plugins
*/
plugins?: Plugins;
/**
* HTML template path used in dev mode.
*
* @default https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/core/lib/client/index.dev.html
* @see https://vuepress.vuejs.org/theme/option-api.html#devtemplate
*/
devTemplate?: string;
/**
* HTML template path used in build mode
*
* @default https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/core/lib/client/index.ssr.html
* @see https://vuepress.vuejs.org/theme/option-api.html#ssrtemplate
*/
ssrTemplate?: string;
/**
* Extends a theme
*
* @see https://vuepress.vuejs.org/theme/option-api.html#extend
*/
extend?: string;
/**
* Global layout component is a component responsible for the global layout strategy.
*
* @see https://vuepress.vuejs.org/theme/option-api.html#globallayout
*/
globalLayout?: string;
};

/**
* Export type of theme entry with function support
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export type UserThemeEntry<T extends ThemeConfig = ThemeConfig> =
| ThemeEntry
| ((themeConfig: T, ctx: Context<T, Config<T>>) => ThemeEntry);
28 changes: 22 additions & 6 deletions packages/vuepress/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import { Context, Config, ThemeConfig, DefaultThemeConfig } from '@vuepress/types'
import {
UserConfig,
ThemeConfig,
DefaultThemeConfig,
ThemeEntry
} from '@vuepress/types'

export * from '@vuepress/types'

export type UserConfig<T extends ThemeConfig> =
| Config<T>
| ((ctx: Context) => Config<T>);

/**
* Helper for type prompt and type checking.
* A helper function to define VuePress config file.
*
* @see https://vuepress.vuejs.org/config/
*/
export function defineConfig(config: UserConfig<DefaultThemeConfig>): void;

/**
* A helper function to define VuePress config file, for custom theme.
*
* @see https://vuepress.vuejs.org/config/
*/
export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
config: UserConfig<T>
): void;

/**
* A helper function to define VuePress theme entry file.
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export function defineThemeEntry(config: ThemeEntry): void;
10 changes: 5 additions & 5 deletions packages/vuepress/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports.defineConfig = function (config) {
return config
}
exports.defineConfig4CustomTheme = function (config) {
return config
function define (config) {
config
}
exports.defineConfig = define
exports.defineConfig4CustomTheme = define
exports.defineThemeEntry = define

0 comments on commit 84250cc

Please sign in to comment.