Skip to content

Commit 1e37053

Browse files
committed
Sorted out some MultiCompiler issues
1 parent d447e49 commit 1e37053

6 files changed

Lines changed: 123 additions & 74 deletions

File tree

lib/CachePlugin.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,34 @@ function CachePlugin(cache) {
1010
module.exports = CachePlugin;
1111

1212
CachePlugin.prototype.apply = function(compiler) {
13-
compiler.plugin("compilation", function(compilation) {
14-
compilation.cache = this.cache;
15-
}.bind(this));
16-
compiler.plugin("run", function(compiler, callback) {
17-
if(!compiler._lastCompilationFileDependencies) return callback();
18-
var fs = compiler.inputFileSystem;
19-
fileTs = compiler.fileTimestamps = {};
20-
async.forEach(compiler._lastCompilationFileDependencies, function(file, callback) {
21-
fs.stat(file, function(err, stat) {
22-
if(err) {
23-
if(err.code === 'ENOENT') return callback();
24-
return callback(err);
25-
}
26-
27-
fileTs[file] = stat.mtime || Infinity;
28-
callback();
29-
});
30-
}, callback);
31-
}.bind(this));
32-
compiler.plugin("after-compile", function(compilation, callback) {
33-
compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies;
34-
compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies;
35-
callback();
36-
}.bind(this));
13+
if(Array.isArray(compiler.compilers)) {
14+
compiler.compilers.forEach(function(c, idx) {
15+
c.apply(new CachePlugin(this.cache[idx] = this.cache[idx] || {}));
16+
}, this);
17+
} else {
18+
compiler.plugin("compilation", function(compilation) {
19+
compilation.cache = this.cache;
20+
}.bind(this));
21+
compiler.plugin("run", function(compiler, callback) {
22+
if(!compiler._lastCompilationFileDependencies) return callback();
23+
var fs = compiler.inputFileSystem;
24+
fileTs = compiler.fileTimestamps = {};
25+
async.forEach(compiler._lastCompilationFileDependencies, function(file, callback) {
26+
fs.stat(file, function(err, stat) {
27+
if(err) {
28+
if(err.code === 'ENOENT') return callback();
29+
return callback(err);
30+
}
31+
32+
fileTs[file] = stat.mtime || Infinity;
33+
callback();
34+
});
35+
}, callback);
36+
}.bind(this));
37+
compiler.plugin("after-compile", function(compilation, callback) {
38+
compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies;
39+
compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies;
40+
callback();
41+
}.bind(this));
42+
}
3743
};

lib/MultiCompiler.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ function MultiCompiler(compilers) {
4747
}
4848
delegateProperty.call(this, "outputFileSystem");
4949
delegateProperty.call(this, "inputFileSystem");
50+
51+
Object.defineProperty(this, "outputPath", {
52+
configurable: false,
53+
get: function() {
54+
var commonPath = compilers[0].outputPath;
55+
for(var i = 1; i < compilers.length; i++) {
56+
while(compilers[i].outputPath.indexOf(commonPath) !== 0 || commonPath.length <= 1) {
57+
commonPath = commonPath.replace(/[\/\\][^\/\\]*$/, "");
58+
}
59+
}
60+
return commonPath;
61+
}
62+
});
5063

5164
var doneCompilers = 0;
5265
var compilerStats = [];

lib/ProgressPlugin.js

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,69 @@ function ProgressPlugin(handler) {
88
module.exports = ProgressPlugin;
99

1010
ProgressPlugin.prototype.apply = function(compiler) {
11-
var lastModulesCount = 0;
12-
var moduleCount = 1;
13-
var doneModules = 0;
1411
var handler = this.handler;
15-
function update() {
16-
handler(0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, doneModules + "/" + moduleCount + " build modules");
17-
}
18-
compiler.plugin("compilation", function(compilation) {
19-
if(compilation.compiler.isChild()) return;
20-
lastModulesCount = moduleCount;
21-
moduleCount = 0;
22-
doneModules = 0;
23-
handler(0, "compile");
24-
compilation.plugin("build-module", function(module) {
25-
moduleCount++;
26-
update();
27-
});
28-
compilation.plugin("succeed-module", function(module) {
29-
doneModules++;
30-
update();
31-
});
32-
compilation.plugin("optimize", function() {
33-
handler(0.73, "optimize");
34-
});
35-
compilation.plugin("before-hash", function() {
36-
handler(0.75, "hashing");
12+
if(compiler.compilers) {
13+
var states = new Array(compiler.compilers.length);
14+
compiler.compilers.forEach(function(compiler, idx) {
15+
compiler.apply(new ProgressPlugin(function(p, msg) {
16+
states[idx] = [p, msg];
17+
handler(states.map(function(state) {
18+
return state && state[0] || 0;
19+
}).reduce(function(a, b) {
20+
return a + b;
21+
}) / states.length, states.map(function(state) {
22+
return state && state[1];
23+
}).filter(Boolean).join(" | "));
24+
}));
3725
});
38-
compilation.plugin("before-chunk-assets", function() {
39-
handler(0.76, "create chunk assets");
26+
} else {
27+
var lastModulesCount = 0;
28+
var moduleCount = 1;
29+
var doneModules = 0;
30+
function update() {
31+
handler(0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, doneModules + "/" + moduleCount + " build modules");
32+
}
33+
compiler.plugin("compilation", function(compilation) {
34+
if(compilation.compiler.isChild()) return;
35+
lastModulesCount = moduleCount;
36+
moduleCount = 0;
37+
doneModules = 0;
38+
handler(0, "compile");
39+
compilation.plugin("build-module", function(module) {
40+
moduleCount++;
41+
update();
42+
});
43+
compilation.plugin("succeed-module", function(module) {
44+
doneModules++;
45+
update();
46+
});
47+
compilation.plugin("optimize", function() {
48+
handler(0.73, "optimize");
49+
});
50+
compilation.plugin("before-hash", function() {
51+
handler(0.75, "hashing");
52+
});
53+
compilation.plugin("before-chunk-assets", function() {
54+
handler(0.76, "create chunk assets");
55+
});
56+
compilation.plugin("additional-chunk-assets", function() {
57+
handler(0.78, "additional chunk assets");
58+
});
59+
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
60+
handler(0.8, "optimize chunk assets");
61+
callback();
62+
});
63+
compilation.plugin("optimize-assets", function(assets, callback) {
64+
handler(0.9, "optimize assets");
65+
callback();
66+
});
4067
});
41-
compilation.plugin("additional-chunk-assets", function() {
42-
handler(0.78, "additional chunk assets");
43-
});
44-
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
45-
handler(0.8, "optimize chunk assets");
68+
compiler.plugin("emit", function(compilation, callback) {
69+
handler(0.95, "emit");
4670
callback();
4771
});
48-
compilation.plugin("optimize-assets", function(assets, callback) {
49-
handler(0.9, "optimize assets");
50-
callback();
72+
compiler.plugin("done", function(stats) {
73+
handler(1, "");
5174
});
52-
});
53-
compiler.plugin("emit", function(compilation, callback) {
54-
handler(0.95, "emit");
55-
callback();
56-
});
57-
compiler.plugin("done", function(stats) {
58-
handler(1, "");
59-
});
75+
}
6076
};

test/ConfigTestCases.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ describe("ConfigTestCases", function() {
2727
var testDirectory = path.join(casesPath, category.name, testName);
2828
var outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
2929
var options = require(path.join(testDirectory, "webpack.config.js"));
30-
if(!options.context) options.context = testDirectory;
31-
if(!options.entry) options.entry = "./index.js";
32-
if(!options.target) options.target = "async-node";
33-
if(!options.output) options.output = {};
34-
if(!options.output.path) options.output.path = outputDirectory;
35-
if(!options.output.filename) options.output.filename = "bundle.js";
30+
[].concat(options).forEach(function(options, idx) {
31+
if(!options.context) options.context = testDirectory;
32+
if(!options.entry) options.entry = "./index.js";
33+
if(!options.target) options.target = "async-node";
34+
if(!options.output) options.output = {};
35+
if(!options.output.path) options.output.path = outputDirectory;
36+
if(!options.output.filename) options.output.filename = "bundle" + idx + ".js";
37+
});
3638
webpack(options, function(err, stats) {
3739
if(err) return done(err);
3840
var jsonStats = stats.toJson({
@@ -56,7 +58,8 @@ describe("ConfigTestCases", function() {
5658
return module.exports;
5759
} else return require(module);
5860
}
59-
_require("./bundle.js");
61+
for(var i = 0; i < [].concat(options).length; i++)
62+
_require("./bundle" + i + ".js");
6063
if(exportedTest === 0) return done(new Error("No tests exported by test case"));
6164
done();
6265
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should run a multi compiler", function() {
2+
3+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var path = require("path");
2+
var webpack = require("../../../../");
3+
4+
module.exports = [
5+
{
6+
7+
}
8+
];

0 commit comments

Comments
 (0)