Skip to content

Commit e896d14

Browse files
committed
MultiCompiler with compilers depend on other
1 parent 530fad4 commit e896d14

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

lib/MultiCompiler.js

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,89 @@ module.exports = MultiCompiler;
9090
MultiCompiler.prototype = Object.create(Tapable.prototype);
9191
MultiCompiler.prototype.constructor = MultiCompiler;
9292

93-
MultiCompiler.prototype.watch = function(watchDelay, handler) {
94-
var watchings = this.compilers.map(function(compiler) {
95-
return compiler.watch(watchDelay, handler);
93+
function runWithDependencies(compilers, fn, callback) {
94+
var fulfilledNames = {};
95+
var remainingCompilers = compilers;
96+
97+
function isDependencyFulfilled(d) {
98+
return fulfilledNames[d];
99+
}
100+
101+
function getReadyCompilers() {
102+
var readyCompilers = [];
103+
var list = remainingCompilers;
104+
remainingCompilers = [];
105+
for(var i = 0; i < list.length; i++) {
106+
var c = list[i];
107+
var ready = !c.dependencies || c.dependencies.every(isDependencyFulfilled);
108+
if(ready)
109+
readyCompilers.push(c);
110+
else
111+
remainingCompilers.push(c);
112+
}
113+
return readyCompilers;
114+
}
115+
116+
function runCompilers(callback) {
117+
if(remainingCompilers.length === 0) return callback();
118+
async.map(getReadyCompilers(), function(compiler, callback) {
119+
fn(compiler, function(err) {
120+
if(err) return callback(err);
121+
fulfilledNames[compiler.name] = true;
122+
runCompilers(callback);
123+
});
124+
}, callback);
125+
}
126+
runCompilers(callback);
127+
}
128+
129+
MultiCompiler.prototype.watch = function(watchOptions, handler) {
130+
var watchings = [];
131+
var allStats = [];
132+
var compilerStatus = this.compilers.map(function() {
133+
return false;
96134
});
135+
136+
runWithDependencies(this.compilers, function(compiler, callback) {
137+
var compilerIdx = this.compilers.indexOf(compiler);
138+
var firstRun = true;
139+
var watching = compiler.watch(watchOptions, function(err, stats) {
140+
if(err)
141+
handler(err);
142+
if(stats) {
143+
allStats.push(stats);
144+
compilerStatus[compilerIdx] = true;
145+
if(compilerStatus.every(Boolean)) {
146+
var multiStats = new MultiStats(allStats);
147+
allStats = [];
148+
handler(null, multiStats)
149+
}
150+
}
151+
if(firstRun && !err) {
152+
firstRun = false;
153+
callback();
154+
}
155+
});
156+
watchings.push(watching);
157+
}.bind(this), function() {
158+
// ignore
159+
});
160+
97161
return new MultiWatching(watchings);
98162
};
99163

100164
MultiCompiler.prototype.run = function(callback) {
101-
async.map(this.compilers, function(compiler, callback) {
102-
compiler.run(callback);
103-
}, function(err, stats) {
165+
var allStats = [];
166+
167+
runWithDependencies(this.compilers, function(compiler, callback) {
168+
compiler.run(function(err, stats) {
169+
if(err) return callback(err);
170+
allStats.push(stats);
171+
callback();
172+
});
173+
}, function(err) {
104174
if(err) return callback(err);
105-
callback(null, new MultiStats(stats));
175+
callback(null, new MultiStats(allStats));
106176
});
107177
};
108178

lib/WebpackOptionsApply.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
6262
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
6363
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
6464
compiler.name = options.name;
65+
compiler.dependencies = options.dependencies;
6566
if(typeof options.target === "string") {
6667
var JsonpTemplatePlugin;
6768
var NodeSourcePlugin;

0 commit comments

Comments
 (0)