"use strict";

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

var _optimizeJs = _interopRequireDefault(require("optimize-js"));

var _webpackSources = require("webpack-sources");

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

class OptimizeJSPlugin {
  constructor(options = {}) {
    this.optimize = options.optimize;
  }

  apply(compiler) {
    compiler.hooks.compilation.tap('OptimizeJSPlugin', compilation => {
      compilation.hooks.afterOptimizeChunks.tap('OptimizeJSPlugin', chunks => {
        chunks.forEach(chunk => {
          const files = chunk.files.map(file => file);
          files.filter(file => /\.js$/.test(file)).forEach(file => {
            try {
              const asset = compilation.assets[file].source();
              const result = (0, _optimizeJs.default)(asset);
              compilation.assets[file] = new _webpackSources.RawSource(result);
            } catch (e) {
              compilation.errors.push(e);
            }
          });
        });
      });
    });
  }

}

var _default = OptimizeJSPlugin;
exports.default = _default;