Skip to content

Commit e7cc639

Browse files
authored
chore: moved logger inside a module instead of having it inside the process (#1179)
1 parent a9940bd commit e7cc639

File tree

7 files changed

+46
-36
lines changed

7 files changed

+46
-36
lines changed

cli.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
require('v8-compile-cache');
66

77
const importLocal = require('import-local');
8+
const logger = require('./lib/utils/logger');
89

910
// Prefer the local installation of webpack-cli
1011
if (importLocal(__filename)) {
1112
return;
1213
}
1314
process.title = 'webpack';
14-
process.cliLogger = require('webpack-log')({
15-
name: 'webpack',
16-
});
1715

1816
const updateNotifier = require('update-notifier');
1917
const packageJson = require('./package.json');
@@ -24,7 +22,7 @@ const notifier = updateNotifier({
2422
});
2523

2624
if (notifier.update) {
27-
process.cliLogger.info(`Update available: ${notifier.update.latest}`);
25+
logger.info(`Update available: ${notifier.update.latest}`);
2826
}
2927

3028
const semver = require('semver');
@@ -33,7 +31,7 @@ const version = packageJson.engines.node;
3331

3432
if (!semver.satisfies(process.version, version)) {
3533
const rawVersion = version.replace(/[^\d\.]*/, '');
36-
process.cliLogger.error('webpack CLI requires at least Node v' + rawVersion + '. ' + 'You have ' + process.version + '.\n' + 'See https://webpack.js.org/ ' + 'for migration help and similar.');
34+
logger.error('webpack CLI requires at least Node v' + rawVersion + '. ' + 'You have ' + process.version + '.\n' + 'See https://webpack.js.org/ ' + 'for migration help and similar.');
3735
process.exit(1);
3836
}
3937

lib/bootstrap.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const WebpackCLI = require('./webpack-cli');
33
const { core, commands } = require('./utils/cli-flags');
44
const cmdArgs = require('command-line-args');
5+
const logger = require('./utils/logger');
56

67
require('./utils/process-log');
78

@@ -65,7 +66,7 @@ async function runCLI(cli, commandIsUsed) {
6566
args._unknown
6667
.filter(e => e)
6768
.forEach(unknown => {
68-
process.cliLogger.warn('Unknown argument:', unknown);
69+
logger.warn('Unknown argument:', unknown);
6970
});
7071
}
7172
const result = await cli.run(args, core);
@@ -74,7 +75,7 @@ async function runCLI(cli, commandIsUsed) {
7475
}
7576
} catch (err) {
7677
if (err.name === 'UNKNOWN_VALUE') {
77-
process.cliLogger.error(`Parse Error (unknown argument): ${err.value}`);
78+
logger.error(`Parse Error (unknown argument): ${err.value}`);
7879
return;
7980
} else if (err.name === 'ALREADY_SET') {
8081
const argsMap = {};
@@ -99,9 +100,9 @@ async function runCLI(cli, commandIsUsed) {
99100

100101
await cli.run(args, core);
101102
process.stdout.write('\n');
102-
process.cliLogger.warn('Duplicate flags found, defaulting to last set value');
103+
logger.warn('Duplicate flags found, defaulting to last set value');
103104
} else {
104-
process.cliLogger.error(err);
105+
logger.error(err);
105106
return;
106107
}
107108
}

lib/commands/external.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable @typescript-eslint/explicit-function-return-type */
22
const { prompt } = require('inquirer');
33
const chalk = require('chalk');
4+
const logger = require('../utils/logger');
5+
46
class ExternalCommand {
57
static async runCommand(command, args = []) {
68
const cp = require('child_process');
@@ -13,7 +15,7 @@ class ExternalCommand {
1315
reject(error);
1416
});
1517

16-
executedCommand.on('exit', code => {
18+
executedCommand.on('exit', () => {
1719
resolve();
1820
});
1921
});
@@ -37,7 +39,7 @@ class ExternalCommand {
3739
const options = [isYarn ? 'add' : 'install', '-D', scopeName];
3840

3941
const commandToBeRun = `${packageManager} ${options.join(' ')}`;
40-
process.cliLogger.error(`The command moved into a separate package: ${chalk.keyword('orange')(name)}\n`);
42+
logger.error(`The command moved into a separate package: ${chalk.keyword('orange')(name)}\n`);
4143
const question = `Would you like to install ${name}? (That will run ${chalk.green(commandToBeRun)})`;
4244
const { installConfirm } = await prompt([
4345
{

lib/groups/advanced.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/explicit-function-return-type */
22
const GroupHelper = require('../utils/group-helper');
3+
const logger = require('../utils/logger');
34

45
class AdvancedGroup extends GroupHelper {
56
constructor(options) {
@@ -15,7 +16,7 @@ class AdvancedGroup extends GroupHelper {
1516
name = name.substring(0, p);
1617
}
1718
} catch (e) {
18-
process.cliLogger.error('Invalid plugin arguments ' + name + ' (' + e + ').');
19+
logger.error('Invalid plugin arguments ' + name + ' (' + e + ').');
1920
process.exit(-1); // eslint-disable-line
2021
}
2122

@@ -24,20 +25,20 @@ class AdvancedGroup extends GroupHelper {
2425
const resolve = require('enhanced-resolve');
2526
path = resolve.sync(process.cwd(), name);
2627
} catch (e) {
27-
process.cliLogger.error('Cannot resolve plugin ' + name + '.');
28+
logger.error('Cannot resolve plugin ' + name + '.');
2829
process.exit(-1); // eslint-disable-line
2930
}
3031
let Plugin;
3132
try {
3233
Plugin = require(path);
3334
} catch (e) {
34-
process.cliLogger.error('Cannot load plugin ' + name + '. (' + path + ')');
35+
logger.error('Cannot load plugin ' + name + '. (' + path + ')');
3536
throw e;
3637
}
3738
try {
3839
return new Plugin(args);
3940
} catch (e) {
40-
process.cliLogger.error('Cannot instantiate plugin ' + name + '. (' + path + ')');
41+
logger.error('Cannot instantiate plugin ' + name + '. (' + path + ')');
4142
throw e;
4243
}
4344
}
@@ -75,11 +76,11 @@ class AdvancedGroup extends GroupHelper {
7576
if (args.global) {
7677
const globalArrLen = args.global.length;
7778
if (!globalArrLen) {
78-
process.cliLogger.warn('Argument to global flag is none');
79+
logger.warn('Argument to global flag is none');
7980
return;
8081
}
8182
if (globalArrLen === 1) {
82-
process.cliLogger.warn('Argument to global flag expected a key/value pair');
83+
logger.warn('Argument to global flag expected a key/value pair');
8384
return;
8485
}
8586

@@ -92,12 +93,12 @@ class AdvancedGroup extends GroupHelper {
9293
const argVal = arg.substr(splitIdx + 1);
9394
const argKey = arg.substr(0, splitIdx);
9495
if (!argVal.length) {
95-
process.cliLogger.warn(`Found unmatching value for global flag key '${argKey}'`);
96+
logger.warn(`Found unmatching value for global flag key '${argKey}'`);
9697
return;
9798
}
9899
// eslint-disable-next-line no-prototype-builtins
99100
if (providePluginObject.hasOwnProperty(argKey)) {
100-
process.cliLogger.warn(`Overriding key '${argKey}' for global flag`);
101+
logger.warn(`Overriding key '${argKey}' for global flag`);
101102
}
102103
providePluginObject[argKey] = argVal;
103104
return;
@@ -106,10 +107,10 @@ class AdvancedGroup extends GroupHelper {
106107
const nextArg = args.global[idx + 1];
107108
// eslint-disable-next-line no-prototype-builtins
108109
if (providePluginObject.hasOwnProperty(arg)) {
109-
process.cliLogger.warn(`Overriding key '${arg}' for global flag`);
110+
logger.warn(`Overriding key '${arg}' for global flag`);
110111
}
111112
if (!nextArg) {
112-
process.cliLogger.warn(`Found unmatching value for global flag key '${arg}'`);
113+
logger.warn(`Found unmatching value for global flag key '${arg}'`);
113114
return;
114115
}
115116
providePluginObject[arg] = nextArg;

lib/utils/compiler.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const webpack = require('webpack');
33
const chalk = require('chalk');
44
const Table = require('cli-table3');
5+
const logger = require('./logger');
56

67
function setUpHookForCompiler(compiler, outputOptions, options) {
78
const { ProgressPlugin } = webpack;
@@ -20,7 +21,7 @@ function setUpHookForCompiler(compiler, outputOptions, options) {
2021
}
2122

2223
if (msg && tmpMsg != msg) {
23-
process.cliLogger.info(percent + '% ' + msg);
24+
logger.info(percent + '% ' + msg);
2425
}
2526
tmpMsg = msg;
2627
};
@@ -44,17 +45,17 @@ function setUpHookForCompiler(compiler, outputOptions, options) {
4445
if (outputOptions.watch) {
4546
compiler.hooks.watchRun.tap('WebpackInfo', compilation => {
4647
const compilationName = compilation.name ? compilation.name : '';
47-
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
48+
logger.info('Compilation ' + compilationName + ' starting…');
4849
});
4950
} else {
5051
compiler.hooks.beforeRun.tap('WebpackInfo', compilation => {
5152
const compilationName = compilation.name ? compilation.name : '';
52-
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
53+
logger.info('Compilation ' + compilationName + ' starting…');
5354
});
5455
}
5556
compiler.hooks.done.tap('WebpackInfo', compilation => {
5657
const compilationName = compilation.name ? compilation.name : '';
57-
process.cliLogger.info('Compilation ' + compilationName + ' finished');
58+
logger.info('Compilation ' + compilationName + ' finished');
5859
});
5960
}
6061
}
@@ -117,22 +118,22 @@ function generateOutputForSingleCompilation(statsObj, statsErrors, processingMes
117118
process.stdout.write('\n\n');
118119

119120
if (warning.message) {
120-
process.cliLogger.warn(warning.message);
121+
logger.warn(warning.message);
121122
process.stdout.write('\n');
122-
process.cliLogger.warn(warning.stack);
123+
logger.warn(warning.stack);
123124
return;
124125
}
125-
process.cliLogger.warn(warning);
126+
logger.warn(warning);
126127
});
127128
process.stdout.write('\n');
128129
}
129130

130131
if (statsErrors) {
131132
statsErrors.forEach(err => {
132-
if (err.loc) process.cliLogger.warn(err.loc);
133+
if (err.loc) logger.warn(err.loc);
133134
if (err.name) {
134135
process.stdout.write('\n');
135-
process.cliLogger.error(err.name);
136+
logger.error(err.name);
136137
}
137138
});
138139
}
@@ -150,7 +151,7 @@ function generateOutput(outputOptions, stats, statsErrors, processingMessageBuff
150151
generateOutputForSingleCompilation(statsObj, statsErrors, processingMessageBuffer);
151152
process.stdout.write('\n');
152153
if (outputOptions.watch) {
153-
process.cliLogger.info('watching files for updates...');
154+
logger.info('watching files for updates...');
154155
}
155156
}
156157

@@ -163,7 +164,7 @@ function compilerCallback(compiler, err, stats, lastHash, options, outputOptions
163164
}
164165
if (err) {
165166
lastHash = null;
166-
process.cliLogger.error(err.stack || err);
167+
logger.error(err.stack || err);
167168
process.exit(1); // eslint-disable-line
168169
}
169170
if (outputOptions.json && !outputOptions.silent) {
@@ -213,7 +214,7 @@ async function webpackInstance(opts) {
213214
compiler = getCompiler(options);
214215
} catch (err) {
215216
process.stdout.write('\n');
216-
process.cliLogger.error(`${err.name}: ${err.message}`);
217+
logger.error(`${err.name}: ${err.message}`);
217218
process.stdout.write('\n');
218219
return;
219220
}

lib/utils/logger.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const logger = require('webpack-log')({
2+
name: 'webpack',
3+
});
4+
5+
module.exports = logger;

lib/utils/process-log.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
const logger = require('./logger');
2+
13
/* eslint-disable @typescript-eslint/explicit-function-return-type */
24
function logErrorAndExit(error) {
3-
if (error && error.stack) process.cliLogger.error(error.stack);
5+
if (error && error.stack) logger.error(error.stack);
46
process.exit(1);
57
}
68

79
process.on('uncaughtException', error => {
8-
process.cliLogger.error(`Uncaught exception: ${error}`);
10+
logger.error(`Uncaught exception: ${error}`);
911
logErrorAndExit(error);
1012
});
1113

1214
process.on('unhandledRejection', error => {
13-
process.cliLogger.error(`Promise rejection: ${error}`);
15+
logger.error(`Promise rejection: ${error}`);
1416
logErrorAndExit(error);
1517
});
1618

0 commit comments

Comments
 (0)