text: "use strict";
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
      if (k2 === undefined) k2 = k;
      Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  }) : (function(o, m, k, k2) {
      if (k2 === undefined) k2 = k;
      o[k2] = m[k];
  }));
  var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
      Object.defineProperty(o, "default", { enumerable: true, value: v });
  }) : function(o, v) {
      o["default"] = v;
  });
  var __importStar = (this && this.__importStar) || function (mod) {
      if (mod && mod.__esModule) return mod;
      var result = {};
      if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
      __setModuleDefault(result, mod);
      return result;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.WebpackServePlugin = void 0;
  const path = __importStar(require("path"));
  const debug_certificate_manager_1 = require("@rushstack/debug-certificate-manager");
  const ConfigureServe_1 = require("./ConfigureServe");
  const InfoPlugin_1 = require("./webpackPlugins/InfoPlugin");
  const MetricsWebpackPlugin_1 = require("./webpackPlugins/MetricsWebpackPlugin");
  /**
   * @public
   */
  class WebpackServePlugin {
      constructor() {
          this.pluginName = "spfx-webpack-serve-plugin" /* SpfxWebpackServePlugin */;
      }
      /**
       * Generate the Webpack Dev Server configuration for SPFx component development
       */
      apply(heftSession, heftConfiguration) {
          const logger = heftSession.requestScopedLogger('spfx-webpack-serve-plugin');
          const terminal = logger.terminal;
          const { buildFolder } = heftConfiguration;
          heftSession.hooks.build.tap("spfx-webpack-serve-plugin" /* SpfxWebpackServePlugin */, (build) => {
              if (build.properties.serveMode) {
                  build.hooks.bundle.tap("spfx-webpack-serve-plugin" /* SpfxWebpackServePlugin */, (bundle) => {
                      bundle.hooks.configureWebpack.tap("spfx-webpack-serve-plugin" /* SpfxWebpackServePlugin */, (config) => {
                          if (!config) {
                              throw new Error('Serve mode is expecting a webpack config to use webpack dev server.');
                          }
                          // Webpack Dev Server will only consider the first config if multiple are defined.
                          if (Array.isArray(config)) {
                              config = config[0];
                          }
                          const serve = new ConfigureServe_1.ConfigureServe(heftConfiguration, terminal);
                          const serveConfig = serve.generateServeConfig();
                          const certificateManager = new debug_certificate_manager_1.CertificateManager();
                          const certificate = certificateManager.ensureCertificate(true, terminal);
                          const metricsCollector = heftSession.metricsCollector;
                          // Webpack Dev Server config
                          config.devServer = {
                              contentBase: path.resolve(buildFolder),
                              publicPath: '/dist',
                              port: serveConfig.port,
                              https: {
                                  key: certificate.pemKey,
                                  cert: certificate.pemCertificate
                              },
                              hot: true,
                              historyApiFallback: false,
                              compress: true,
                              disableHostCheck: true,
                              headers: {
                                  'Access-Control-Allow-Origin': '*',
                                  'Access-Control-Allow-Methods': 'HEAD, GET, OPTIONS'
                              },
                              stats: {
                                  assets: false,
                                  chunks: false,
                                  modules: false,
                                  warningsFilter: [/export .* was not found in/]
                              },
                              before: (app, server, compiler) => {
                                  if (serveConfig.apiMap) {
                                      for (const [route, handler] of Object.entries(serveConfig.apiMap)) {
                                          // Don't include universal route handlers.
                                          if (route.startsWith('*')) {
                                              continue;
                                          }
                                          app.get(route, handler);
                                      }
                                  }
                                  app.use(serve.logRequestsMiddleware());
                              },
                              open: !!serveConfig.initialPage,
                              openPage: serveConfig.initialPage,
                              writeToDisk: true
                          };
                          // Webpack Dev Server plugins
                          config.plugins = [
                              ...(config.plugins ? config.plugins : []),
                              new InfoPlugin_1.InfoPlugin({
                                  terminal: terminal,
                                  port: serveConfig.port,
                                  tempFolderName: 'temp'
                              }),
                              new MetricsWebpackPlugin_1.MetricsWebpackPlugin(metricsCollector)
                          ];
                          return config;
                      });
                  });
              }
          });
      }
  }
  exports.WebpackServePlugin = WebpackServePlugin;
  exports.default = new WebpackServePlugin(); // tslint:disable-line:export-name
  //# sourceMappingURL=WebpackServePlugin.js.map