Skip to content

Commit 4de0f97

Browse files
fix: output stats to stdout instead stdout
1 parent ba21f9a commit 4de0f97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3526
-3602
lines changed

jest.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
'use strict';
2-
31
module.exports = {
42
collectCoverage: false,
53
coveragePathIgnorePatterns: ['test', '<rootDir>/node_modules'],
64
moduleFileExtensions: ['js', 'json'],
75
testMatch: ['**/test/**/*.test.js'],
86
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
7+
snapshotResolver: './test/helpers/snapshotResolver.js',
98
};

package-lock.json

+653-649
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"webpack": "^4.0.0 || ^5.0.0"
3939
},
4040
"dependencies": {
41+
"colorette": "^1.2.1",
4142
"mem": "^8.0.0",
4243
"memfs": "^3.2.0",
4344
"mime-types": "^2.1.28",
@@ -56,22 +57,25 @@
5657
"chokidar": "^3.5.0",
5758
"connect": "^3.7.0",
5859
"cross-env": "^7.0.3",
60+
"deepmerge": "^4.2.2",
5961
"del": "^6.0.0",
6062
"del-cli": "^3.0.1",
6163
"eslint": "^7.17.0",
6264
"eslint-config-prettier": "^7.1.0",
6365
"eslint-plugin-import": "^2.22.1",
64-
"eslint-plugin-prettier": "^3.3.1",
66+
"eslint-plugin-prettier": "^3.3.0",
67+
"execa": "^5.0.0",
6568
"express": "^4.17.1",
6669
"file-loader": "^6.2.0",
67-
"husky": "^5.0.6",
70+
"husky": "^4.3.7",
6871
"jest": "^26.6.3",
6972
"lint-staged": "^10.5.3",
7073
"npm-run-all": "^4.1.5",
7174
"prettier": "^2.2.1",
7275
"standard-version": "^9.1.0",
76+
"strip-ansi": "^6.0.0",
7377
"supertest": "^6.0.1",
74-
"webpack": "^5.11.1"
78+
"webpack": "^5.13.0"
7579
},
7680
"keywords": [
7781
"webpack",

src/utils/setupHooks.js

+63-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import webpack from 'webpack';
2+
import colorette from 'colorette';
3+
14
export default function setupHooks(context) {
25
function invalid() {
36
if (context.state) {
4-
context.logger.info('Compiling...');
7+
context.logger.log('Compilation starting...');
58
}
69

710
// We are now in invalid state
@@ -20,47 +23,71 @@ export default function setupHooks(context) {
2023

2124
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
2225
process.nextTick(() => {
23-
const { state, compiler, callbacks, logger } = context;
26+
const { compiler, logger, state, callbacks } = context;
2427

2528
// Check if still in valid state
2629
if (!state) {
2730
return;
2831
}
2932

30-
// Print webpack output
31-
const printStats = (childCompiler, childStats) => {
32-
const statsString = childStats.toString(childCompiler.options.stats);
33-
const name = childCompiler.options.name
34-
? `Child "${childCompiler.options.name}": `
35-
: '';
36-
37-
if (statsString.length) {
38-
if (childStats.hasErrors()) {
39-
logger.error(`${name}${statsString}`);
40-
} else if (childStats.hasWarnings()) {
41-
logger.warn(`${name}${statsString}`);
42-
} else {
43-
logger.info(`${name}${statsString}`);
33+
logger.log('Compilation finished');
34+
35+
let statsOptions = compiler.compilers
36+
? {
37+
children: compiler.compilers.map((child) =>
38+
// eslint-disable-next-line no-undefined
39+
child.options ? child.options.stats : undefined
40+
),
4441
}
45-
}
42+
: compiler.options
43+
? compiler.options.stats
44+
: // eslint-disable-next-line no-undefined
45+
undefined;
46+
47+
const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
48+
49+
if (compiler.compilers) {
50+
statsOptions.children = statsOptions.children.map(
51+
(childStatsOptions) => {
52+
if (statsForWebpack4) {
53+
// eslint-disable-next-line no-param-reassign
54+
childStatsOptions = webpack.Stats.presetToOptions(
55+
childStatsOptions
56+
);
57+
}
4658

47-
let message = `${name}Compiled successfully.`;
59+
if (typeof childStatsOptions.colors === 'undefined') {
60+
// eslint-disable-next-line no-param-reassign
61+
childStatsOptions.colors = Boolean(colorette.options.enabled);
62+
}
4863

49-
if (childStats.hasErrors()) {
50-
message = `${name}Failed to compile.`;
51-
} else if (childStats.hasWarnings()) {
52-
message = `${name}Compiled with warnings.`;
64+
return childStatsOptions;
65+
}
66+
);
67+
} else if (
68+
typeof statsOptions.colors === 'undefined' ||
69+
typeof statsOptions === 'string'
70+
) {
71+
if (statsForWebpack4) {
72+
statsOptions = webpack.Stats.presetToOptions(statsOptions);
5373
}
5474

55-
logger.info(message);
56-
};
75+
statsOptions.colors = Boolean(colorette.options.enabled);
76+
}
5777

58-
if (compiler.compilers) {
59-
compiler.compilers.forEach((compilerFromMultiCompileMode, index) => {
60-
printStats(compilerFromMultiCompileMode, stats.stats[index]);
61-
});
62-
} else {
63-
printStats(compiler, stats);
78+
// TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
79+
if (compiler.compilers && statsForWebpack4) {
80+
statsOptions.colors = statsOptions.children.some(
81+
(child) => child.colors
82+
);
83+
}
84+
85+
const printedStats = stats.toString(statsOptions);
86+
87+
// Avoid extra empty line when `stats: 'none'`
88+
if (printedStats) {
89+
// eslint-disable-next-line no-console
90+
console.log(printedStats);
6491
}
6592

6693
// eslint-disable-next-line no-param-reassign
@@ -73,7 +100,10 @@ export default function setupHooks(context) {
73100
});
74101
}
75102

76-
context.compiler.hooks.watchRun.tap('DevMiddleware', invalid);
77-
context.compiler.hooks.invalid.tap('DevMiddleware', invalid);
78-
context.compiler.hooks.done.tap('DevMiddleware', done);
103+
context.compiler.hooks.watchRun.tap('webpack-dev-middleware', invalid);
104+
context.compiler.hooks.invalid.tap('webpack-dev-middleware', invalid);
105+
(context.compiler.webpack
106+
? context.compiler.hooks.afterDone
107+
: context.compiler.hooks.done
108+
).tap('webpack-dev-middleware', done);
79109
}

0 commit comments

Comments
 (0)