Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 317987e

Browse files
authored
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
1 parent ed169cf commit 317987e

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

core/docz-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docz-core",
3-
"version": "2.3.2-alpha.0",
3+
"version": "2.3.2-alpha.1",
44
"description": "All docz core logic of bundle and parsing is included on this package",
55
"license": "MIT",
66
"main": "dist/index.js",

core/docz-core/src/lib/Entries.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ export class Entries {
8585
const entry = new Entry(ast, file, config)
8686

8787
if (this.repoEditUrl) entry.setLink(this.repoEditUrl)
88-
const { settings, ...rest } = entry
88+
89+
// reduce modify entry plugin
90+
const reduce = Plugin.reduceFromPlugins<Entry>(plugins)
91+
const modifiedEntry = reduce('modifyEntry', entry, config)
92+
93+
const { settings, ...rest } = modifiedEntry
8994

9095
return {
9196
...settings,
@@ -98,7 +103,7 @@ export class Entries {
98103
}
99104

100105
const reduce = Plugin.reduceFromPlugins<string[]>(plugins)
101-
const modifiedFiles = reduce('modifyFiles', files)
106+
const modifiedFiles = reduce('modifyFiles', files, config)
102107

103108
const map = new Map()
104109
const entries = await Promise.all(

core/docz-core/src/lib/Plugin.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@ import { get, isFunction } from 'lodash/fp'
22

33
import { pReduce } from '../utils/p-reduce'
44
import { Config } from '../config/argv'
5+
import { Entry } from './Entry'
56

67
export type SetConfig = (config: Config) => Config | Promise<Config>
7-
export type onCreateBabelConfig = (params: any, dev: boolean) => void
8+
export type OnCreateBabelConfig = (params: any, dev: boolean) => void
89

9-
export type onCreateWebpackConfig<C = any> = (
10+
export type OnCreateWebpackConfig<C = any> = (
1011
config: C,
1112
dev: boolean,
1213
args: Config
1314
) => C
1415

1516
export type ModifyFiles = (files: string[], args: Config) => string[]
16-
export type onCreateDevServer = <A>(app: A) => void
17+
export type ModifyEntry = (entry: Entry, args: Config) => Entry
18+
export type OnCreateDevServer = <A>(app: A) => void
1719
export type OnPreBuild = (args: Config) => void
1820
export type OnPostBuild = (args: Config) => void
1921
export type OnPreRender = () => void
2022
export type OnPostRender = () => void
2123

2224
export interface PluginFactory {
2325
setConfig?: SetConfig
24-
onCreateBabelConfig?: onCreateBabelConfig
25-
onCreateDevServer?: onCreateDevServer
26-
onCreateWebpackConfig?: onCreateWebpackConfig
26+
onCreateBabelConfig?: OnCreateBabelConfig
27+
onCreateDevServer?: OnCreateDevServer
28+
onCreateWebpackConfig?: OnCreateWebpackConfig
2729
modifyFiles?: ModifyFiles
30+
modifyEntry?: ModifyEntry
2831
onPreBuild?: OnPreBuild
2932
onPostBuild?: OnPostBuild
3033
}
@@ -81,10 +84,11 @@ export class Plugin<C = any> implements PluginFactory {
8184
}
8285

8386
public readonly setConfig?: SetConfig
84-
public readonly onCreateWebpackConfig?: onCreateWebpackConfig<C>
85-
public readonly onCreateBabelConfig?: onCreateBabelConfig
87+
public readonly onCreateWebpackConfig?: OnCreateWebpackConfig<C>
88+
public readonly onCreateBabelConfig?: OnCreateBabelConfig
8689
public readonly modifyFiles?: ModifyFiles
87-
public readonly onCreateDevServer?: onCreateDevServer
90+
public readonly modifyEntry?: ModifyEntry
91+
public readonly onCreateDevServer?: OnCreateDevServer
8892
public readonly onPreBuild?: OnPreBuild
8993
public readonly onPostBuild?: OnPostBuild
9094

@@ -93,6 +97,7 @@ export class Plugin<C = any> implements PluginFactory {
9397
this.onCreateWebpackConfig = p.onCreateWebpackConfig
9498
this.onCreateBabelConfig = p.onCreateBabelConfig
9599
this.modifyFiles = p.modifyFiles
100+
this.modifyEntry = p.modifyEntry
96101
this.onCreateDevServer = p.onCreateDevServer
97102
this.onPreBuild = p.onPreBuild
98103
this.onPostBuild = p.onPostBuild

examples/with-algolia-search/src/docs/references/creating-plugins.mdx

+24-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const myPlugin = () => createPlugin({
2020
onCreateDevServer: (args) => /* ... */,
2121
onCreateWebpackConfig: (args) => /* ... */,
2222
modifyFiles: (files, args) => /* ... */,
23+
modifyEntry: (args) => /* ... */,
2324
onPreBuild: () => /* ... */,
2425
onPostBuild: () => /* ... */,
2526
})
@@ -68,7 +69,7 @@ A set of preconfigured webpack config plugins
6869

6970
```js
7071
exports.onCreateWebpackConfig = ({
71-
stage, getConfig, rules, loaders, actions
72+
stage, getConfig, rules, loaders, actions
7273
}) => {
7374
actions.setWebpackConfig({
7475
module: {
@@ -112,7 +113,7 @@ A set of preconfigured webpack config plugins
112113

113114
```js
114115
exports.onCreateBabelConfig = ({
115-
stage, getConfig, rules, loaders, actions
116+
stage, getConfig, rules, loaders, actions
116117
}) => {
117118
actions.setBabelPlugin({
118119
name: `@emotion/babel-plugin`,
@@ -146,6 +147,27 @@ export type ModifyFiles = (files: string[], args: Config) => string[]
146147
147148
---
148149
150+
## `modifyEntry`
151+
152+
Use to modify files entry after created
153+
154+
#### Params
155+
156+
- **entry:** files entry by Docz
157+
- **args:** The Docz config object merged with argv
158+
159+
#### Return
160+
161+
- `Entry`
162+
163+
#### Types definitions
164+
165+
```ts
166+
export type ModifyEntry = (entry: Entry, args: Config) => Entry
167+
```
168+
169+
---
170+
149171
## `onCreateDevServer`
150172
151173
Run when gatsby develop server is started, its useful to add proxy and middleware to the dev server app

0 commit comments

Comments
 (0)