Skip to content

Commit

Permalink
feat(docz-core): add modifyEntry plugin supported (#1621)
Browse files Browse the repository at this point in the history
* feat(docz-core): add modifyEntry plugin supported

- add modifyEntry plugin supported
- fix OnCreateDevServer type name typo

* fix(docz-core): fix type name typo
  • Loading branch information
yinxulai authored Jun 3, 2021
1 parent ed169cf commit 317987e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/docz-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 7 additions & 2 deletions core/docz-core/src/lib/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry>(plugins)
const modifiedEntry = reduce('modifyEntry', entry, config)

const { settings, ...rest } = modifiedEntry

return {
...settings,
Expand All @@ -98,7 +103,7 @@ export class Entries {
}

const reduce = Plugin.reduceFromPlugins<string[]>(plugins)
const modifiedFiles = reduce('modifyFiles', files)
const modifiedFiles = reduce('modifyFiles', files, config)

const map = new Map()
const entries = await Promise.all(
Expand Down
23 changes: 14 additions & 9 deletions core/docz-core/src/lib/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ 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<Config>
export type onCreateBabelConfig = (params: any, dev: boolean) => void
export type OnCreateBabelConfig = (params: any, dev: boolean) => void

export type onCreateWebpackConfig<C = any> = (
export type OnCreateWebpackConfig<C = any> = (
config: C,
dev: boolean,
args: Config
) => C

export type ModifyFiles = (files: string[], args: Config) => string[]
export type onCreateDevServer = <A>(app: A) => void
export type ModifyEntry = (entry: Entry, args: Config) => Entry
export type OnCreateDevServer = <A>(app: A) => void
export type OnPreBuild = (args: Config) => void
export type OnPostBuild = (args: Config) => void
export type OnPreRender = () => void
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
}
Expand Down Expand Up @@ -81,10 +84,11 @@ export class Plugin<C = any> implements PluginFactory {
}

public readonly setConfig?: SetConfig
public readonly onCreateWebpackConfig?: onCreateWebpackConfig<C>
public readonly onCreateBabelConfig?: onCreateBabelConfig
public readonly onCreateWebpackConfig?: OnCreateWebpackConfig<C>
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

Expand All @@ -93,6 +97,7 @@ export class Plugin<C = any> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const myPlugin = () => createPlugin({
onCreateDevServer: (args) => /* ... */,
onCreateWebpackConfig: (args) => /* ... */,
modifyFiles: (files, args) => /* ... */,
modifyEntry: (args) => /* ... */,
onPreBuild: () => /* ... */,
onPostBuild: () => /* ... */,
})
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 317987e

Please sign in to comment.