module.exports = function fn(opts) { const defaults = { dev: {}, hot: {} }; const options = Object.assign(defaults, opts); let { compiler, config } = options; if (!compiler) { if (!config) { // eslint-disable-next-line import/no-dynamic-require, global-require config = require(path.join(root.path, 'webpack.config.js')); } compiler = Webpack(config); } if (!options.dev.publicPath) { const { publicPath } = compiler.options.output; if (!publicPath) { throw new Error('koa-webpack: publicPath must be set on `dev` options, or in a compiler\'s `output` configuration.'); } options.dev.publicPath = publicPath; } const client = options.hot ? hotClient(compiler, options.hot) : null; const dev = devMiddleware(compiler, options.dev); return Object.assign(koaDevware(dev, compiler), { dev, client, close(callback) { const next = client ? () => { client.close(callback); } : callback; dev.close(next); } }); };
return new Promise((resolve) => { const client = hotClient(compiler, options.hotClient); const { server } = client; server.on('listening', () => resolve(client)); });