Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Commit 14f0c44

Browse files
richardscarrottgeowarin
authored andcommitted
Support Webpack MultiCompiler (#9)
* Support Webpack MultiCompiler
1 parent 230d761 commit 14f0c44

File tree

14 files changed

+181
-24
lines changed

14 files changed

+181
-24
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules/
1+
node_modules/
2+
.log
3+
test/fixtures/**/dist

src/friendly-errors-plugin.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const os = require('os');
66
const transformErrors = require('./core/transformErrors');
77
const formatErrors = require('./core/formatErrors');
88
const output = require('./output');
9-
const concat = require('./utils').concat;
9+
const utils = require('./utils');
10+
11+
const concat = utils.concat;
12+
const uniqueBy = utils.uniqueBy;
1013

1114
const defaultTransformers = [
1215
require('./transformers/babelSyntax'),
@@ -44,12 +47,12 @@ class FriendlyErrorsWebpackPlugin {
4447
}
4548

4649
if (hasErrors) {
47-
this.displayErrors(stats.compilation.errors, 'error');
50+
this.displayErrors(extractErrorsFromStats(stats, 'errors'), 'error');
4851
return;
4952
}
5053

5154
if (hasWarnings) {
52-
this.displayErrors(stats.compilation.warnings, 'warning');
55+
this.displayErrors(extractErrorsFromStats(stats, 'warnings'), 'warning');
5356
}
5457
});
5558

@@ -66,7 +69,7 @@ class FriendlyErrorsWebpackPlugin {
6669
}
6770

6871
displaySuccess(stats) {
69-
const time = stats.endTime - stats.startTime;
72+
const time = getCompileTime(stats);
7073
output.title('success', 'DONE', 'Compiled successfully in ' + time + 'ms');
7174

7275
if (this.compilationSuccessInfo.messages) {
@@ -98,6 +101,31 @@ class FriendlyErrorsWebpackPlugin {
98101
}
99102
}
100103

104+
function extractErrorsFromStats(stats, type) {
105+
if (isMultiStats(stats)) {
106+
const errors = stats.stats
107+
.reduce((errors, stats) => errors.concat(extractErrorsFromStats(stats, type)), []);
108+
// Dedupe to avoid showing the same error many times when multiple
109+
// compilers depend on the same module.
110+
return uniqueBy(errors, error => error.message);
111+
}
112+
return stats.compilation[type];
113+
}
114+
115+
function getCompileTime(stats) {
116+
if (isMultiStats(stats)) {
117+
// Webpack multi compilations run in parallel so using the longest duration.
118+
// https://webpack.github.io/docs/configuration.html#multiple-configurations
119+
return stats.stats
120+
.reduce((time, stats) => Math.max(time, getCompileTime(stats)), 0);
121+
}
122+
return stats.endTime - stats.startTime;
123+
}
124+
125+
function isMultiStats(stats) {
126+
return stats.stats;
127+
}
128+
101129
function getMaxSeverityErrors(errors) {
102130
const maxSeverity = getMaxInt(errors, 'severity');
103131
return errors.filter(e => e.severity === maxSeverity);

src/utils/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ function concat() {
1010
return Array.prototype.concat.apply(baseArray, args.slice(1));
1111
}
1212

13+
/**
14+
* Dedupes array based on criterion returned from iteratee function.
15+
* Ex: uniqueBy(
16+
* [{ id: 1 }, { id: 1 }, { id: 2 }],
17+
* val => val.id
18+
* ) = [{ id: 1 }, { id: 2 }]
19+
*/
20+
function uniqueBy(arr, fun) {
21+
const seen = {};
22+
return arr.filter(el => {
23+
const e = fun(el);
24+
return !(e in seen) && (seen[e] = 1);
25+
})
26+
}
27+
1328
module.exports = {
14-
concat: concat
15-
};
29+
concat: concat,
30+
uniqueBy: uniqueBy
31+
};

test/fixtures/eslint-warnings/webpack.config.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@ module.exports = {
1111

1212
],
1313
module: {
14-
preLoaders: [
14+
loaders: [
1515
{
1616
test: /\.js$/,
1717
loader: 'eslint',
18+
enforce: 'pre',
1819
include: __dirname
19-
}
20-
],
21-
loaders: [
20+
},
2221
{
2322
test: /\.jsx?$/,
2423
loader: 'babel',
2524
exclude: /node_modules/
2625
}
2726
]
2827
}
29-
};
28+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./non-existing');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('not-found');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = [
2+
{
3+
entry: __dirname + "/index.js",
4+
output: {
5+
path: __dirname + "/dist",
6+
filename: "bundle.js"
7+
}
8+
},
9+
{
10+
entry: __dirname + "/index2.js",
11+
output: {
12+
path: __dirname + "/dist",
13+
filename: "bundle2.js"
14+
}
15+
}
16+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'I am an entry point';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'I am another entry point';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = [
2+
{
3+
entry: __dirname + "/index.js",
4+
output: {
5+
path: __dirname + "/dist",
6+
filename: "bundle.js"
7+
}
8+
},
9+
{
10+
entry: __dirname + "/index2.js",
11+
output: {
12+
path: __dirname + "/dist",
13+
filename: "bundle2.js"
14+
}
15+
}
16+
];

0 commit comments

Comments
 (0)