"use strict";

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

var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class InitialHtmlPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    const {
      filename,
      template,
      minify,
      inject,
      mainChunkName
    } = this.options;
    new _htmlWebpackPlugin.default({
      filename,
      template,
      minify,
      chunks: [mainChunkName],
      inject,
      scriptLoading: 'defer'
    }).apply(compiler);
    compiler.hooks.thisCompilation.tap('InitialHtmlPlugin', compilation => {
      _htmlWebpackPlugin.default.getHooks(compilation).alterAssetTagGroups.tapAsync('MyPlugin', (data, cb) => {
        const headTags = [];
        const bodyTags = [];
        data.headTags.forEach(tag => {
          if (tag.tagName === 'link') {
            headTags.push(tag);
          } else {
            bodyTags.push(tag);
          }
        });
        cb(null, { ...data,
          headTags,
          bodyTags
        });
      });
    });
  }

}

exports.InitialHtmlPlugin = InitialHtmlPlugin;