const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const history = require('connect-history-api-fallback');


module.exports.chisel = function (extra_config) {
  let config_path = extra_config.dev == null || extra_config.dev == false ? './webpack.prod.config' : './webpack.dev.config';
  const config = require(config_path);

  const port = process.env.PORT || 1337;

  const server = new express();

  server.use(history());

  const compiler = webpack(config);
  server.use(webpackDevMiddleware(compiler, {
    publicPath: extra_config.path || config.output.publicPath,
    stats: {
      colors: true,
      chunks: false
    }
  }));
  server.use(webpackHotMiddleware(compiler));

  // serve pure static assets
  //server.use('/', express.static('./static'));
  return server;
}
