Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
chore(docz): simplify bundler constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 22, 2018
1 parent eea3d94 commit bfe25d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
19 changes: 8 additions & 11 deletions packages/docz-bundler-webpack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { Configuration } from 'webpack'
import { Configuration as CFG } from 'webpack'
import { ConfigArgs, createBundler, BundlerCreate } from 'docz'
import * as WebpackDevServer from 'webpack-dev-server'
import * as Server from 'webpack-dev-server'
import webpack from 'webpack'

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

export const compiler = (args: ConfigArgs) => (cfg: Configuration) =>
webpack(cfg)
export const server = (args: ConfigArgs) => (config: CFG): Server => {
const compiler = webpack(config)
const devserver = devServerConfig(args)

export const server = (cfg: ConfigArgs) => (comp: any): WebpackDevServer =>
new WebpackDevServer(comp, devServerConfig(cfg))
return new Server(compiler, devserver)
}

export const bundler: BundlerCreate<
Configuration,
WebpackDevServer
> = createBundler<Configuration, WebpackDevServer>({
compiler,
export const bundler: BundlerCreate<CFG, Server> = createBundler<CFG, Server>({
config,
server,
id: 'webpack',
Expand Down
41 changes: 21 additions & 20 deletions packages/docz/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Plugin } from './Plugin'
import { ConfigArgs } from './Server'

export type TConfigFn<C> = () => C
export type TCompilerFn<C> = (config: C) => any
export type TServerFn<S> = (compiler: any) => S
export type TServerFn<C, S> = (config: C) => S

export interface CompilerOpts {
args: ConfigArgs
Expand All @@ -12,57 +11,60 @@ export interface CompilerOpts {
export interface BundlerConstructor<C, S> extends CompilerOpts {
id: string
config: TConfigFn<C>
compiler: TCompilerFn<C>
server: TServerFn<S>
server: TServerFn<C, S>
}

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

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

this.args = args
this.id = id
this.config = config
this.compiler = compiler
this.server = server
}

public async createCompiler(): Promise<any> {
return this.compiler(this.mountConfig())
public getConfig(): C {
return this.mountConfig(this.config())
}

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

if (plugins && plugins.length > 0) {
for (const plugin of plugins) {
await plugin.bundlerServer(server)
}
}

return server
}

private reduceWithPlugins(dev: boolean): any {
return (config: C, plugin: Plugin) =>
plugin.bundlerConfig(config, dev) || config
}

private mountConfig(): C {
private mountConfig(config: C): C {
const { plugins, env } = this.args

const dev = env === 'development'
const initialConfig = this.config()

return plugins && plugins.length > 0
? plugins.reduce(this.reduceWithPlugins(dev), initialConfig)
: initialConfig
? plugins.reduce(this.reduceWithPlugins(dev), config)
: config
}
}

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

export type BundlerCreate<C, S> = (args: ConfigArgs) => Bundler<C, S>
Expand All @@ -73,7 +75,6 @@ export function createBundler<C, S>(
return (args: ConfigArgs): Bundler<C, S> =>
new Bundler({
args,
compiler: factory.compiler(args),
config: factory.config(args),
id: factory.id,
server: factory.server(args),
Expand Down
14 changes: 3 additions & 11 deletions packages/docz/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,13 @@ export class Server {
}

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

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

const compiler = await this.bundler.createCompiler()
const server = await this.bundler.createServer(compiler)

if (plugins && plugins.length > 0) {
for (const plugin of plugins) {
await plugin.bundlerCompiler(compiler)
await plugin.bundlerServer(server)
}
}
const config = this.bundler.getConfig()
const server = await this.bundler.createServer(config)

server.listen(port)
}
Expand All @@ -84,7 +77,6 @@ export class Server {

private processEntries(config: ConfigArgs): void {
const entries = new Entries(config)

const update = () => entries.write()

const onUnlink = (file: string) => {
Expand Down

0 comments on commit bfe25d0

Please sign in to comment.