Skip to content

Commit d27cbf3

Browse files
refactor: improve
1 parent 2875d0f commit d27cbf3

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ class CLIPlugin {
3939

4040
setupHelpfulOutput(compiler) {
4141
const pluginName = 'webpack-cli';
42-
const getCompilationName = () => (compiler.name ? ` '${compiler.name}'` : '');
42+
const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : '');
4343

4444
const { configPath } = this.options;
4545

4646
compiler.hooks.run.tap(pluginName, () => {
47+
const name = getCompilationName();
48+
49+
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
50+
4751
if (configPath) {
48-
this.logger.log(`Using config '${configPath}'`);
52+
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
4953
}
50-
this.logger.log(`Compilation${getCompilationName()} starting...`);
5154
});
5255

5356
compiler.hooks.watchRun.tap(pluginName, (compiler) => {
@@ -57,11 +60,13 @@ class CLIPlugin {
5760
this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
5861
}
5962

63+
const name = getCompilationName();
64+
65+
this.logger.log(`Compiler${name ? ` ${name}` : ''} starting...`);
66+
6067
if (configPath) {
61-
this.logger.log(`Using config ${configPath}`);
68+
this.logger.log(`Compiler${name ? ` ${name}` : ''} is using config: '${configPath}'`);
6269
}
63-
64-
this.logger.log(`Compilation${getCompilationName()} starting...`);
6570
});
6671

6772
compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
@@ -72,11 +77,13 @@ class CLIPlugin {
7277
});
7378

7479
(compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => {
75-
this.logger.log(`Compilation${getCompilationName()} finished`);
80+
const name = getCompilationName();
81+
82+
this.logger.log(`Compiler${name ? ` ${name}` : ''} finished`);
7683

7784
process.nextTick(() => {
7885
if (compiler.watchMode) {
79-
this.logger.log(`Compiler${getCompilationName()} is watching files for updates...`);
86+
this.logger.log(`Compiler${name ? `${name}` : ''} is watching files for updates...`);
8087
}
8188
});
8289
});

test/build/basic/basic.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ describe('bundle command', () => {
143143

144144
it('should log supplied config when logging level is log', () => {
145145
const { exitCode, stderr, stdout } = run(__dirname, ['--config', './log.config.js']);
146-
expect(exitCode).toBe(0);
147146
const configPath = resolve(__dirname, './log.config.js');
148-
expect(stderr).toContain(`Using config '${configPath}'`);
147+
148+
expect(exitCode).toBe(0);
149+
expect(stderr).toContain('Compiler starting...');
150+
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
151+
expect(stderr).toContain('Compiler finished');
149152
expect(stdout).toBeTruthy();
150153
});
151154
});

test/core-flags/infrastructure-logging.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe('infrastructure logging related flag', () => {
2323
const { exitCode, stderr, stdout } = run(__dirname, ['--infrastructure-logging-level', 'log']);
2424

2525
expect(exitCode).toBe(0);
26-
expect(stderr).toContain("Compilation 'compiler' starting...");
27-
expect(stderr).toContain("Compilation 'compiler' finished");
26+
expect(stderr).toContain("Compiler 'compiler' starting...");
27+
expect(stderr).toContain("Compiler 'compiler' finished");
2828
expect(stdout).toContain(`level: 'log'`);
2929
});
3030
});

test/json/json.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ describe('json', () => {
111111
const { exitCode, stderr, stdout } = run(__dirname, ['--json', '--config', 'logging.config.js']);
112112

113113
expect(exitCode).toBe(0);
114-
expect(stderr).toContain('Compilation starting');
115-
expect(stderr).toContain('Compilation finished');
114+
expect(stderr).toContain('Compiler starting...');
115+
expect(stderr).toContain('Compiler finished');
116116
expect(() => JSON.parse(stdout)).not.toThrow();
117117
expect(JSON.parse(stdout)['hash']).toBeDefined();
118118
});
@@ -121,8 +121,8 @@ describe('json', () => {
121121
const { exitCode, stderr, stdout } = run(__dirname, ['--json', 'stats.json', '--config', 'logging.config.js']);
122122

123123
expect(exitCode).toBe(0);
124-
expect(stderr).toContain('Compilation starting');
125-
expect(stderr).toContain('Compilation finished');
124+
expect(stderr).toContain('Compiler starting...');
125+
expect(stderr).toContain('Compiler finished');
126126
expect(stderr).toContain(successMessage);
127127
expect(stdout).toBeFalsy();
128128
expect(existsSync(resolve(__dirname, './stats.json'))).toBeTruthy();

test/serve/basic/serve-basic.test.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ describe('basic serve usage', () => {
306306
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
307307
});
308308

309+
it('should log used supplied config with serve', async () => {
310+
const { stderr, stdout } = await runServe(__dirname, ['--config', 'log.config.js', '--port', port]);
311+
const configPath = path.resolve(__dirname, './log.config.js');
312+
313+
expect(stderr).toContain('Compiler starting...');
314+
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
315+
expect(stderr).toContain('Compiler finished');
316+
expect(stdout).toBeTruthy();
317+
});
318+
309319
it("should log error on using '--watch' flag with serve", async () => {
310320
const { stdout, stderr } = await runServe(testPath, ['--watch']);
311321

@@ -329,12 +339,4 @@ describe('basic serve usage', () => {
329339
expect(stderr).toContain("Error: Unknown option '--unknown-flag'");
330340
expect(stdout).toBeFalsy();
331341
});
332-
333-
it('should log used supplied config with serve', async () => {
334-
const { stderr, stdout } = await runServe(__dirname, ['--config', 'log.config.js']);
335-
336-
const configPath = path.resolve(__dirname, './log.config.js');
337-
expect(stderr).toContain(`Using config '${configPath}'`);
338-
expect(stdout).toBeTruthy();
339-
});
340342
});

test/watch/basic/basic.test.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ describe('basic', () => {
159159
});
160160
});
161161

162+
it('should log supplied config with watch', (done) => {
163+
const proc = runAndGetWatchProc(__dirname, ['watch', '--config', 'log.config.js']);
164+
const configPath = resolve(__dirname, './log.config.js');
165+
166+
let stderr = '';
167+
168+
proc.stderr.on('data', (chunk) => {
169+
const data = chunk.toString();
170+
171+
stderr += stripAnsi(data);
172+
173+
if (/Compiler finished/.test(data)) {
174+
expect(stderr).toContain('Compiler starting...');
175+
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
176+
expect(stderr).toContain('Compiler finished');
177+
178+
proc.kill();
179+
done();
180+
}
181+
});
182+
});
183+
162184
it('should recompile upon file change using the `command` option and the `--watch` option and log warning', async () => {
163185
const { exitCode, stderr, stdout } = await run(__dirname, ['watch', '--watch', '--mode', 'development']);
164186

@@ -176,12 +198,4 @@ describe('basic', () => {
176198
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
177199
expect(stdout).toBeFalsy();
178200
});
179-
180-
it('should log supplied config with watch', async () => {
181-
const { stderr, stdout } = await run(__dirname, ['watch', '--config', 'log.config.js']);
182-
183-
const configPath = resolve(__dirname, './log.config.js');
184-
expect(stderr).toContain(`Using config '${configPath}'`);
185-
expect(stdout).toBeTruthy();
186-
});
187201
});

0 commit comments

Comments
 (0)