Skip to content

Commit 16af669

Browse files
refactor: avoid printing stats at all for the plugin usage
1 parent f149b0a commit 16af669

1 file changed

Lines changed: 86 additions & 87 deletions

File tree

src/utils/setupHooks.js

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -54,106 +54,105 @@ function setupHooks(context) {
5454
// We are now on valid state
5555

5656
context.state = true;
57-
5857
context.stats = stats;
5958

60-
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
61-
process.nextTick(() => {
62-
const { compiler, logger, options, state, callbacks } = context;
63-
64-
// Check if still in valid state
65-
if (!state) {
66-
return;
67-
}
68-
69-
logger.log("Compilation finished");
70-
71-
const isMultiCompilerMode = Boolean(
72-
/** @type {MultiCompiler} */
73-
(compiler).compilers,
74-
);
75-
76-
/**
77-
* @type {StatsOptions | MultiStatsOptions | undefined}
78-
*/
79-
let statsOptions;
80-
81-
if (typeof options.stats !== "undefined") {
82-
statsOptions = isMultiCompilerMode
83-
? {
84-
children:
85-
/** @type {MultiCompiler} */
86-
(compiler).compilers.map(() => options.stats),
87-
}
88-
: options.stats;
89-
} else {
90-
statsOptions = isMultiCompilerMode
91-
? {
92-
children:
93-
/** @type {MultiCompiler} */
94-
(compiler).compilers.map((child) => child.options.stats),
95-
}
96-
: /** @type {Compiler} */ (compiler).options.stats;
97-
}
98-
99-
if (isMultiCompilerMode) {
100-
/** @type {MultiStatsOptions} */
101-
(statsOptions).children =
102-
/** @type {MultiStatsOptions} */
103-
(statsOptions).children.map(
104-
/**
105-
* @param {StatsOptions} childStatsOptions child stats options
106-
* @returns {StatsObjectOptions} object child stats options
107-
*/
108-
(childStatsOptions) => {
109-
childStatsOptions = normalizeStatsOptions(childStatsOptions);
110-
111-
if (typeof childStatsOptions.colors === "undefined") {
112-
const [firstCompiler] =
113-
/** @type {MultiCompiler} */
114-
(compiler).compilers;
59+
// For plugin support we should print nothing, because webpack/webpack-cli/webpack-dev-server will print them on using `stats.toString()`
60+
if (!context.isPlugin) {
61+
// Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
62+
process.nextTick(() => {
63+
const { compiler, logger, options, state, callbacks } = context;
11564

116-
childStatsOptions.colors =
117-
firstCompiler.webpack.cli.isColorSupported();
118-
}
65+
// Check if still in valid state
66+
if (!state) {
67+
return;
68+
}
11969

120-
return childStatsOptions;
121-
},
122-
);
123-
} else {
124-
statsOptions = normalizeStatsOptions(
125-
/** @type {StatsOptions} */ (statsOptions),
70+
logger.log("Compilation finished");
71+
72+
const isMultiCompilerMode = Boolean(
73+
/** @type {MultiCompiler} */
74+
(compiler).compilers,
12675
);
12776

128-
if (typeof statsOptions.colors === "undefined") {
129-
const { compiler } = /** @type {{ compiler: Compiler }} */ (context);
130-
statsOptions.colors = compiler.webpack.cli.isColorSupported();
77+
/**
78+
* @type {StatsOptions | MultiStatsOptions | undefined}
79+
*/
80+
let statsOptions;
81+
82+
if (typeof options.stats !== "undefined") {
83+
statsOptions = isMultiCompilerMode
84+
? {
85+
children:
86+
/** @type {MultiCompiler} */
87+
(compiler).compilers.map(() => options.stats),
88+
}
89+
: options.stats;
90+
} else {
91+
statsOptions = isMultiCompilerMode
92+
? {
93+
children:
94+
/** @type {MultiCompiler} */
95+
(compiler).compilers.map((child) => child.options.stats),
96+
}
97+
: /** @type {Compiler} */ (compiler).options.stats;
13198
}
132-
}
133-
134-
const printedStats = stats.toString(
135-
/** @type {StatsObjectOptions} */
136-
(statsOptions),
137-
);
138-
139-
// Avoid extra empty line when `stats: 'none'`
140-
if (printedStats) {
141-
if (context.isPlugin) {
142-
// Use logger when used as webpack plugin
143-
logger.log(printedStats);
99+
100+
if (isMultiCompilerMode) {
101+
/** @type {MultiStatsOptions} */
102+
(statsOptions).children =
103+
/** @type {MultiStatsOptions} */
104+
(statsOptions).children.map(
105+
/**
106+
* @param {StatsOptions} childStatsOptions child stats options
107+
* @returns {StatsObjectOptions} object child stats options
108+
*/
109+
(childStatsOptions) => {
110+
childStatsOptions = normalizeStatsOptions(childStatsOptions);
111+
112+
if (typeof childStatsOptions.colors === "undefined") {
113+
const [firstCompiler] =
114+
/** @type {MultiCompiler} */
115+
(compiler).compilers;
116+
117+
childStatsOptions.colors =
118+
firstCompiler.webpack.cli.isColorSupported();
119+
}
120+
121+
return childStatsOptions;
122+
},
123+
);
144124
} else {
125+
statsOptions = normalizeStatsOptions(
126+
/** @type {StatsOptions} */ (statsOptions),
127+
);
128+
129+
if (typeof statsOptions.colors === "undefined") {
130+
const { compiler } = /** @type {{ compiler: Compiler }} */ (
131+
context
132+
);
133+
statsOptions.colors = compiler.webpack.cli.isColorSupported();
134+
}
135+
}
136+
137+
const printedStats = stats.toString(
138+
/** @type {StatsObjectOptions} */
139+
(statsOptions),
140+
);
141+
142+
// Avoid extra empty line when `stats: 'none'`
143+
if (printedStats) {
145144
// eslint-disable-next-line no-console
146145
console.log(printedStats);
147146
}
148-
}
149147

150-
context.callbacks = [];
148+
context.callbacks = [];
151149

152-
// Execute callback that are delayed
153-
for (const callback of callbacks) {
154-
callback(stats);
155-
}
156-
});
150+
// Execute callback that are delayed
151+
for (const callback of callbacks) {
152+
callback(stats);
153+
}
154+
});
155+
}
157156
}
158157

159158
// eslint-disable-next-line prefer-destructuring

0 commit comments

Comments
 (0)