Skip to content

Commit

Permalink
refactor(docz-core): update webpack serve to fit breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jul 11, 2018
1 parent d300c45 commit d39d6e9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/docz-core/src/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Plugin } from './Plugin'
import { Config as Args } from './commands/args'

export interface Server {
close: () => void
app: any
on: (event: string, cb: (server: any) => void) => void
close: () => void
}

export interface BundlerServer {
Expand Down
14 changes: 5 additions & 9 deletions packages/docz-core/src/bundlers/webpack/devserver.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import * as path from 'path'
import { Compiler, Configuration } from 'webpack'
import { Configuration } from 'webpack'
import convert from 'koa-connect'
import history from 'connect-history-api-fallback'

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

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

return {
compiler,
config,
host: args.host,
port: args.port,
content: [nonExistentDir],
logLevel: logLevel('error'),
dev: {
devMiddleware: {
logLevel: logLevel('silent'),
},
hot: {
hotClient: {
reload: false,
logLevel: logLevel('error'),
},
Expand Down
17 changes: 8 additions & 9 deletions packages/docz-core/src/bundlers/webpack/server.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import webpack, { Configuration as CFG } from 'webpack'
import { Configuration as Config } from 'webpack'
import serve from 'webpack-serve'
import detectPort from 'detect-port'

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

export const server = (args: Args) => async (
config: CFG
): Promise<BundlerServer> => {
const compiler = webpack(config)
type Server = Promise<BundlerServer>

export const server = (args: Args) => async (config: Config): Server => {
const port = await detectPort(args.port)
const devserver = devServerConfig({ ...args, port }, compiler, config)
const devserver = devServerConfig({ ...args, port }, config)

return {
start: async () => {
const instance = await serve(devserver)
const instance = await serve({}, devserver)

return {
on: instance.on,
close: instance.close,
...instance,
close: () => instance.app.stop(),
}
},
}
Expand Down
47 changes: 23 additions & 24 deletions packages/docz-core/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development'
export const dev = async (args: Config) => {
const config = loadConfig(args)
const port = await detectPort(config.port)
const bundler = webpack({ ...config, port }, 'development')
const websocketPort = await detectPort(config.websocketPort)
const entries = new Entries(config)
const map = await entries.get()
const bundler = webpack({ ...config, port }, 'development')
const server = await bundler.createServer(bundler.getConfig())
const app = await server.start()

app.on('listening', async ({ server, options }) => {
const websocketPort = await detectPort(config.websocketPort)
const run = Plugin.runPluginsMethod(config.plugins)
const newConfig = { ...config, websocketPort }
const dataServer = new DataServer({ server, config: newConfig })
const { app } = await server.start()
const run = Plugin.runPluginsMethod(config.plugins)
const newConfig = { ...config, websocketPort }
const dataServer = new DataServer({
server: app.server,
config: newConfig,
})

try {
logger.info('Removing old app files')
await fs.remove(paths.app)
try {
logger.info('Removing old app files')
await fs.remove(paths.app)

logger.info('Creating new docz files')
await Entries.writeApp(newConfig, true)
await Entries.writeImports(map)
logger.info('Creating boilerplate files')
await Entries.writeApp(newConfig, true)
await Entries.writeImports(await entries.get())

logger.info(`Setup entries socket 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)
}
})
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)
}
}

0 comments on commit d39d6e9

Please sign in to comment.