import webpack from 'webpack'
import { logError, log } from '../util/log'
import Server from 'webpack-dev-server'

/**
 * Development server
 */
export default (saguiConfig, webpackConfig) => new Promise((resolve, reject) => {
  const options = {
    hot: true,
    historyApiFallback: saguiConfig.pages && saguiConfig.pages[0] && `${saguiConfig.pages[0]}.html`,
    ...saguiConfig.develop
  }

  try {
    new Server(webpack(setupHMR(webpackConfig)), options).listen(saguiConfig.port, '0.0.0.0', (err) => {
      if (err) {
        logError(`Server failed to started at http://localhost:${saguiConfig.port}`)
        reject(err)
      } else {
        log(`Server started at http://localhost:${saguiConfig.port}/webpack-dev-server/`)
      }
    })
  } catch (e) {
    reject(e)
  }
})

/**
 * HMR bundle setup based on code from
 * https://github.com/webpack/webpack-dev-server/blob/master/bin/webpack-dev-server.js
 */
function setupHMR (webpackConfig) {
  return webpackConfig.map((webpack) => ({
    ...webpack,
    entry: concatHMRBundle(webpack.entry)
  }))
}

function concatHMRBundle (entry) {
  const devClient = [
    require.resolve('webpack-dev-server/client/') + '?/',
    'webpack/hot/dev-server'
  ]

  if (typeof entry === 'object' && !Array.isArray(entry)) {
    return Object.keys(entry).reduce((entries, key) => ({
      ...entries,
      [key]: devClient.concat(entry[key])
    }), {})
  } else {
    return devClient.concat(entry)
  }
}

@@ -263,103 +263,20 @@
     
-historyApiFallback: saguiConfig.pages && saguiConfig.pages%5B0%5D && %60$%7BsaguiConfig.pages%5B0%5D%7D.html%60
+noInfo: true
 ,%0A  
@@ -316,16 +316,31 @@
 ry %7B%0A   
+ const server =
  new Ser
@@ -385,16 +385,292 @@
 options)
+%0A%0A    server.listeningApp.on('error', function (e) %7B%0A      if (e.code === 'EADDRINUSE') %7B%0A        logError(%60Server failed to started at http://localhost:$%7BsaguiConfig.port%7D. Port already used.%60)%0A        reject(new Error('This port is already used'))%0A      %7D%0A    %7D)%0A%0A    server
 .listen(
@@ -963,24 +963,91 @@
 catch (e) %7B%0A
+    logError('Server failed to start.')%0A    logError(e.stack %7C%7C e)%0A
     reject(e

Fix development server not starting up after Webpack 2 update 😭