Skip to content

Commit 84250cc

Browse files
committedDec 23, 2021
feat(types): defineThemeEntry helper to infer type for theme entry
1 parent 8c4b5bc commit 84250cc

File tree

10 files changed

+114
-15
lines changed

10 files changed

+114
-15
lines changed
 

‎packages/@vuepress/theme-default/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require('path')
22

3-
// Theme API.
3+
/**
4+
* @type {import('@vuepress/types'.UserThemeEntry)}
5+
*/
46
module.exports = (options, ctx) => {
57
const { themeConfig, siteConfig } = ctx
68

‎packages/@vuepress/theme-default/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@vuepress/plugin-active-header-links": "1.9.1",
2525
"@vuepress/plugin-nprogress": "1.9.1",
2626
"@vuepress/plugin-search": "1.9.1",
27+
"@vuepress/types": "1.9.1",
2728
"docsearch.js": "^2.5.2",
2829
"lodash": "^4.17.15",
2930
"stylus": "^0.54.8",

‎packages/@vuepress/types/lib/config.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { PostCssLoaderOptions } from "./style";
33
import { MarkdownConfig } from "./markdown";
44
import { LocaleConfig } from "./locale";
55
import { ThemeConfig } from "./theme";
6-
import { PluginTuple, PluginObject } from "./plugin";
6+
import { Plugins } from "./plugin";
7+
import { Context } from "./context";
78

89
/**
910
* HTML tag name
@@ -123,7 +124,7 @@ export interface Config<T extends ThemeConfig> {
123124
*
124125
* @see https://vuepress.vuejs.org/config/#plugins
125126
*/
126-
plugins?: PluginObject | Array<PluginTuple>;
127+
plugins?: Plugins;
127128
/**
128129
* Markdown options.
129130
*
@@ -187,3 +188,10 @@ export interface Config<T extends ThemeConfig> {
187188
*/
188189
evergreen?: boolean;
189190
}
191+
192+
/**
193+
* Expose `VuePress` config with function support
194+
*/
195+
export type UserConfig<T extends ThemeConfig> =
196+
| Config<T>
197+
| ((ctx: Context<T, Config<T>>) => Config<T>);

‎packages/@vuepress/types/lib/context.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { ThemeConfig } from "./theme";
2+
import { Config } from "./config";
3+
14
/**
25
* Page instance.
36
*
@@ -24,7 +27,10 @@ export interface Page {
2427
*
2528
* @see https://vuepress.vuejs.org/plugin/context-api.html
2629
*/
27-
export interface Context {
30+
export interface Context<
31+
T extends ThemeConfig = ThemeConfig,
32+
C extends Config<T> = Config<ThemeConfig>
33+
> {
2834
/**
2935
* Whether VuePress run in production environment mode.
3036
*/
@@ -53,4 +59,12 @@ export interface Context {
5359
* A utility for writing temporary files to tempPath.
5460
*/
5561
writeTemp(filename: string, content: string): Promise<void>;
62+
/**
63+
* Current theme config.
64+
*/
65+
themeConfig: T;
66+
/**
67+
* VuePress Config.
68+
*/
69+
siteConfig: C;
5670
}

‎packages/@vuepress/types/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './markdown'
44
export * from './style'
55
export * from './context'
66
export * from './theme-default'
7+
export * from './theme'

‎packages/@vuepress/types/lib/plugin-api.ts

Whitespace-only changes.

‎packages/@vuepress/types/lib/plugin.ts

+6
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ export type PluginObject = Partial<PluginConfigMap>
6363
[k: string]: Record<string, any>;
6464
}
6565

66+
/**
67+
* Specify plugins.
68+
*
69+
* @see https://vuepress.vuejs.org/config/#plugins
70+
*/
71+
export type Plugins= PluginObject | Array<PluginTuple>;

‎packages/@vuepress/types/lib/theme.ts

+51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
1+
import { Plugins } from "./plugin";
2+
import { Context } from "./context";
3+
import { Config } from "./config";
4+
15
/**
26
* Default theme config type
37
*/
48
export type ThemeConfig = any;
9+
10+
/**
11+
* Export type of theme entry
12+
*
13+
* @see https://vuepress.vuejs.org/theme/option-api.html
14+
*/
15+
export type ThemeEntry = {
16+
/**
17+
* plugins
18+
*/
19+
plugins?: Plugins;
20+
/**
21+
* HTML template path used in dev mode.
22+
*
23+
* @default https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/core/lib/client/index.dev.html
24+
* @see https://vuepress.vuejs.org/theme/option-api.html#devtemplate
25+
*/
26+
devTemplate?: string;
27+
/**
28+
* HTML template path used in build mode
29+
*
30+
* @default https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/core/lib/client/index.ssr.html
31+
* @see https://vuepress.vuejs.org/theme/option-api.html#ssrtemplate
32+
*/
33+
ssrTemplate?: string;
34+
/**
35+
* Extends a theme
36+
*
37+
* @see https://vuepress.vuejs.org/theme/option-api.html#extend
38+
*/
39+
extend?: string;
40+
/**
41+
* Global layout component is a component responsible for the global layout strategy.
42+
*
43+
* @see https://vuepress.vuejs.org/theme/option-api.html#globallayout
44+
*/
45+
globalLayout?: string;
46+
};
47+
48+
/**
49+
* Export type of theme entry with function support
50+
*
51+
* @see https://vuepress.vuejs.org/theme/option-api.html
52+
*/
53+
export type UserThemeEntry<T extends ThemeConfig = ThemeConfig> =
54+
| ThemeEntry
55+
| ((themeConfig: T, ctx: Context<T, Config<T>>) => ThemeEntry);

‎packages/vuepress/config.d.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
import { Context, Config, ThemeConfig, DefaultThemeConfig } from '@vuepress/types'
1+
import {
2+
UserConfig,
3+
ThemeConfig,
4+
DefaultThemeConfig,
5+
ThemeEntry
6+
} from '@vuepress/types'
27

38
export * from '@vuepress/types'
49

5-
export type UserConfig<T extends ThemeConfig> =
6-
| Config<T>
7-
| ((ctx: Context) => Config<T>);
8-
910
/**
10-
* Helper for type prompt and type checking.
11+
* A helper function to define VuePress config file.
12+
*
13+
* @see https://vuepress.vuejs.org/config/
1114
*/
1215
export function defineConfig(config: UserConfig<DefaultThemeConfig>): void;
16+
17+
/**
18+
* A helper function to define VuePress config file, for custom theme.
19+
*
20+
* @see https://vuepress.vuejs.org/config/
21+
*/
1322
export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
1423
config: UserConfig<T>
1524
): void;
25+
26+
/**
27+
* A helper function to define VuePress theme entry file.
28+
*
29+
* @see https://vuepress.vuejs.org/theme/option-api.html
30+
*/
31+
export function defineThemeEntry(config: ThemeEntry): void;

‎packages/vuepress/config.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
exports.defineConfig = function (config) {
2-
return config
3-
}
4-
exports.defineConfig4CustomTheme = function (config) {
5-
return config
1+
function define (config) {
2+
config
63
}
4+
exports.defineConfig = define
5+
exports.defineConfig4CustomTheme = define
6+
exports.defineThemeEntry = define

0 commit comments

Comments
 (0)
Please sign in to comment.