text: /*
   * Copyright (C) 2020 Dremio
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  "use strict";
  
  const fs = require("fs");
  const errorOverlayMiddleware = require("react-dev-utils/errorOverlayMiddleware");
  const evalSourceMapMiddleware = require("react-dev-utils/evalSourceMapMiddleware");
  const noopServiceWorkerMiddleware = require("react-dev-utils/noopServiceWorkerMiddleware");
  const ignoredFiles = require("react-dev-utils/ignoredFiles");
  const redirectServedPath = require("react-dev-utils/redirectServedPathMiddleware");
  const paths = require("./paths");
  const getHttpsConfig = require("./getHttpsConfig");
  
  const sockHost = process.env.WDS_SOCKET_HOST;
  const sockPath = process.env.WDS_SOCKET_PATH; // default: '/sockjs-node'
  const sockPort = process.env.WDS_SOCKET_PORT;
  
  module.exports = function (proxy, allowedHost, host, port) {
    return {
      allowedHosts: "auto",
      // Enable gzip compression of generated files.
      compress: true,
      // Silence WebpackDevServer's own logs since they're generally not useful.
      // It will still show compile warnings and errors with this setting.
      client: {
        logging: "none",
        overlay: false,
        webSocketURL: {
          hostname: sockHost,
          pathname: sockPath,
          port: sockPort
        },
      },
      // By default WebpackDevServer serves physical files from current directory
      // in addition to all the virtual build products that it serves from memory.
      // This is confusing because those files won’t automatically be available in
      // production build folder unless we copy them. However, copying the whole
      // project directory is dangerous because we may expose sensitive files.
      // Instead, we establish a convention that only files in `public` directory
      // get served. Our build script will copy `public` into the `build` folder.
      // In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
      // <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
      // In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
      // Note that we only recommend to use `public` folder as an escape hatch
      // for files like `favicon.ico`, `manifest.json`, and libraries that are
      // for some reason broken when imported through webpack. If you just want to
      // use an image, put it in `src` and `import` it from JavaScript instead.
      static: {
        directory: paths.appPublic,
        staticOptions: {},
        publicPath: paths.publicUrlOrPath,
        watch: true,
        watch: {
          ignored: ignoredFiles(paths.appSrc)
        }
      },
  
      // enable hot module replacement
      hot: true,
      // Use 'ws' instead of 'sockjs-node' on server since we're using native
      // websockets in `webpackHotDevClient`.
      webSocketServer: "ws",
      // Prevent a WS client from getting injected as we're already including
      // `webpackHotDevClient`.
      client: false,
      // It is important to tell WebpackDevServer to use the same "publicPath" path as
      // we specified in the webpack config. When homepage is '.', default to serving
      // from the root.
      // remove last slash so user can land on `/test` instead of `/test/`
      devMiddleware: {
        publicPath: paths.publicUrlOrPath.slice(0, -1)
      },
      https: getHttpsConfig(),
      host: host,
      port: port,
      historyApiFallback: {
        // Paths with dots should still use the history fallback.
        // See https://github.com/facebook/create-react-app/issues/387.
        disableDotRule: true,
        index: paths.publicUrlOrPath,
      },
      // `proxy` is run between `before` and `after` `webpack-dev-server` hooks
      proxy,
  
      onBeforeSetupMiddleware: function (devServer) {
        // Keep `evalSourceMapMiddleware` and `errorOverlayMiddleware`
        // middlewares before `redirectServedPath` otherwise will not have any effect
        // This lets us fetch source contents from webpack for the error overlay
        devServer.app.use(evalSourceMapMiddleware(devServer));
        // This lets us open files from the runtime error overlay.
        devServer.app.use(errorOverlayMiddleware());
        if (fs.existsSync(paths.proxySetup)) {
          // This registers user provided middleware for proxy reasons
          require(paths.proxySetup)(app);
        }
      },
  
      onAfterSetupMiddleware: function (devServer) {
        // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
        devServer.app.use(redirectServedPath(paths.publicUrlOrPath));
        // This service worker file is effectively a 'no-op' that will reset any
        // previous service worker registered for the same host:port combination.
        // We do this in development to avoid hitting the production cache if
        // it used the same host and port.
        // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
        devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
      },
    };
  };
  