Skip to content

Commit

Permalink
feat(docz-core): add onCreateApp plugin method
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 19, 2018
1 parent f6b6260 commit 13c07e7
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 28 deletions.
2 changes: 2 additions & 0 deletions packages/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"hast-util-to-string": "^1.0.1",
"html-webpack-plugin": "^3.2.0",
"humanize-string": "^1.0.2",
"koa": "^2.5.2",
"koa-connect": "^2.0.1",
"koa-mount": "^3.0.0",
"koa-static": "^5.0.0",
Expand Down Expand Up @@ -98,6 +99,7 @@
"@types/express": "^4.16.0",
"@types/fs-extra": "^5.0.4",
"@types/html-webpack-plugin": "^2.30.4",
"@types/koa": "^2.0.46",
"@types/lodash.get": "^4.4.3",
"@types/node": "10.5.2",
"@types/prettier": "^1.13.2",
Expand Down
21 changes: 19 additions & 2 deletions packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ export interface Server {
close: () => void
}

export interface ServerHooks {
onCreateApp<A>(app: A): void
OnServerListening<S>(server: S): void
}

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

export type ConfigFn<C> = (babelrc: BabelRC) => C
export type ServerFn<C> = (config: C) => BundlerServer | Promise<BundlerServer>
export type BuildFn<C> = (config: C, dist: string) => void

export type ServerFnReturn = BundlerServer | Promise<BundlerServer>
export type ServerFn<C> = (config: C, hooks: ServerHooks) => ServerFnReturn

export interface BundlerConstructor<Config> {
args: Args
config: ConfigFn<Config>
Expand Down Expand Up @@ -52,7 +59,17 @@ export class Bundler<C = ConfigObj> {
}

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

return this.server(config, hooks)
}

public async build(config: C): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions packages/docz-core/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ModifyBundlerConfig<C = any> = (
args: Config
) => C
export type ModifyBabelRC = (babelrc: BabelRC, args: Config) => BabelRC
export type onCreateApp = <A>(app: A) => void
export type OnServerListening = <S>(server: S) => void
export type OnPreBuild = () => void
export type OnPostBuild = () => void
Expand All @@ -21,6 +22,7 @@ export interface PluginFactory {
setConfig?: SetConfig
modifyBundlerConfig?: ModifyBundlerConfig
modifyBabelRc?: ModifyBabelRC
onCreateApp?: onCreateApp
onServerListening?: OnServerListening
onPreBuild?: OnPreBuild
onPostBuild?: OnPostBuild
Expand Down Expand Up @@ -65,6 +67,7 @@ export class Plugin<C = any> implements PluginFactory {
public readonly setConfig?: SetConfig
public readonly modifyBundlerConfig?: ModifyBundlerConfig<C>
public readonly modifyBabelRc?: ModifyBabelRC
public readonly onCreateApp?: onCreateApp
public readonly onServerListening?: OnServerListening
public readonly onPreBuild?: OnPreBuild
public readonly onPostBuild?: OnPostBuild
Expand All @@ -75,6 +78,7 @@ export class Plugin<C = any> implements PluginFactory {
this.setConfig = p.setConfig
this.modifyBundlerConfig = p.modifyBundlerConfig
this.modifyBabelRc = p.modifyBabelRc
this.onCreateApp = p.onCreateApp
this.onServerListening = p.onServerListening
this.onPreBuild = p.onPreBuild
this.onPostBuild = p.onPostBuild
Expand Down
14 changes: 12 additions & 2 deletions packages/docz-core/src/bundlers/webpack/devserver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as path from 'path'
import * as Koa from 'koa'
import { Configuration } from 'webpack'
import convert from 'koa-connect'
import history from 'connect-history-api-fallback'
import serveWaitpage from 'webpack-serve-waitpage'

import { Config } from '../../commands/args'
import { ServerHooks } from '../../Bundler'

export const devServerConfig = (args: Config, config: Configuration) => {
export const devServerConfig = (
args: Config,
config: Configuration,
hooks: ServerHooks
) => {
const nonExistentDir = path.resolve(__dirname, 'non-existent')
const logLevel = (level: string) => (args.debug ? 'debug' : level)

Expand All @@ -23,7 +29,10 @@ export const devServerConfig = (args: Config, config: Configuration) => {
reload: false,
logLevel: logLevel('error'),
},
add: (app: any, middleware: any, options: any) => {
add: (app: Koa, middleware: any, options: any) => {
middleware.webpack()
middleware.content()

app.use(
convert(
history({
Expand All @@ -33,6 +42,7 @@ export const devServerConfig = (args: Config, config: Configuration) => {
)

app.use(serveWaitpage(options, { title: args.title }))
hooks.onCreateApp<Koa>(app)
},
}
}
11 changes: 8 additions & 3 deletions packages/docz-core/src/bundlers/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { Configuration as Config } from 'webpack'
import serve from 'webpack-serve'

import { devServerConfig } from './devserver'
import { BundlerServer } from '../../Bundler'
import { BundlerServer, ServerHooks } from '../../Bundler'
import { Config as Args } from '../../commands/args'
import * as http from 'http'

type Server = Promise<BundlerServer>

export const server = (args: Args) => async (config: Config): Server => {
const devserver = devServerConfig(args, config)
export const server = (args: Args) => async (
config: Config,
hooks: ServerHooks
): Server => {
const devserver = devServerConfig(args, config, hooks)

return {
start: async () => {
const instance = await serve({}, devserver)
hooks.OnServerListening<http.Server>(instance.app.server)

return {
...instance,
Expand Down
3 changes: 0 additions & 3 deletions packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Config, Env } from './args'
import { DataServer } from '../DataServer'
import { webpack } from '../bundlers'
import { Entries } from '../Entries'
import { Plugin } from '../Plugin'
import { loadConfig } from '../utils/load-config'

const env = process.env.NODE_ENV as Env
Expand All @@ -25,7 +24,6 @@ export const dev = async (args: Config) => {
const server = await bundler.createServer(bundler.getConfig(env))
const { app } = await server.start()

const run = Plugin.runPluginsMethod(config.plugins)
const newConfig = { ...config, websocketPort }
const dataServer = new DataServer({
server: app.server,
Expand All @@ -43,7 +41,6 @@ export const dev = async (args: Config) => {
logger.info(`Setup entries websockets server on port ${websocketPort}`)
await dataServer.processEntries(entries)
await dataServer.processThemeConfig()
await run('onServerListening', server)
} catch (err) {
logger.fatal('Failed to process your server:', err)
process.exit(1)
Expand Down
Loading

0 comments on commit 13c07e7

Please sign in to comment.