Skip to content

Commit 2cb0c0e

Browse files
authored
fix: callback deprecation (#1977)
1 parent 8b8c007 commit 2cb0c0e

File tree

4 files changed

+107
-88
lines changed

4 files changed

+107
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { packageExists } = require('../utils/package-exists');
2+
const webpack = packageExists('webpack') ? require('webpack') : undefined;
3+
const logger = require('../utils/logger');
4+
5+
const PluginName = 'webpack-cli';
6+
7+
class WebpackCLIPlugin {
8+
constructor(options) {
9+
this.options = options;
10+
}
11+
async apply(compiler) {
12+
const compilers = compiler.compilers || [compiler];
13+
14+
for (const compiler of compilers) {
15+
if (this.options.progress) {
16+
const { ProgressPlugin } = compiler.webpack || webpack;
17+
18+
let progressPluginExists;
19+
20+
if (compiler.options.plugins) {
21+
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
22+
}
23+
24+
if (!progressPluginExists) {
25+
new ProgressPlugin().apply(compiler);
26+
}
27+
}
28+
}
29+
30+
const compilationName = (compilation) => (compilation.name ? ` ${compilation.name}` : '');
31+
32+
compiler.hooks.watchRun.tap(PluginName, (compilation) => {
33+
const { bail, watch } = compilation.options;
34+
if (bail && watch) {
35+
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
36+
}
37+
38+
logger.success(`Compilation${compilationName(compilation)} starting...`);
39+
});
40+
41+
compiler.hooks.done.tap(PluginName, (compilation) => {
42+
logger.success(`Compilation${compilationName(compilation)} finished`);
43+
44+
process.nextTick(() => {
45+
if (compiler.watchMode) {
46+
logger.success('watching files for updates...');
47+
}
48+
});
49+
});
50+
}
51+
}
52+
53+
module.exports = WebpackCLIPlugin;

packages/webpack-cli/lib/webpack-cli.js

+45-87
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { toKebabCase } = require('./utils/helpers');
1010
const assignFlagDefaults = require('./utils/flag-defaults');
1111
const { writeFileSync } = require('fs');
1212
const { options: coloretteOptions } = require('colorette');
13+
const WebpackCLIPlugin = require('./plugins/WebpackCLIPlugin');
1314

1415
// CLI arg resolvers
1516
const handleConfigResolution = require('./groups/ConfigGroup');
@@ -212,24 +213,27 @@ class WebpackCLI extends GroupHelper {
212213
return this.runOptionGroups(args);
213214
}
214215

215-
createCompiler(options) {
216+
handleError(error) {
217+
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
218+
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
219+
const ValidationError = webpack.ValidationError || webpack.WebpackOptionsValidationError;
220+
221+
// In case of schema errors print and exit process
222+
// For webpack@4 and webpack@5
223+
if (error instanceof ValidationError) {
224+
logger.error(error.message);
225+
} else {
226+
logger.error(error);
227+
}
228+
}
229+
230+
createCompiler(options, callback) {
216231
let compiler;
217232

218233
try {
219-
compiler = webpack(options);
234+
compiler = webpack(options, callback);
220235
} catch (error) {
221-
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
222-
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
223-
const ValidationError = webpack.ValidationError ? webpack.ValidationError : webpack.WebpackOptionsValidationError;
224-
225-
// In case of schema errors print and exit process
226-
// For webpack@4 and webpack@5
227-
if (error instanceof ValidationError) {
228-
logger.error(error.message);
229-
} else {
230-
logger.error(error);
231-
}
232-
236+
this.handleError(error);
233237
process.exit(2);
234238
}
235239

@@ -245,54 +249,35 @@ class WebpackCLI extends GroupHelper {
245249
async run(args, cliOptions) {
246250
await this.processArgs(args, cliOptions);
247251

248-
const compiler = this.createCompiler(this.compilerConfiguration);
249-
250-
const options = this.compilerConfiguration;
251-
const outputOptions = this.outputConfiguration;
252-
253-
if (outputOptions.interactive) {
254-
const interactive = require('./utils/interactive');
252+
let compiler;
255253

256-
return interactive(compiler, options, outputOptions);
257-
}
254+
let options = this.compilerConfiguration;
255+
let outputOptions = this.outputConfiguration;
258256

259-
const compilers = compiler.compilers ? compiler.compilers : [compiler];
260-
const isWatchMode = Boolean(compilers.find((compiler) => compiler.options.watch));
261257
const isRawOutput = typeof outputOptions.json === 'undefined';
262258

263259
if (isRawOutput) {
264-
for (const compiler of compilers) {
265-
if (outputOptions.progress) {
266-
const { ProgressPlugin } = webpack;
267-
268-
let progressPluginExists;
269-
270-
if (compiler.options.plugins) {
271-
progressPluginExists = Boolean(compiler.options.plugins.find((e) => e instanceof ProgressPlugin));
272-
}
260+
const webpackCLIPlugin = new WebpackCLIPlugin({
261+
progress: outputOptions.progress,
262+
});
273263

274-
if (!progressPluginExists) {
275-
new ProgressPlugin().apply(compiler);
276-
}
264+
const addPlugin = (options) => {
265+
if (!options.plugins) {
266+
options.plugins = [];
277267
}
268+
options.plugins.unshift(webpackCLIPlugin);
269+
};
270+
if (Array.isArray(options)) {
271+
options.forEach(addPlugin);
272+
} else {
273+
addPlugin(options);
278274
}
279-
280-
compiler.hooks.watchRun.tap('watchInfo', (compilation) => {
281-
if (compilation.options.bail && isWatchMode) {
282-
logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
283-
}
284-
285-
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} starting...`);
286-
});
287-
compiler.hooks.done.tap('watchInfo', (compilation) => {
288-
logger.success(`Compilation${compilation.name ? `${compilation.name}` : ''} finished`);
289-
});
290275
}
291276

292277
const callback = (error, stats) => {
293278
if (error) {
294-
logger.error(error);
295-
process.exit(1);
279+
this.handleError(error);
280+
process.exit(2);
296281
}
297282

298283
if (stats.hasErrors()) {
@@ -314,9 +299,11 @@ class WebpackCLI extends GroupHelper {
314299
return stats;
315300
};
316301

302+
const getStatsOptionsFromCompiler = (compiler) => getStatsOptions(compiler.options ? compiler.options.stats : undefined);
303+
317304
const foundStats = compiler.compilers
318-
? { children: compiler.compilers.map((compiler) => getStatsOptions(compiler.options.stats)) }
319-
: getStatsOptions(compiler.options.stats);
305+
? { children: compiler.compilers.map(getStatsOptionsFromCompiler) }
306+
: getStatsOptionsFromCompiler(compiler);
320307

321308
if (outputOptions.json === true) {
322309
process.stdout.write(JSON.stringify(stats.toJson(foundStats), null, 2) + '\n');
@@ -335,46 +322,17 @@ class WebpackCLI extends GroupHelper {
335322
} else {
336323
logger.raw(`${stats.toString(foundStats)}`);
337324
}
338-
339-
if (isWatchMode) {
340-
logger.success('watching files for updates...');
341-
}
342325
};
343326

344-
if (isWatchMode) {
345-
const watchOptions = (compiler.options && compiler.options.watchOptions) || {};
327+
compiler = this.createCompiler(options, callback);
346328

347-
if (watchOptions.stdin) {
348-
process.stdin.on('end', function () {
349-
process.exit();
350-
});
351-
process.stdin.resume();
352-
}
353-
354-
return new Promise((resolve) => {
355-
compiler.watch(watchOptions, (error, stats) => {
356-
callback(error, stats);
329+
if (compiler && outputOptions.interactive) {
330+
const interactive = require('./utils/interactive');
357331

358-
resolve();
359-
});
360-
});
361-
} else {
362-
return new Promise((resolve) => {
363-
compiler.run((error, stats) => {
364-
if (compiler.close) {
365-
compiler.close(() => {
366-
callback(error, stats);
367-
368-
resolve();
369-
});
370-
} else {
371-
callback(error, stats);
372-
373-
resolve();
374-
}
375-
});
376-
});
332+
interactive(compiler, options, outputOptions);
377333
}
334+
335+
return Promise.resolve();
378336
}
379337
}
380338

test/core-flags/cache-flags.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
'use strict';
22

3+
const path = require('path');
4+
const rimraf = require('rimraf');
35
const { run, isWindows } = require('../utils/test-utils');
46
const { existsSync, writeFileSync, unlinkSync } = require('fs');
57
const { resolve } = require('path');
68

79
describe('cache related flags from core', () => {
10+
beforeEach((done) => {
11+
rimraf(path.join(__dirname, '../../node_modules/.cache/webpack/*'), () => {
12+
done();
13+
});
14+
});
15+
816
it('should be successful with --cache ', () => {
917
const { stderr, stdout } = run(__dirname, ['--cache']);
1018
expect(stderr).toBeFalsy();

test/utils/cli-plugin-test/plugin.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ describe('webpack-cli-test-plugin Test', () => {
1010
if (typeof cli !== 'undefined') {
1111
expect(stdout).toContain(`alias: { alias: [ 'alias1', 'alias2' ] }`);
1212
}
13-
expect(stdout).toContain('plugins: [ WebpackCLITestPlugin { opts: [Array], showAll: true } ]');
13+
expect(stdout).toContain(` WebpackCLITestPlugin { opts: [Array], showAll: true }`);
1414
});
1515
});

0 commit comments

Comments
 (0)