File: index.js

package info (click to toggle)
node-stats-webpack-plugin 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 252 kB
  • sloc: makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,447 bytes parent folder | download
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
var _ = require('lodash')

/**
 * Create a new StatsPlugin that causes webpack to generate a stats file as
 * part of the emitted assets.
 * @constructor
 * @param {String} output Path to output file.
 * @param {Object} options Options passed to the stats' `.toJson()`.
 */

function StatsPlugin (output, options, cache) {
  this.output = output
  this.options = options
  this.cache = cache
}

function onEmit (output, options, cache) {
  return function (compilation, done) {
    var result

    compilation.assets[output] = {
      size: function getSize () {
        return result ? result.length : 0
      },
      source: function getSource () {
        var stats = compilation.getStats().toJson(options)
        var result

        if (cache) {
          cache = _.merge(cache, stats)
          if (stats.errors) cache.errors = stats.errors
          if (stats.warnings) cache.warnings = stats.warnings
          result = JSON.stringify(cache)
        } else {
          result = JSON.stringify(stats)
        }
        return result
      }
    }
    done()
  }
}

StatsPlugin.prototype.apply = function apply (compiler) {
  var output = this.output
  var options = this.options
  var cache = this.cache

  var onEmitCallback = onEmit(output, options, cache)

  if (compiler.hooks) {
    compiler.hooks.emit.tapAsync('StatsPlugin', onEmitCallback)
  } else {
    compiler.plugin('emit', onEmitCallback)
  }
}

module.exports = StatsPlugin