# All files / src/webpack/utils normalizeAssets.js

100% Statements 43/43
91.67% Branches 22/24
100% Functions 9/9
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    773x

    3x
    3x
    3x
    3x

    3x
    1x

    3x
    2x

    3x

    3x

    3x

    3x

    7x
    2x
    3x

    5x
    2x

    2x
    1x
    2x

    1x
    1x

    3x
    3x

    3x
    21x
    21x
    21x
    24x

    21x
    21x

    21x

    3x
    3x
    46x

    3x

    3x

    3x

    3x

     const _ = require("lodash");

    module.exports = function(webpackStats) {
      let assets = {};
      let cssDependencyMap = [];
      webpackStats = webpackStats.toJson();

      if (webpackStats.children && webpackStats.children.length === 1) {
        webpackStats = webpackStats.children;
      }

      if (!_.isArray(webpackStats)) {
        webpackStats = [webpackStats];
      }

      _.each(webpackStats, stat => {

        const assetsByChunkName = stat.assetsByChunkName;

        const publicPath = stat.publicPath;

        _.each(assetsByChunkName, (chunkValue, chunkName) => {

          // If its array then it just contains chunk value as array
          if (_.isArray(chunkValue)) {
            _.each(chunkValue, (path, index) => {
              assetsByChunkName[chunkName][index] = `${publicPath}${path}`;
            });
          } else if (_.isObject(chunkValue)) {
            _.each(chunkValue, (subChunkValues, subChunkType) => {

              if (_.isArray(subChunkValues) || _.isObject(subChunkValues)) {
                _.each(subChunkValues, (subChunkValue, subChunkIndex) => {
                  assetsByChunkName[chunkName][subChunkType][subChunkIndex] = `${publicPath}${subChunkValue}`;
                });
              } else Eif (_.isString(subChunkValues)) {
                assetsByChunkName[chunkName][subChunkType] = `${publicPath}${subChunkValues}`;
              }

            });
          } else Eif (_.isString(chunkValue)) {
            assetsByChunkName[chunkName] = `${publicPath}${chunkValue}`;
          }
        });

        _.each(stat.chunks, chunk => {
          let hasCSS = false;
          let cssFileName = "";
          _.each(chunk.files, f => {
            if (hasCSS) return;

            hasCSS = f.indexOf(".css") !== -1;
            if (hasCSS) cssFileName = f;
          });

          if (!hasCSS) return;

          let moduleReasons = [];
          _.each(chunk.modules, m => {
            moduleReasons = moduleReasons.concat(_.map(m.reasons, "userRequest"));
          });
          moduleReasons = _.uniq(_.compact(moduleReasons));

          cssDependencyMap.push({
            path: `${publicPath}${cssFileName}`,
            modules:  moduleReasons
          });

        });
        assets = {...assetsByChunkName, cssDependencyMap};
      });

      return assets;
    };