<commit_before>import { join } from "path"
import express, { Router } from "express"
import webpack from "webpack"
import webpackDevMiddleware from "webpack-dev-middleware"
import webpackHotMiddleware from "webpack-hot-middleware"
import historyFallbackMiddleware from "connect-history-api-fallback"
import WebpackNotifierPlugin from "webpack-notifier"

import opn from "opn"
import debug from "debug"
import portFinder from "portfinder"

import minifyCollection from "../phenomic-loader/minify"
import serialize from "../_utils/serialize"

import collection from "../phenomic-loader/cache.js"
import pathToUri from "../_utils/path-to-uri"

const log = debug("phenomic:builder:server")

export default (config) => {
  const { webpackConfigBrowser: webpackConfig } = config

  if (!config.baseUrl) {
    throw new Error(
      "You must provide a 'baseUrl' object that contains the following keys:" +
      "'href', 'port', 'hostname'. See https://nodejs.org/api/url.html"
    )
  }

  const server = express()

  if (config.static && config.server) {
    server.use(
      config.baseUrl.pathname,
      express.static(join(config.cwd, config.destination))
    )
  }
  else {
    const devEntries = [
      require.resolve("webpack-hot-middleware/client"),
    ]

    const devConfig = {
      ...webpackConfig,
      // debug: true,
      // watch: true,
      // colors: true,
      entry: {
        // add devEntries
        ...Object.keys(webpackConfig.entry)
          .reduce((acc, key) => {
            // some entries do not need extra stuff
            acc[key] = [
              ...devEntries,
              ...Array.isArray(webpackConfig.entry[key])
                ? webpackConfig.entry[key]
                : [ webpackConfig.entry[key] ],
            ]
            return acc
          },
          {}
        ),
      },
      plugins: [
        ...(webpackConfig.plugins || []),
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new WebpackNotifierPlugin(),
      ],
      eslint: {
        ...webpackConfig.eslint,
        emitWarning: true,
      },
    }

    // webpack requirements
    const webpackCompiler = webpack(devConfig)

    server.use(webpackDevMiddleware(webpackCompiler, {
      publicPath: webpackConfig.output.publicPath,
      noInfo: !config.verbose,
      ...devConfig.devServer,
    }))

    // HMR
    server.use(webpackHotMiddleware(webpackCompiler))<commit_msg>Minor dev server adjustements<commit_after>import { join } from "path"
import express, { Router } from "express"
import webpack from "webpack"
import webpackDevMiddleware from "webpack-dev-middleware"
import webpackHotMiddleware from "webpack-hot-middleware"
import historyFallbackMiddleware from "connect-history-api-fallback"
import WebpackNotifierPlugin from "webpack-notifier"

import opn from "opn"
import debug from "debug"
import portFinder from "portfinder"

import minifyCollection from "../phenomic-loader/minify"
import serialize from "../_utils/serialize"

import collection from "../phenomic-loader/cache.js"
import pathToUri from "../_utils/path-to-uri"

const log = debug("phenomic:builder:server")

export default (config) => {
  const { webpackConfigBrowser: webpackConfig } = config

  if (!config.baseUrl) {
    throw new Error(
      "You must provide a 'baseUrl' object that contains the following keys:" +
      "'href', 'port', 'hostname'. See https://nodejs.org/api/url.html"
    )
  }

  const server = express()

  if (config.static && config.server) {
    server.use(
      config.baseUrl.pathname,
      express.static(join(config.cwd, config.destination))
    )
  }
  else {
    const devEntries = [
      require.resolve("webpack-hot-middleware/client"),
    ]

    const devConfig = {
      ...webpackConfig,
      entry: {
        // add devEntries
        ...Object.keys(webpackConfig.entry).reduce((acc, key) => ({
          ...acc,
          [key]: [
            ...devEntries,
            ...Array.isArray(webpackConfig.entry[key])
              ? webpackConfig.entry[key]
              : [ webpackConfig.entry[key] ],
          ],
        }), {}),
      },
      plugins: [
        ...(webpackConfig.plugins || []),

        // for hot-middleware
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),

        new WebpackNotifierPlugin(),
      ],
      eslint: {
        ...webpackConfig.eslint,
        emitWarning: true,
      },
    }

    // webpack dev + hot middlewares
    const webpackCompiler = webpack(devConfig)
    server.use(webpackDevMiddleware(webpackCompiler, {
      publicPath: webpackConfig.output.publicPath,
      noInfo: !config.verbose,
      ...devConfig.devServer,
    }))
    server.use(webpackHotMiddleware(webpackCompiler))
{'fraction_non_alphanumeric': 0.12318097970895675, 'fraction_numerical': 0.0, 'mean_word_length': 2.585598824393828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7422568', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1394, 'n_words': 306}