Skip to content

Commit

Permalink
feat(docz-core): add onCreateWebpackChain hook
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Dec 17, 2018
1 parent 2797608 commit 70bb242
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"humanize-string": "^1.0.2",
"koa-range": "^0.3.0",
"load-cfg": "^0.12.16",
"lodash.get": "^4.4.2",
"lodash": "^4.17.11",
"mini-html-webpack-plugin": "^0.2.3",
"p-reduce": "^1.0.0",
"progress-estimator": "^0.2.2",
Expand Down
40 changes: 22 additions & 18 deletions packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import logger from 'signale'
import WebpackDevServer from 'webpack-dev-server'

import { Plugin } from './Plugin'

import { Config as Args, Env } from './commands/args'
import { getBabelConfig, BabelRC } from './utils/babel-config'
import * as paths from './config/paths'

export interface ServerHooks {
onCreateWebpackChain<C>(config: C, dev: boolean, args: Args): void
onPreCreateApp<A>(app: A): void
onCreateApp<A>(app: A): void
OnServerListening<S>(server: S): void
onServerListening<S>(server: S): void
}

export interface BundlerServer {
start(): Promise<WebpackDevServer>
}

export type ConfigFn<C> = (babelrc: BabelRC) => Promise<C>
export type ConfigFn<C> = (babelrc: BabelRC, hooks: ServerHooks) => Promise<C>
export type BuildFn<C> = (config: C, dist: string, publicDir: string) => void
export type ServerFn<C> = (
config: C,
Expand All @@ -41,42 +41,46 @@ export class Bundler<C = ConfigObj> {
private config: ConfigFn<C>
private server: ServerFn<C>
private builder: BuildFn<C>
private hooks: ServerHooks

constructor(params: BundlerConstructor<C>) {
const { args, config, server, build } = params
const run = Plugin.runPluginsMethod(args.plugins)

this.args = args
this.config = config
this.server = server
this.builder = build

this.hooks = {
onCreateWebpackChain<C>(config: C, dev: boolean, args: Args): void {
run('onCreateApp', config, dev, args)
},
onPreCreateApp<A>(app: A): void {
run('onPreCreateApp', app)
},
onCreateApp<A>(app: A): void {
run('onCreateApp', app)
},
onServerListening<S>(server: S): void {
run('onServerListening', server)
},
}
}

public async mountConfig(env: Env): Promise<C> {
const { plugins } = this.args
const isDev = env !== 'production'
const reduce = Plugin.reduceFromPlugins<C>(plugins)
const babelConfig = await getBabelConfig(this.args, env)
const userConfig = await this.config(babelConfig)
const userConfig = await this.config(babelConfig, this.hooks)
const config = reduce('modifyBundlerConfig', userConfig, isDev, this.args)

return this.args.modifyBundlerConfig(config, isDev, this.args)
}

public async createApp(config: C): Promise<BundlerServer> {
const run = Plugin.runPluginsMethod(this.args.plugins)
const hooks = {
onPreCreateApp<A>(app: A): void {
run('onPreCreateApp', app)
},
onCreateApp<A>(app: A): void {
run('onCreateApp', app)
},
OnServerListening<S>(server: S): void {
run('onServerListening', server)
},
}

return this.server(config, hooks)
return this.server(config, this.hooks)
}

public async build(config: C): Promise<void> {
Expand Down
19 changes: 16 additions & 3 deletions packages/docz-core/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import get from 'lodash.get'
import pReduce from 'p-reduce'
import WebpackChainConfig from 'webpack-chain'
import get from 'lodash/get'

import { Config } from './commands/args'
import { isFn } from './utils/helpers'
import { BabelRC } from './utils/babel-config'

export type SetConfig = (config: Config) => Config | Promise<Config>

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

export type ModifyBabelRC = (babelrc: BabelRC, args: Config) => BabelRC
export type ModifyFiles = (files: string[], args: Config) => string[]

export type OnCreateWebpackChain = (
config: WebpackChainConfig,
dev: boolean,
args: Config
) => void

export type onPreCreateApp = <A>(app: A) => void
export type onCreateApp = <A>(app: A) => void
export type OnServerListening = <S>(server: S) => void
Expand All @@ -26,6 +36,7 @@ export interface PluginFactory {
modifyBundlerConfig?: ModifyBundlerConfig
modifyBabelRc?: ModifyBabelRC
modifyFiles?: ModifyFiles
onCreateWebpackChain?: OnCreateWebpackChain
onPreCreateApp?: onPreCreateApp
onCreateApp?: onCreateApp
onServerListening?: OnServerListening
Expand All @@ -42,7 +53,7 @@ export class Plugin<C = any> implements PluginFactory {
return (method, ...args) => {
if (plugins && plugins.length > 0) {
for (const plugin of plugins) {
const fn = get(plugin, method)
const fn = get<Plugin, any>(plugin, method)
isFn(fn) && fn(...args)
}
}
Expand All @@ -63,7 +74,7 @@ export class Plugin<C = any> implements PluginFactory {
): (method: keyof Plugin, initial: C, ...args: any[]) => C {
return (method, initial, ...args) => {
return [...(plugins || [])].reduce((obj: any, plugin) => {
const fn = get(plugin, method)
const fn = get<Plugin, any>(plugin, method)
return fn && isFn(fn) ? fn(obj, ...args) : obj
}, initial)
}
Expand All @@ -88,6 +99,7 @@ export class Plugin<C = any> implements PluginFactory {
public readonly modifyBundlerConfig?: ModifyBundlerConfig<C>
public readonly modifyBabelRc?: ModifyBabelRC
public readonly modifyFiles?: ModifyFiles
public readonly onCreateWebpackChain?: OnCreateWebpackChain
public readonly onPreCreateApp?: onPreCreateApp
public readonly onCreateApp?: onCreateApp
public readonly onServerListening?: OnServerListening
Expand All @@ -102,6 +114,7 @@ export class Plugin<C = any> implements PluginFactory {
this.modifyBabelRc = p.modifyBabelRc
this.modifyFiles = p.modifyFiles
this.onPreCreateApp = p.onPreCreateApp
this.onCreateWebpackChain = p.onCreateWebpackChain
this.onCreateApp = p.onCreateApp
this.onServerListening = p.onServerListening
this.onPreBuild = p.onPreBuild
Expand Down
3 changes: 2 additions & 1 deletion packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs-extra'
import envDotProp from 'env-dot-prop'
import humanize from 'humanize-string'
import titleize from 'titleize'
import get from 'lodash.get'
import get from 'lodash/get'

import { Plugin } from '../Plugin'
import { BabelRC } from '../utils/babel-config'
Expand Down Expand Up @@ -90,6 +90,7 @@ export interface Config extends Argv {
themeConfig: ThemeConfig
modifyBundlerConfig<C>(config: C, dev: boolean, args: Config): C
modifyBabelRc(babelrc: BabelRC, args: Config): BabelRC
onCreateWebpackChain<C>(c: C, dev: boolean, args: Config): void
}

export const args = (env: Env) => (yargs: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs-extra'
import { load, finds } from 'load-cfg'
import chokidar from 'chokidar'
import get from 'lodash.get'
import get from 'lodash/get'

import { Params, State } from '../DataServer'
import { Config, Menu, ThemeConfig } from '../commands/args'
Expand Down
1 change: 0 additions & 1 deletion packages/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ declare module 'happypack'
declare module 'hast-util-to-string'
declare module 'html-minifer'
declare module 'humanize-string'
declare module 'lodash.get'
declare module 'mini-html-webpack-plugin'
declare module 'p-reduce'
declare module 'progress-estimator'
Expand Down
1 change: 1 addition & 0 deletions packages/docz-core/src/utils/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const loadConfig = async (args: Config): Promise<Config> => {
themeConfig: {},
modifyBundlerConfig: (config: any) => config,
modifyBabelRc: (babelrc: BabelRC) => babelrc,
onCreateWebpackChain: () => null,
}

const config = args.config
Expand Down
7 changes: 6 additions & 1 deletion packages/docz-core/src/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import * as paths from '../config/paths'
import { Config as Args, Env } from '../commands/args'
import { BabelRC } from '../utils/babel-config'
import { minifier } from './minifier'
import { ServerHooks } from '../Bundler'

export const createConfig = (args: Args, env: Env) => async (
babelrc: BabelRC
babelrc: BabelRC,
hooks: ServerHooks
): Promise<Configuration> => {
const { debug } = args

Expand Down Expand Up @@ -171,5 +173,8 @@ export const createConfig = (args: Args, env: Env) => async (
config.when(isProd, cfg => minifier(cfg))
config.performance.hints(false)

hooks.onCreateWebpackChain<Config>(config, !isProd, args)
args.onCreateWebpackChain<Config>(config, !isProd, args)

return config.toConfig() as Configuration
}
2 changes: 1 addition & 1 deletion packages/docz-core/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const server = (args: Args) => async (config: Config, hooks: Hooks) => {
const devServer = new WebpackDevServer(compiler, serverConfig)
devServer.listen(args.port, args.host, err => {
if (err) return console.log(err)
hooks.OnServerListening<WebpackDevServer>(devServer)
hooks.onServerListening<WebpackDevServer>(devServer)
})

return devServer
Expand Down

0 comments on commit 70bb242

Please sign in to comment.