"use strict";

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

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

var _uglifycss = _interopRequireDefault(require("uglifycss"));

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

class MinifierPlugin {
  apply(compiler) {
    compiler.hooks.emit.tap('MinifierPlugin', compilation => {
      Object.keys(compilation.assets).forEach(filename => {
        if (/\.css$/.test(filename)) {
          try {
            let asset = compilation.assets[filename].source();

            if (typeof asset !== 'string') {
              asset = asset.toString();
            }

            const result = _uglifycss.default.processString(asset);

            compilation.assets[filename] = new _webpackSources.RawSource(result);
          } catch (e) {
            compilation.errors.push(e);
          }
        }
      });
    });
  }

}

exports.default = MinifierPlugin;