Skip to content

Commit

Permalink
feat(docz-core): add setConfig method for Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 28, 2018
1 parent c88e9f7 commit 9ba6507
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
14 changes: 2 additions & 12 deletions packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Plugin } from './Plugin'
import { Config as Args } from './commands/args'

import { isFn } from './utils/helpers'

export interface Server {
close: () => void
on: (event: string, cb: (server: any) => void) => void
Expand Down Expand Up @@ -49,19 +47,11 @@ export class Bundler<C = any> {
await this.builder(config)
}

private reduceWithPlugins(dev: boolean): any {
return (config: C, { modifyBundlerConfig }: Plugin) =>
modifyBundlerConfig && isFn(modifyBundlerConfig)
? modifyBundlerConfig(config, dev)
: config
}

private mountConfig(config: C): any {
const { plugins, env } = this.args
const dev = env === 'development'
const reduce = Plugin.reduceFromPlugins<C>(plugins)

return plugins && plugins.length > 0
? plugins.reduce(this.reduceWithPlugins(dev), config)
: config
return reduce('modifyBundlerConfig', config, dev)
}
}
26 changes: 18 additions & 8 deletions packages/docz-core/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import get from 'lodash.get'

import { Config } from './commands/args'
import { isFn } from './utils/helpers'
import { BabelRC } from './utils/babelrc'

export interface BabelRC {
presets?: any[]
plugins?: any[]
cacheDirectory?: boolean
babelrc?: boolean
}

export type SetConfig = (config: Config) => Config
export type ModifyBundlerConfig<C = any> = (config: C, dev: boolean) => C
export type ModifyBabelRC = (babelrc: BabelRC) => BabelRC
export type OnServerListening = <S>(server: S) => void
Expand All @@ -19,6 +15,7 @@ export type OnPostRender = () => void
export type Wrapper = <R>(props: any) => R

export interface PluginFactory {
setConfig?: SetConfig
modifyBundlerConfig?: ModifyBundlerConfig
modifyBabelRc?: ModifyBabelRC
onServerListening?: OnServerListening
Expand All @@ -31,7 +28,7 @@ export interface PluginFactory {

export class Plugin<C = any> implements PluginFactory {
public static runPluginsMethod(
plugins: Plugin[]
plugins: Plugin[] | undefined
): (method: keyof Plugin, ...args: any[]) => void {
return (method, ...args) => {
if (plugins && plugins.length > 0) {
Expand All @@ -52,6 +49,18 @@ export class Plugin<C = any> implements PluginFactory {
plugins.map(p => get(p, prop)).filter(m => m)
}

public static reduceFromPlugins<C>(
plugins: Plugin[] | undefined
): (method: keyof Plugin, initial: C, ...args: any[]) => C {
return (method, initial, ...args) => {
return [...(plugins || [])].reduce((obj: any, plugin) => {
const fn = get(plugin, method)
return fn && isFn(fn) ? fn(obj) : obj
}, initial)
}
}

public readonly setConfig?: SetConfig
public readonly modifyBundlerConfig?: ModifyBundlerConfig<C>
public readonly modifyBabelRc?: ModifyBabelRC
public readonly onServerListening?: OnServerListening
Expand All @@ -62,6 +71,7 @@ export class Plugin<C = any> implements PluginFactory {
public readonly wrapper?: Wrapper

constructor(p: PluginFactory) {
this.setConfig = p.setConfig
this.modifyBundlerConfig = p.modifyBundlerConfig
this.modifyBabelRc = p.modifyBabelRc
this.onServerListening = p.onServerListening
Expand Down
4 changes: 2 additions & 2 deletions packages/docz-core/src/bundlers/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import UglifyJs from 'uglifyjs-webpack-plugin'
import matter from 'remark-frontmatter'
import merge from 'deepmerge'

import { Config as Args } from '../../commands/args'
import { BabelRC } from '../../utils/babelrc'
import { plugin as mdastPlugin } from '../../utils/plugin-mdast'
import { plugin as hastPlugin } from '../../utils/plugin-hast'
import { BabelRC } from '../../Plugin'
import { Config as Args } from '../../commands/args'
import { Env } from './'

const INLINE_LIMIT = 10000
Expand Down
21 changes: 12 additions & 9 deletions packages/docz-core/src/utils/babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { load } from 'load-cfg'
import merge from 'deepmerge'

import { Config } from '../commands/args'
import { isFn } from './helpers'
import { Plugin } from '../Plugin'

export const babelrc = (args: Config) => {
export interface BabelRC {
presets?: any[]
plugins?: any[]
cacheDirectory?: boolean
babelrc?: boolean
}

export const babelrc = (args: Config): BabelRC => {
const config = merge(load('babel', null), {
babelrc: false,
cacheDirectory: !args.debug,
Expand All @@ -15,11 +22,7 @@ export const babelrc = (args: Config) => {
plugins: [],
})

return [...(args.plugins || [])].reduce(
(obj, plugin) =>
plugin.modifyBabelRc && isFn(plugin.modifyBabelRc)
? plugin.modifyBabelRc(config)
: obj,
config
)
const reduce = Plugin.reduceFromPlugins<BabelRC>(args.plugins)

return reduce('modifyBabelRc', config)
}
12 changes: 10 additions & 2 deletions packages/docz-core/src/utils/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import { load } from 'load-cfg'

import * as paths from '../config/paths'
import { Config } from '../commands/args'
import { Plugin } from '../Plugin'
import { omit } from './helpers'

export const loadConfig = (args: Config) =>
load('docz', {
const toOmit = ['_', '$0', 'version', 'help']

export const loadConfig = (args: Config): Config => {
const config = load('docz', {
...args,
paths,
plugins: [],
mdPlugins: [],
hastPlugins: [],
themeConfig: {},
})

const reduce = Plugin.reduceFromPlugins<Config>(config.plugins)
return omit<Config>(toOmit, reduce('setConfig', config))
}

0 comments on commit 9ba6507

Please sign in to comment.