|
| 1 | +var path = require('path') |
| 2 | +var crypto = require('crypto') |
| 3 | +var through = require('through2') |
| 4 | + |
| 5 | +module.exports = function webpackStats (b) { |
| 6 | + var stats = null |
| 7 | + var startTime |
| 8 | + var chunkId |
| 9 | + function init () { |
| 10 | + stats = { |
| 11 | + errors: [], |
| 12 | + warnings: [], |
| 13 | + version: '1.0.0', |
| 14 | + publicPath: '', |
| 15 | + assets: [], |
| 16 | + entrypoints: {}, |
| 17 | + chunks: [], |
| 18 | + modules: [] |
| 19 | + } |
| 20 | + startTime = Date.now() |
| 21 | + chunkId = 0 |
| 22 | + } |
| 23 | + |
| 24 | + b.on('reset', addHooks) |
| 25 | + b.on('split.pipeline', addChunk) |
| 26 | + b.on('factor.pipeline', addChunk) |
| 27 | + addHooks() |
| 28 | + |
| 29 | + function addHooks () { |
| 30 | + init() |
| 31 | + |
| 32 | + addChunk('bundle.js', b.pipeline) |
| 33 | + b.pipeline.get('wrap').on('end', function () { |
| 34 | + require('fs').writeFile('stats.json', JSON.stringify(stats, null, 2), function () {}) |
| 35 | + }) |
| 36 | + } |
| 37 | + function addChunk (name, pipeline) { |
| 38 | + var totalSize = 0 |
| 39 | + var chunk = { |
| 40 | + id: chunkId++, |
| 41 | + rendered: true, |
| 42 | + initial: true, |
| 43 | + entry: true, |
| 44 | + extraAsync: false, |
| 45 | + size: 0, |
| 46 | + names: [ name.replace(/\.[a-z]*$/, '') ], |
| 47 | + files: [], |
| 48 | + hash: '', |
| 49 | + modules: [], |
| 50 | + } |
| 51 | + var hash = crypto.createHash('sha512') |
| 52 | + pipeline.get('pack').unshift(through.obj(onmodule, onmodulesend)) |
| 53 | + pipeline.get('wrap').push(through(ondata, onchunkend)) |
| 54 | + |
| 55 | + function onmodule (row, enc, cb) { |
| 56 | + var relative = path.relative(b._options.basedir || process.cwd(), row.file) |
| 57 | + var match = /^(\.\.\/)+(.*?)\/browserify\/(lib|node_modules)\//.exec(relative) |
| 58 | + if (match) { |
| 59 | + relative = '(browserify)/' + match[3] + '/' + relative.slice(match[0].length) |
| 60 | + } |
| 61 | + if (!relative.includes('(browserify') && !relative.includes('node_modules/')) { |
| 62 | + chunk.files.push(relative) |
| 63 | + } |
| 64 | + if (!/^\.\.?\//.test(relative) && !relative.startsWith('(browserify)') && !path.isAbsolute(relative)) { |
| 65 | + relative = './' + relative |
| 66 | + } |
| 67 | + var module = { |
| 68 | + id: row.id, |
| 69 | + identifier: row.file, |
| 70 | + name: relative, |
| 71 | + chunks: [chunk.id], |
| 72 | + chunkNames: chunk.names, |
| 73 | + source: row.source, |
| 74 | + size: Math.max(1, row.source.length) |
| 75 | + } |
| 76 | + chunk.modules.push(module) |
| 77 | + stats.modules.push(module) |
| 78 | + cb(null, row) |
| 79 | + } |
| 80 | + |
| 81 | + function onmodulesend (done) { |
| 82 | + chunk.size = chunk.modules.reduce(function (s, r) { return s + r.size }, 0) |
| 83 | + done() |
| 84 | + } |
| 85 | + |
| 86 | + function ondata (chunk, enc, cb) { |
| 87 | + hash.update(chunk) |
| 88 | + totalSize += chunk.length |
| 89 | + cb(null, chunk) |
| 90 | + } |
| 91 | + function onchunkend (done) { |
| 92 | + chunk.hash = hash.digest('hex') |
| 93 | + stats.chunks.push(chunk) |
| 94 | + stats.assets.push({ |
| 95 | + name: name, |
| 96 | + size: totalSize, |
| 97 | + chunks: [chunk.id], |
| 98 | + chunkNames: chunk.names, |
| 99 | + emitted: true |
| 100 | + }) |
| 101 | + done() |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments