Skip to content

Commit

Permalink
feat(docz): make server bundler api concise
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 22, 2018
1 parent bfe25d0 commit ae5f0c0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
13 changes: 8 additions & 5 deletions packages/docz-bundler-webpack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Configuration as CFG } from 'webpack'
import { ConfigArgs, createBundler, BundlerCreate } from 'docz'
import * as Server from 'webpack-dev-server'
import { ConfigArgs, createBundler, BundlerCreate, BundlerServer } from 'docz'
import * as WebpackDevServer from 'webpack-dev-server'
import webpack from 'webpack'

import { devServerConfig } from './devserver'
import { createConfig as config } from './config'

export const server = (args: ConfigArgs) => (config: CFG): Server => {
export const server = (args: ConfigArgs) => (config: CFG): BundlerServer => {
const compiler = webpack(config)
const devserver = devServerConfig(args)
const server = new WebpackDevServer(compiler, devserver)

return new Server(compiler, devserver)
return {
start: () => server.listen(args.port),
}
}

export const bundler: BundlerCreate<CFG, Server> = createBundler<CFG, Server>({
export const bundler: BundlerCreate<CFG> = createBundler<CFG>({
config,
server,
id: 'webpack',
Expand Down
40 changes: 21 additions & 19 deletions packages/docz/src/Bundler.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { Plugin } from './Plugin'
import { ConfigArgs } from './Server'

export type TConfigFn<C> = () => C
export type TServerFn<C, S> = (config: C) => S
export interface BundlerServer {
start(): any
}

export type ConfigFn<C> = () => C
export type ServerFn<C> = (config: C) => BundlerServer

export interface CompilerOpts {
args: ConfigArgs
}

export interface BundlerConstructor<C, S> extends CompilerOpts {
export interface BundlerConstructor<C> extends CompilerOpts {
id: string
config: TConfigFn<C>
server: TServerFn<C, S>
config: ConfigFn<C>
server: ServerFn<C>
}

export class Bundler<C = any, S = any> {
export class Bundler<C = any> {
public readonly id: string
private readonly args: ConfigArgs
private config: TConfigFn<C>
private server: TServerFn<C, S>
private config: ConfigFn<C>
private server: ServerFn<C>

constructor(params: BundlerConstructor<C, S>) {
constructor(params: BundlerConstructor<C>) {
const { args, id, config, server } = params

this.args = args
Expand All @@ -33,7 +37,7 @@ export class Bundler<C = any, S = any> {
return this.mountConfig(this.config())
}

public async createServer(config: C): Promise<S> {
public async createServer(config: C): Promise<BundlerServer> {
const { plugins } = this.args
const server = await this.server(config)

Expand Down Expand Up @@ -61,22 +65,20 @@ export class Bundler<C = any, S = any> {
}
}

export interface Factory<C, S> {
export interface Factory<C> {
id: string
config: (args: ConfigArgs) => TConfigFn<C>
server: (args: ConfigArgs) => TServerFn<C, S>
config: (args: ConfigArgs) => ConfigFn<C>
server: (args: ConfigArgs) => ServerFn<C>
}

export type BundlerCreate<C, S> = (args: ConfigArgs) => Bundler<C, S>
export type BundlerCreate<C> = (args: ConfigArgs) => Bundler<C>

export function createBundler<C, S>(
factory: Factory<C, S>
): BundlerCreate<C, S> {
return (args: ConfigArgs): Bundler<C, S> =>
export function createBundler<C>(factory: Factory<C>): BundlerCreate<C> {
return (args: ConfigArgs): Bundler<C> =>
new Bundler({
args,
config: factory.config(args),
id: factory.id,
config: factory.config(args),
server: factory.server(args),
})
}
4 changes: 1 addition & 3 deletions packages/docz/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ export class Server {
}

public async start(): Promise<void> {
const { port } = this.config

del.sync(paths.docz)
this.processEntries(this.config)

const config = this.bundler.getConfig()
const server = await this.bundler.createServer(config)

server.listen(port)
server.start()
}

private getBundler(bundler: string): any {
Expand Down
2 changes: 1 addition & 1 deletion packages/docz/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Entry } from './Entry'
export { createBundler, BundlerCreate } from './Bundler'
export { createBundler, BundlerCreate, BundlerServer } from './Bundler'
export { createPlugin } from './Plugin'
export { Server, ConfigArgs } from './Server'

0 comments on commit ae5f0c0

Please sign in to comment.