"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.configHtmlWebpackPlugin = configHtmlWebpackPlugin;

var _constants = require("../../../constants");

var _nameTemplates = require("../common/nameTemplates");

var _modeUtils = require("../common/modeUtils");

var _InitialHtmlPlugin = require("../custom_plugins/InitialHtmlPlugin");

function configHtmlWebpackPlugin(options) {
  const {
    htmlTemplate,
    mode
  } = options;
  const {
    inject,
    templateFile
  } = htmlTemplate;
  const appInitialHTMLTemplatePath = (0, _constants.joinWithAppPath)(templateFile);
  const minifyHtmlOptions = (0, _modeUtils.isProductionMode)(mode) ? // eslint-disable-next-line no-use-before-define
  getHTMLMinifyOptions(htmlTemplate) : false;
  return new _InitialHtmlPlugin.InitialHtmlPlugin({
    mainChunkName: 'main',
    filename: (0, _nameTemplates.nameTemplates)('html', options),
    template: appInitialHTMLTemplatePath,
    minify: minifyHtmlOptions,
    inject,
    scriptLoading: 'defer'
  });
}

const defaultHTMLMiniFyOption = {
  collapseWhitespace: true,
  minifyCSS: true,
  minifyJS: true,
  keepClosingSlash: true,
  removeComments: false,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  useShortDoctype: true
};

function getHTMLMinifyOptions(htmlTemplate) {
  const {
    minify
  } = htmlTemplate;
  const minifyHtmlOptions = minify === null ? false : minify;
  return minifyHtmlOptions === true ? defaultHTMLMiniFyOption : minifyHtmlOptions;
}