From 317987e460d170625919038f1d3e3cdde06eeab7 Mon Sep 17 00:00:00 2001 From: Alain Date: Fri, 4 Jun 2021 02:22:52 +0800 Subject: [PATCH] feat(docz-core): add modifyEntry plugin supported (#1621) * feat(docz-core): add modifyEntry plugin supported - add modifyEntry plugin supported - fix OnCreateDevServer type name typo * fix(docz-core): fix type name typo --- core/docz-core/package.json | 2 +- core/docz-core/src/lib/Entries.ts | 9 +++++-- core/docz-core/src/lib/Plugin.ts | 23 +++++++++------- .../src/docs/references/creating-plugins.mdx | 26 +++++++++++++++++-- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/core/docz-core/package.json b/core/docz-core/package.json index 3fdb879a5..24783bd2d 100644 --- a/core/docz-core/package.json +++ b/core/docz-core/package.json @@ -1,6 +1,6 @@ { "name": "docz-core", - "version": "2.3.2-alpha.0", + "version": "2.3.2-alpha.1", "description": "All docz core logic of bundle and parsing is included on this package", "license": "MIT", "main": "dist/index.js", diff --git a/core/docz-core/src/lib/Entries.ts b/core/docz-core/src/lib/Entries.ts index 41110f691..f7f73f420 100644 --- a/core/docz-core/src/lib/Entries.ts +++ b/core/docz-core/src/lib/Entries.ts @@ -85,7 +85,12 @@ export class Entries { const entry = new Entry(ast, file, config) if (this.repoEditUrl) entry.setLink(this.repoEditUrl) - const { settings, ...rest } = entry + + // reduce modify entry plugin + const reduce = Plugin.reduceFromPlugins(plugins) + const modifiedEntry = reduce('modifyEntry', entry, config) + + const { settings, ...rest } = modifiedEntry return { ...settings, @@ -98,7 +103,7 @@ export class Entries { } const reduce = Plugin.reduceFromPlugins(plugins) - const modifiedFiles = reduce('modifyFiles', files) + const modifiedFiles = reduce('modifyFiles', files, config) const map = new Map() const entries = await Promise.all( diff --git a/core/docz-core/src/lib/Plugin.ts b/core/docz-core/src/lib/Plugin.ts index 059d511aa..31b481819 100644 --- a/core/docz-core/src/lib/Plugin.ts +++ b/core/docz-core/src/lib/Plugin.ts @@ -2,18 +2,20 @@ import { get, isFunction } from 'lodash/fp' import { pReduce } from '../utils/p-reduce' import { Config } from '../config/argv' +import { Entry } from './Entry' export type SetConfig = (config: Config) => Config | Promise -export type onCreateBabelConfig = (params: any, dev: boolean) => void +export type OnCreateBabelConfig = (params: any, dev: boolean) => void -export type onCreateWebpackConfig = ( +export type OnCreateWebpackConfig = ( config: C, dev: boolean, args: Config ) => C export type ModifyFiles = (files: string[], args: Config) => string[] -export type onCreateDevServer = (app: A) => void +export type ModifyEntry = (entry: Entry, args: Config) => Entry +export type OnCreateDevServer = (app: A) => void export type OnPreBuild = (args: Config) => void export type OnPostBuild = (args: Config) => void export type OnPreRender = () => void @@ -21,10 +23,11 @@ export type OnPostRender = () => void export interface PluginFactory { setConfig?: SetConfig - onCreateBabelConfig?: onCreateBabelConfig - onCreateDevServer?: onCreateDevServer - onCreateWebpackConfig?: onCreateWebpackConfig + onCreateBabelConfig?: OnCreateBabelConfig + onCreateDevServer?: OnCreateDevServer + onCreateWebpackConfig?: OnCreateWebpackConfig modifyFiles?: ModifyFiles + modifyEntry?: ModifyEntry onPreBuild?: OnPreBuild onPostBuild?: OnPostBuild } @@ -81,10 +84,11 @@ export class Plugin implements PluginFactory { } public readonly setConfig?: SetConfig - public readonly onCreateWebpackConfig?: onCreateWebpackConfig - public readonly onCreateBabelConfig?: onCreateBabelConfig + public readonly onCreateWebpackConfig?: OnCreateWebpackConfig + public readonly onCreateBabelConfig?: OnCreateBabelConfig public readonly modifyFiles?: ModifyFiles - public readonly onCreateDevServer?: onCreateDevServer + public readonly modifyEntry?: ModifyEntry + public readonly onCreateDevServer?: OnCreateDevServer public readonly onPreBuild?: OnPreBuild public readonly onPostBuild?: OnPostBuild @@ -93,6 +97,7 @@ export class Plugin implements PluginFactory { this.onCreateWebpackConfig = p.onCreateWebpackConfig this.onCreateBabelConfig = p.onCreateBabelConfig this.modifyFiles = p.modifyFiles + this.modifyEntry = p.modifyEntry this.onCreateDevServer = p.onCreateDevServer this.onPreBuild = p.onPreBuild this.onPostBuild = p.onPostBuild diff --git a/examples/with-algolia-search/src/docs/references/creating-plugins.mdx b/examples/with-algolia-search/src/docs/references/creating-plugins.mdx index a85acc7c4..23fbf57b2 100644 --- a/examples/with-algolia-search/src/docs/references/creating-plugins.mdx +++ b/examples/with-algolia-search/src/docs/references/creating-plugins.mdx @@ -20,6 +20,7 @@ const myPlugin = () => createPlugin({ onCreateDevServer: (args) => /* ... */, onCreateWebpackConfig: (args) => /* ... */, modifyFiles: (files, args) => /* ... */, + modifyEntry: (args) => /* ... */, onPreBuild: () => /* ... */, onPostBuild: () => /* ... */, }) @@ -68,7 +69,7 @@ A set of preconfigured webpack config plugins ```js exports.onCreateWebpackConfig = ({ - stage, getConfig, rules, loaders, actions + stage, getConfig, rules, loaders, actions }) => { actions.setWebpackConfig({ module: { @@ -112,7 +113,7 @@ A set of preconfigured webpack config plugins ```js exports.onCreateBabelConfig = ({ - stage, getConfig, rules, loaders, actions + stage, getConfig, rules, loaders, actions }) => { actions.setBabelPlugin({ name: `@emotion/babel-plugin`, @@ -146,6 +147,27 @@ export type ModifyFiles = (files: string[], args: Config) => string[] --- +## `modifyEntry` + +Use to modify files entry after created + +#### Params + +- **entry:** files entry by Docz +- **args:** The Docz config object merged with argv + +#### Return + +- `Entry` + +#### Types definitions + +```ts +export type ModifyEntry = (entry: Entry, args: Config) => Entry +``` + +--- + ## `onCreateDevServer` Run when gatsby develop server is started, its useful to add proxy and middleware to the dev server app