Skip to content

Commit

Permalink
fix: use multiple instances at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Feb 28, 2018
1 parent 51fb5a4 commit a1c7f4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
45 changes: 35 additions & 10 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var path = require('path');
var fse = require('fs-extra');
var _ = require('lodash');
var mutexify = require('mutexify');

var lock = mutexify();
var manifestMap = {};

function ManifestPlugin(opts) {
this.opts = _.assign({
Expand Down Expand Up @@ -105,7 +104,11 @@ ManifestPlugin.prototype.apply = function(compiler) {

files = files.filter(function (file) {
// Don't add hot updates to manifest
return file.path.indexOf('hot-update') === -1;
var isUpdateChunk = file.path.indexOf('hot-update') >= 0;
// Don't add manifest from another instance
var isManifest = manifestMap[file.name];

return !isUpdateChunk && !isManifest;
});

// Append optional basepath onto all references.
Expand Down Expand Up @@ -174,21 +177,43 @@ ManifestPlugin.prototype.apply = function(compiler) {
fse.outputFileSync(outputFile, output);
}

// NOTE: make sure webpack is not writing multiple manifests simultaneously
lock(function(release) {
if (!manifestMap[outputName]) {
manifestMap[outputName] = {
running: false,
queue: []
};
}

function unqueueNext() {
if (manifestMap[outputName].queue.length > 0) {
manifestMap[outputName].running = true;
manifestMap[outputName].queue.shift()();
} else {
manifestMap[outputName].running = false;
}
}


manifestMap[outputName].queue.push(function () {
if (compiler.hooks) {
compiler.hooks.afterEmit.tap('ManifestPlugin', function(compilation) {
release();
// TODO: when we deprecate webpack < 3, we can remove the queue logic
unqueueNext()
});
} else {
compiler.plugin('after-emit', function(compilation, cb) {
release();
unqueueNext();
cb();
});

compilation.applyPluginsAsync('webpack-manifest-plugin-after-emit', manifest, compileCallback);
}
});
})

if(!manifestMap[outputName].running) {
manifestMap[outputName].running = true;
manifestMap[outputName].queue.shift()();
}
}.bind(this);

if (compiler.hooks) {
Expand All @@ -204,4 +229,4 @@ ManifestPlugin.prototype.apply = function(compiler) {
}
};

module.exports = ManifestPlugin;
module.exports = ManifestPlugin;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
"homepage": "https://github.com/danethurber/webpack-manifest-plugin",
"dependencies": {
"fs-extra": "^0.30.0",
"lodash": ">=3.5 <5",
"mutexify": "1.0.1"
"lodash": ">=3.5 <5"
},
"nyc": {
"reporter": [
Expand Down

0 comments on commit a1c7f4e

Please sign in to comment.