Skip to content

Commit

Permalink
refactor: typed all official plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Dec 23, 2021
1 parent cb07377 commit b814133
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 23 deletions.
5 changes: 4 additions & 1 deletion packages/@vuepress/plugin-active-header-links/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { path } = require('@vuepress/shared-utils')

module.exports = (options) => ({
/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = options => ({
clientRootMixin: path.resolve(__dirname, 'clientRootMixin.js'),
define: {
AHL_SIDEBAR_LINK_SELECTOR: options.sidebarLinkSelector || '.sidebar-link',
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-active-header-links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1",
"lodash.debounce": "^4.0.8"
},
"publishConfig": {
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-back-to-top/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { path } = require('@vuepress/shared-utils')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = {
enhanceAppFiles: [
path.resolve(__dirname, 'enhanceAppFile.js')
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-back-to-top/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1",
"lodash.debounce": "^4.0.8"
},
"publishConfig": {
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-google-analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { path } = require('@vuepress/shared-utils')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (options = {}, context) => ({
define () {
const { siteConfig = {}} = context
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-google-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"license": "MIT",
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1"
},
"publishConfig": {
"access": "public"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-last-updated/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const path = require('path')
const spawn = require('cross-spawn')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (options = {}, context) => ({
extendPageData ($page) {
const { transformer, dateOptions } = options
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-last-updated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1",
"cross-spawn": "^6.0.5"
},
"publishConfig": {
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-medium-zoom/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { path } = require('@vuepress/shared-utils')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (options, context) => ({
define: {
SELECTOR: options.selector || '.theme-default-content :not(a) > img',
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-medium-zoom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1",
"medium-zoom": "^1.0.4"
},
"publishConfig": {
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-nprogress/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { resolve } = require('path')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = {
clientRootMixin: resolve(__dirname, 'clientRootMixin.js'),
enhanceAppFiles: resolve(__dirname, 'enhanceAppFile.js')
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-nprogress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1",
"nprogress": "^0.2.0"
},
"publishConfig": {
Expand Down
27 changes: 19 additions & 8 deletions packages/@vuepress/plugin-pwa/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const { logger, fs, path } = require('@vuepress/shared-utils')

module.exports = ({
serviceWorker = true,
updatePopup = false,
popupComponent = 'SWUpdatePopup',
generateSWConfig = {}
}, context) => ({
/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (
{
serviceWorker = true,
updatePopup = false,
popupComponent = 'SWUpdatePopup',
generateSWConfig = {}
},
context
) => ({
alias: {
'@sw-event': path.resolve(__dirname, 'lib/event.js')
},
Expand Down Expand Up @@ -33,12 +39,17 @@ module.exports = ({
await wbb.generateSW({
swDest: swFilePath,
globDirectory: outDir,
globPatterns: ['**\/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}'],
globPatterns: [
'**/*.{js,css,html,png,jpg,jpeg,gif,svg,woff,woff2,eot,ttf,otf}'
],
...generateSWConfig
})
await fs.writeFile(
swFilePath,
await fs.readFile(path.resolve(__dirname, 'lib/skip-waiting.js'), 'utf8'),
await fs.readFile(
path.resolve(__dirname, 'lib/skip-waiting.js'),
'utf8'
),
{ flag: 'a' }
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"main": "index.js",
"dependencies": {
"@vuepress/shared-utils": "1.9.1",
"@vuepress/types": "1.9.1",
"register-service-worker": "^1.7.0",
"workbox-build": "^4.3.1"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-register-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ async function resolveComponents (componentDir) {
// overwritten.
let moduleId = 0

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (options, context) => ({
multiple: true,

Expand Down
3 changes: 2 additions & 1 deletion packages/@vuepress/plugin-register-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/shared-utils": "1.9.1"
"@vuepress/shared-utils": "1.9.1",
"@vuepress/types": "1.9.1"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-search/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { path } = require('@vuepress/shared-utils')

/**
* @type {import('@vuepress/types').Plugin}
*/
module.exports = (options) => ({
alias: {
'@SearchBox':
Expand Down
3 changes: 3 additions & 0 deletions packages/@vuepress/plugin-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"license": "MIT",
"author": "ULIVZ <[email protected]>",
"main": "index.js",
"dependencies": {
"@vuepress/types": "1.9.1"
},
"publishConfig": {
"access": "public"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/theme-default/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')

/**
* @type {import('@vuepress/types'.UserThemeEntry)}
* @type {import('@vuepress/types'.Theme)}
*/
module.exports = (options, ctx) => {
const { themeConfig, siteConfig } = ctx
Expand Down
8 changes: 4 additions & 4 deletions packages/@vuepress/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
ThemeConfig,
PluginOptions,
DefaultThemeConfig,
UserThemeEntry,
UserPluginEntry,
Theme,
Plugin,
} from './lib'

export * from './lib'
Expand All @@ -30,11 +30,11 @@ export function defineConfig4CustomTheme<T extends ThemeConfig = ThemeConfig>(
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export function defineThemeEntry<T extends ThemeConfig = ThemeConfig>(config: UserThemeEntry<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 definePluginEntry<T extends PluginOptions = PluginOptions>(config: UserPluginEntry<T>): void;
export function definePlugin<T extends PluginOptions = PluginOptions>(config: Plugin<T>): void;
4 changes: 2 additions & 2 deletions packages/@vuepress/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ function define(config) {
}
exports.defineConfig = define;
exports.defineConfig4CustomTheme = define;
exports.defineThemeEntry = define;
exports.definePluginEntry = define;
exports.defineTheme = define;
exports.definePlugin = define;
16 changes: 11 additions & 5 deletions packages/@vuepress/types/lib/plugin-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Application } from "express";
import WebpackDevServer from "webpack-dev-server";
import { UserPlugins, PluginOptions } from "./plugin";
import { ChainWebpack, Hook, AsyncHook } from "./shared";
import { ChainWebpack, Hook, AsyncHook, PluginObject } from "./shared";
import { Page, Context } from "./context";
import { ExtendMarkdown } from "./markdown";

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

export type FileDescriptor = { name: string; content: string };

/**
* Plugin Options API
*/
Expand All @@ -22,6 +24,10 @@ export type PluginEntryOptions = {
* Current name
*/
name: string;
/**
* Specify whether current plugin can be applied multiple times.
*/
multiple?: boolean;
/**
* Sub plugins
*/
Expand Down Expand Up @@ -76,19 +82,19 @@ export type PluginEntryOptions = {
enhanceAppFiles?:
| string
| string[]
| AsyncHook<[], { name: string; content: string }>;
| AsyncHook<[], FileDescriptor | FileDescriptor[]>;
/**
* Generate some client modules at compile time.
*
* @see https://vuepress.vuejs.org/plugin/option-api.html#clientdynamicmodules
*/
clientDynamicModules?: AsyncHook<[], { name: string; content: string }>;
clientDynamicModules?: AsyncHook<[], FileDescriptor | FileDescriptor[]>;
/**
* A function used to extend or edit the $page object
*
* @see https://vuepress.vuejs.org/plugin/option-api.html#extendpagedata
*/
extendPageData: Hook<[Page], unknown>;
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 @@ -155,6 +161,6 @@ export type PluginEntry = PluginEntryOptions & {
*
* @see https://vuepress.vuejs.org/plugin/writing-a-plugin.html
*/
export type UserPluginEntry<T extends PluginOptions = PluginOptions> =
export type Plugin<T extends PluginOptions = PluginOptions> =
| PluginEntry
| ((options: T, ctx: Context) => PluginEntry);
2 changes: 2 additions & 0 deletions packages/@vuepress/types/lib/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export type AsyncHook<U extends unknown[], R extends unknown> = Hook<
U,
Promise<R>
>;

export type PluginObject = Record<string, any>;
2 changes: 1 addition & 1 deletion packages/@vuepress/types/lib/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ export type ThemeEntry = PluginEntryOptions & {
*
* @see https://vuepress.vuejs.org/theme/option-api.html
*/
export type UserThemeEntry<T extends ThemeConfig = ThemeConfig> =
export type Theme<T extends ThemeConfig = ThemeConfig> =
| ThemeEntry
| ((themeConfig: T, ctx: Context<T, Config<T>>) => ThemeEntry);

0 comments on commit b814133

Please sign in to comment.