import webpack from 'webpack';
import webpackConfig from '../webpack.config';

class Server extends Task {
  constructor() {
    super('serve');
  }
  get deps() {
    let tasks = [this.getStyleTask(), 'scripts', 'fonts'];

    if (!config.static) {
      tasks.push('images');
    }

    return tasks;
  }
  get fn() {
    return () => {
      let middleware;

      if (config.scripts.HMR) {
        let compiler = webpack(webpackConfig());

        middleware = [
          require('webpack-dev-middleware')(compiler, {
            noInfo: true,
            publicPath: config.scripts.publicPath
          }),
          require('webpack-hot-middleware')(compiler),
          // proxy api requests
          ...(Object.keys(config.server.proxyTable).map(context => {
            let options = config.server.proxyTable[context];
            if (typeof options === 'string') {
              options = { target: options };
            }
            require('http-proxy-middleware')(context, options);
          }))
        ];
      }

      let options = {
        files: [config.source.js + '/**/*'],
        notify: false,
        localOnly: config.server.localOnly
      };
      if (config.server.proxy) {
        options.proxy = {
          target: config.server.proxy,
          middleware: middleware || undefined
        };
      } else {
        options.host = config.server.host;
        options.port = config.server.port;
        options.server = {
          baseDir: [config.tmp.base, config.source.base],
          routes: {
            '/bower_components': 'bower_components'
          },
          middleware: middleware || false
        };
      }

      browserSync(options);

      gulp.watch([
        config.source.html + '/*.{html,php}',
        config.source.img + '/**/*',
        config.tmp.font + '/**/*'
      ]).on('change', reload);

      gulp.watch(config.source.css + '/**/*', [this.getStyleTask()]);
      gulp.watch(config.source.font + '/**/*', ['fonts']);
      gulp.watch(config.workspace + '/bower.json', ['wiredep', 'fonts']);
      if (!config.scripts.HMR) {
        gulp.watch(config.source.js + '/**/*', ['webpack']);
      }
    };
  }
}

export default Server;

@@ -1,20 +1,63 @@
+import proxy from 'http-proxy-middleware';%0A
 import webpack from 
@@ -401,43 +401,8 @@
 re;%0A
-%0A      if (config.scripts.HMR) %7B%0A  
     
@@ -441,24 +441,56 @@
 Config());%0A%0A
+      if (!config.production) %7B%0A
         midd
@@ -650,63 +650,8 @@
 %7D),%0A
-          require('webpack-hot-middleware')(compiler),%0A
     
@@ -932,38 +932,18 @@
   re
-quire('http-proxy-middleware')
+turn proxy
 (con
@@ -995,20 +995,132 @@
   %7D%0A
-%0A      let o
+      if (config.scripts.HMR) %7B%0A        middleware.push(require('webpack-hot-middleware')(compiler));%0A      %7D%0A%0A      let bsO
 ptio
@@ -1279,33 +1279,35 @@
 roxy) %7B%0A        
-o
+bsO
 ptions.proxy = %7B
@@ -1422,25 +1422,27 @@
 e %7B%0A        
-o
+bsO
 ptions.host 
@@ -1467,25 +1467,27 @@
 st;%0A        
-o
+bsO
 ptions.port 
@@ -1512,25 +1512,27 @@
 rt;%0A        
-o
+bsO
 ptions.serve
@@ -1760,17 +1760,19 @@
 serSync(
-o
+bsO
 ptions);

update server