Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Add support for multi compile mode (#14)
Browse files Browse the repository at this point in the history
Add support for multi compile mode
  • Loading branch information
Patrick Corbett authored and unindented committed Mar 2, 2017
1 parent e7891b2 commit 91fb22f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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) {
function StatsPlugin (output, options, cache) {
this.output = output
this.options = options
this.cache = cache || {}
}

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

compiler.plugin('emit', function onEmit (compilation, done) {
var result
Expand All @@ -22,7 +26,9 @@ StatsPlugin.prototype.apply = function apply (compiler) {
return result && result.length || 0
},
source: function getSource () {
result = JSON.stringify(compilation.getStats().toJson(options))
var stats = compilation.getStats().toJson(options)
cache = _.merge(cache, stats)
result = JSON.stringify(cache)
return result
}
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"node-libs-browser": "^2.0.0",
"rimraf": "^2.5.4",
"webpack": "^1.14.0"
},
"dependencies": {
"lodash": "^4.17.4"
}
}
51 changes: 51 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ var defaultCompilerOptions = {
]
}

var multiCompilerOptions = (cache) => [{
entry: {
file1: inputFile
},

output: {
path: outputFolder,
filename: 'bundle1.js'
},

profile: true,

plugins: [
new StatsPlugin('stats.json', options, cache)
]
}, {
entry: {
file2: inputFile
},

output: {
path: outputFolder,
filename: 'bundle2.js'
},

profile: true,

plugins: [
new StatsPlugin('stats.json', options, cache)
]
}]

describe('StatsWebpackPlugin', function () {
beforeEach(function () {
rimraf.sync(outputFolder)
Expand Down Expand Up @@ -63,4 +95,23 @@ describe('StatsWebpackPlugin', function () {
done()
})
})

it('supports multi-compile mode and outputs one `stats.json` file', function (done) {
var cache = {}
var compiler = webpack(multiCompilerOptions(cache))
compiler.run(function (err, stats) {
if (err) {
return done(err)
}

var actual = JSON.parse(fs.readFileSync(outputFile, 'utf8'))

var expectedAssetsByChunkName = {
file1: 'bundle1.js',
file2: 'bundle2.js'
}
expect(actual.assetsByChunkName).to.deep.equal(expectedAssetsByChunkName)
done()
})
})
})

0 comments on commit 91fb22f

Please sign in to comment.