"use strict";

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

var _webpack = require("webpack");

/* eslint-disable class-methods-use-this */
class SourceMapPlugin {
  apply(compiler) {
    const {
      RawSource
    } = compiler.webpack.sources;
    compiler.hooks.thisCompilation.tap('splitSourceMap', compilation => {
      compilation.hooks.processAssets.tap({
        name: 'splitSourceMap',
        stage: _webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
      }, assets => {
        Object.keys(assets).forEach(assetName => {
          const assetCode = assets[assetName].source();

          if (/\.js$/.test(assetName) && !/\.i18n\.js$/.test(assetName) || /\.js\.map$/.test(assetName) && !/\.i18n\.js\.map$/.test(assetName)) {
            compilation.renameAsset(assetName, `smap/${assetName}`);

            if (!/\.map$/.test(assetName)) {
              compilation.emitAsset(assetName, new RawSource(assetCode));
            }
          }
        });
      });
    });
  }

}

exports.SourceMapPlugin = SourceMapPlugin;