Skip to content

Commit b814133

Browse files
committed
refactor: typed all official plugins
1 parent cb07377 commit b814133

File tree

24 files changed

+79
-23
lines changed

24 files changed

+79
-23
lines changed

Diff for: packages/@vuepress/plugin-active-header-links/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const { path } = require('@vuepress/shared-utils')
22

3-
module.exports = (options) => ({
3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
6+
module.exports = options => ({
47
clientRootMixin: path.resolve(__dirname, 'clientRootMixin.js'),
58
define: {
69
AHL_SIDEBAR_LINK_SELECTOR: options.sidebarLinkSelector || '.sidebar-link',

Diff for: packages/@vuepress/plugin-active-header-links/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24+
"@vuepress/types": "1.9.1",
2425
"lodash.debounce": "^4.0.8"
2526
},
2627
"publishConfig": {

Diff for: packages/@vuepress/plugin-back-to-top/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { path } = require('@vuepress/shared-utils')
22

3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
36
module.exports = {
47
enhanceAppFiles: [
58
path.resolve(__dirname, 'enhanceAppFile.js')

Diff for: packages/@vuepress/plugin-back-to-top/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24+
"@vuepress/types": "1.9.1",
2425
"lodash.debounce": "^4.0.8"
2526
},
2627
"publishConfig": {

Diff for: packages/@vuepress/plugin-google-analytics/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { path } = require('@vuepress/shared-utils')
22

3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
36
module.exports = (options = {}, context) => ({
47
define () {
58
const { siteConfig = {}} = context

Diff for: packages/@vuepress/plugin-google-analytics/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"license": "MIT",
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
23+
"dependencies": {
24+
"@vuepress/types": "1.9.1"
25+
},
2326
"publishConfig": {
2427
"access": "public"
2528
}

Diff for: packages/@vuepress/plugin-last-updated/index.js

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

4+
/**
5+
* @type {import('@vuepress/types').Plugin}
6+
*/
47
module.exports = (options = {}, context) => ({
58
extendPageData ($page) {
69
const { transformer, dateOptions } = options

Diff for: packages/@vuepress/plugin-last-updated/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24+
"@vuepress/types": "1.9.1",
2425
"cross-spawn": "^6.0.5"
2526
},
2627
"publishConfig": {

Diff for: packages/@vuepress/plugin-medium-zoom/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { path } = require('@vuepress/shared-utils')
22

3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
36
module.exports = (options, context) => ({
47
define: {
58
SELECTOR: options.selector || '.theme-default-content :not(a) > img',

Diff for: packages/@vuepress/plugin-medium-zoom/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24+
"@vuepress/types": "1.9.1",
2425
"medium-zoom": "^1.0.4"
2526
},
2627
"publishConfig": {

Diff for: packages/@vuepress/plugin-nprogress/index.js

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

3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
36
module.exports = {
47
clientRootMixin: resolve(__dirname, 'clientRootMixin.js'),
58
enhanceAppFiles: resolve(__dirname, 'enhanceAppFile.js')

Diff for: packages/@vuepress/plugin-nprogress/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24+
"@vuepress/types": "1.9.1",
2425
"nprogress": "^0.2.0"
2526
},
2627
"publishConfig": {

Diff for: packages/@vuepress/plugin-pwa/index.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const { logger, fs, path } = require('@vuepress/shared-utils')
22

3-
module.exports = ({
4-
serviceWorker = true,
5-
updatePopup = false,
6-
popupComponent = 'SWUpdatePopup',
7-
generateSWConfig = {}
8-
}, context) => ({
3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
6+
module.exports = (
7+
{
8+
serviceWorker = true,
9+
updatePopup = false,
10+
popupComponent = 'SWUpdatePopup',
11+
generateSWConfig = {}
12+
},
13+
context
14+
) => ({
915
alias: {
1016
'@sw-event': path.resolve(__dirname, 'lib/event.js')
1117
},
@@ -33,12 +39,17 @@ module.exports = ({
3339
await wbb.generateSW({
3440
swDest: swFilePath,
3541
globDirectory: outDir,
36-
globPatterns: ['**\/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}'],
42+
globPatterns: [
43+
'**/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}'
44+
],
3745
...generateSWConfig
3846
})
3947
await fs.writeFile(
4048
swFilePath,
41-
await fs.readFile(path.resolve(__dirname, 'lib/skip-waiting.js'), 'utf8'),
49+
await fs.readFile(
50+
path.resolve(__dirname, 'lib/skip-waiting.js'),
51+
'utf8'
52+
),
4253
{ flag: 'a' }
4354
)
4455
}

Diff for: packages/@vuepress/plugin-pwa/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"main": "index.js",
2323
"dependencies": {
2424
"@vuepress/shared-utils": "1.9.1",
25+
"@vuepress/types": "1.9.1",
2526
"register-service-worker": "^1.7.0",
2627
"workbox-build": "^4.3.1"
2728
},

Diff for: packages/@vuepress/plugin-register-components/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ async function resolveComponents (componentDir) {
1616
// overwritten.
1717
let moduleId = 0
1818

19+
/**
20+
* @type {import('@vuepress/types').Plugin}
21+
*/
1922
module.exports = (options, context) => ({
2023
multiple: true,
2124

Diff for: packages/@vuepress/plugin-register-components/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
2323
"dependencies": {
24-
"@vuepress/shared-utils": "1.9.1"
24+
"@vuepress/shared-utils": "1.9.1",
25+
"@vuepress/types": "1.9.1"
2526
},
2627
"publishConfig": {
2728
"access": "public"

Diff for: packages/@vuepress/plugin-search/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { path } = require('@vuepress/shared-utils')
22

3+
/**
4+
* @type {import('@vuepress/types').Plugin}
5+
*/
36
module.exports = (options) => ({
47
alias: {
58
'@SearchBox':

Diff for: packages/@vuepress/plugin-search/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"license": "MIT",
2121
"author": "ULIVZ <[email protected]>",
2222
"main": "index.js",
23+
"dependencies": {
24+
"@vuepress/types": "1.9.1"
25+
},
2326
"publishConfig": {
2427
"access": "public"
2528
}

Diff for: packages/@vuepress/theme-default/index.js

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

33
/**
4-
* @type {import('@vuepress/types'.UserThemeEntry)}
4+
* @type {import('@vuepress/types'.Theme)}
55
*/
66
module.exports = (options, ctx) => {
77
const { themeConfig, siteConfig } = ctx

Diff for: packages/@vuepress/types/index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
ThemeConfig,
44
PluginOptions,
55
DefaultThemeConfig,
6-
UserThemeEntry,
7-
UserPluginEntry,
6+
Theme,
7+
Plugin,
88
} from './lib'
99

1010
export * from './lib'
@@ -30,11 +30,11 @@ export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
3030
*
3131
* @see https://vuepress.vuejs.org/theme/option-api.html
3232
*/
33-
export function defineThemeEntry<T extends ThemeConfig = ThemeConfig>(config: UserThemeEntry<T>): void;
33+
export function defineTheme<T extends ThemeConfig = ThemeConfig>(config: Theme<T>): void;
3434

3535
/**
3636
* A helper function to define VuePress theme entry file.
3737
*
3838
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
3939
*/
40-
export function definePluginEntry<T extends PluginOptions = PluginOptions>(config: UserPluginEntry<T>): void;
40+
export function definePlugin<T extends PluginOptions = PluginOptions>(config: Plugin<T>): void;

Diff for: packages/@vuepress/types/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ function define(config) {
33
}
44
exports.defineConfig = define;
55
exports.defineConfig4CustomTheme = define;
6-
exports.defineThemeEntry = define;
7-
exports.definePluginEntry = define;
6+
exports.defineTheme = define;
7+
exports.definePlugin = define;

Diff for: packages/@vuepress/types/lib/plugin-api.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Application } from "express";
22
import WebpackDevServer from "webpack-dev-server";
33
import { UserPlugins, PluginOptions } from "./plugin";
4-
import { ChainWebpack, Hook, AsyncHook } from "./shared";
4+
import { ChainWebpack, Hook, AsyncHook, PluginObject } from "./shared";
55
import { Page, Context } from "./context";
66
import { ExtendMarkdown } from "./markdown";
77

@@ -14,6 +14,8 @@ export type LifeCycleHook$Ready = AsyncHook<[], unknown>;
1414
export type LifeCycleHook$Updated = Hook<[], unknown>;
1515
export type LifeCycleHook$Generated = Hook<[], unknown>;
1616

17+
export type FileDescriptor = { name: string; content: string };
18+
1719
/**
1820
* Plugin Options API
1921
*/
@@ -22,6 +24,10 @@ export type PluginEntryOptions = {
2224
* Current name
2325
*/
2426
name: string;
27+
/**
28+
* Specify whether current plugin can be applied multiple times.
29+
*/
30+
multiple?: boolean;
2531
/**
2632
* Sub plugins
2733
*/
@@ -76,19 +82,19 @@ export type PluginEntryOptions = {
7682
enhanceAppFiles?:
7783
| string
7884
| string[]
79-
| AsyncHook<[], { name: string; content: string }>;
85+
| AsyncHook<[], FileDescriptor | FileDescriptor[]>;
8086
/**
8187
* Generate some client modules at compile time.
8288
*
8389
* @see https://vuepress.vuejs.org/plugin/option-api.html#clientdynamicmodules
8490
*/
85-
clientDynamicModules?: AsyncHook<[], { name: string; content: string }>;
91+
clientDynamicModules?: AsyncHook<[], FileDescriptor | FileDescriptor[]>;
8692
/**
8793
* A function used to extend or edit the $page object
8894
*
8995
* @see https://vuepress.vuejs.org/plugin/option-api.html#extendpagedata
9096
*/
91-
extendPageData: Hook<[Page], unknown>;
97+
extendPageData: <T extends PluginObject = PluginObject>(page: Page & T) => void;
9298
/**
9399
* A path to the mixin file which allows you to control the lifecycle of root component.
94100
*
@@ -155,6 +161,6 @@ export type PluginEntry = PluginEntryOptions & {
155161
*
156162
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
157163
*/
158-
export type UserPluginEntry<T extends PluginOptions = PluginOptions> =
164+
export type Plugin<T extends PluginOptions = PluginOptions> =
159165
| PluginEntry
160166
| ((options: T, ctx: Context) => PluginEntry);

Diff for: packages/@vuepress/types/lib/shared.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export type AsyncHook<U extends unknown[], R extends unknown> = Hook<
88
U,
99
Promise<R>
1010
>;
11+
12+
export type PluginObject = Record<string, any>;

Diff for: packages/@vuepress/types/lib/theme.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ export type ThemeEntry = PluginEntryOptions & {
5151
*
5252
* @see https://vuepress.vuejs.org/theme/option-api.html
5353
*/
54-
export type UserThemeEntry<T extends ThemeConfig = ThemeConfig> =
54+
export type Theme<T extends ThemeConfig = ThemeConfig> =
5555
| ThemeEntry
5656
| ((themeConfig: T, ctx: Context<T, Config<T>>) => ThemeEntry);

0 commit comments

Comments
 (0)