Skip to content

Commit 880d8b8

Browse files
committed
fix(swc): support silent mode for running log
Add a --silent option to build/start commands and pass it to the SWC compiler extras. Suppress the SWC 'Running...' status line when silent mode is enabled or npm_config_loglevel is set to silent. Also adds unit tests for the new SWC logging gate behavior. Refs #3164
1 parent 55fa12e commit 880d8b8

7 files changed

Lines changed: 55 additions & 3 deletions

File tree

actions/build.action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export class BuildAction extends AbstractAction {
178178
) {
179179
const { SwcCompiler } = await import('../lib/compiler/swc/swc-compiler.js');
180180
const swc = new SwcCompiler(this.pluginsLoader);
181+
const isSilent = !!options.silent;
181182

182183
await swc.run(
183184
configuration,
@@ -194,6 +195,7 @@ export class BuildAction extends AbstractAction {
194195
),
195196
tsOptions,
196197
assetsManager: this.assetsManager,
198+
silent: isSilent,
197199
},
198200
onSuccess,
199201
);

commands/build.command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class BuildCommand extends AbstractCommand {
2020
'Use webpack for compilation (deprecated option, use --builder instead).',
2121
)
2222
.option('--type-check', 'Enable type checking (when SWC is used).')
23+
.option('--silent', 'Suppress informational compiler logs.')
2324
.option('--webpackPath [path]', 'Path to webpack configuration.')
2425
.option('--tsc', 'Use typescript compiler for compilation.')
2526
.option(
@@ -52,6 +53,7 @@ export class BuildCommand extends AbstractCommand {
5253
webpackPath: options.webpackPath,
5354
builder: options.builder,
5455
typeCheck: options.typeCheck,
56+
silent: !!options.silent,
5557
preserveWatchOutput:
5658
!!options.preserveWatchOutput &&
5759
!!options.watch &&

commands/context/build.context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface BuildCommandContext {
88
webpackPath?: string;
99
builder?: string;
1010
typeCheck?: boolean;
11+
silent?: boolean;
1112
preserveWatchOutput: boolean;
1213
all: boolean;
1314
}

commands/context/start.context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface StartCommandContext {
88
webpackPath?: string;
99
builder?: string;
1010
typeCheck?: boolean;
11+
silent?: boolean;
1112
preserveWatchOutput: boolean;
1213
debug?: boolean | string;
1314
exec?: string;

commands/start.command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class StartCommand extends AbstractCommand {
3131
)
3232
.option('--webpackPath [path]', 'Path to webpack configuration.')
3333
.option('--type-check', 'Enable type checking (when SWC is used).')
34+
.option('--silent', 'Suppress informational compiler logs.')
3435
.option('--tsc', 'Use typescript compiler for compilation.')
3536
.option(
3637
'--sourceRoot [sourceRoot]',
@@ -82,6 +83,7 @@ export class StartCommand extends AbstractCommand {
8283
webpackPath: options.webpackPath,
8384
builder: options.builder,
8485
typeCheck: options.typeCheck,
86+
silent: !!options.silent,
8587
preserveWatchOutput:
8688
!!options.preserveWatchOutput &&
8789
!!options.watch &&

lib/compiler/swc/swc-compiler.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type SwcCompilerExtras = {
3434
typeCheck: boolean;
3535
assetsManager: AssetsManager;
3636
tsOptions: ts.CompilerOptions;
37+
silent?: boolean;
3738
};
3839

3940
export class SwcCompiler extends BaseCompiler {
@@ -163,7 +164,9 @@ export class SwcCompiler extends BaseCompiler {
163164
extras: SwcCompilerExtras,
164165
swcrcFilePath?: string,
165166
) {
166-
process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...')));
167+
if (this.shouldLogSwcStatus(extras)) {
168+
process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...')));
169+
}
167170

168171
const swcCli = this.loadSwcCliBinary();
169172
const swcRcFile = await this.getSwcRcFileContentIfExists(swcrcFilePath);
@@ -202,7 +205,15 @@ export class SwcCompiler extends BaseCompiler {
202205
await swcCli.default(swcCliOpts);
203206
}
204207

205-
private loadSwcCliBinary(): any {
208+
private shouldLogSwcStatus(extras: SwcCompilerExtras): boolean {
209+
if (extras.silent) {
210+
return false;
211+
}
212+
const npmLogLevel = process.env.npm_config_loglevel?.toLowerCase();
213+
return npmLogLevel !== 'silent';
214+
}
215+
216+
private loadSwcCliBinary() {
206217
try {
207218
return require('@swc/cli/lib/swc/dir');
208219
} catch {

test/lib/compiler/swc/swc-compiler.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi, beforeEach } from 'vitest';
1+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
22
import { PluginsLoader } from '../../../../lib/compiler/plugins/plugins-loader.js';
33
import { SwcCompiler } from '../../../../lib/compiler/swc/swc-compiler.js';
44

@@ -307,4 +307,37 @@ describe('SWC Compiler', () => {
307307
expect(closeWatchersMock).toHaveBeenCalledTimes(2);
308308
});
309309
});
310+
311+
describe('shouldLogSwcStatus', () => {
312+
const originalLogLevel = process.env.npm_config_loglevel;
313+
314+
afterEach(() => {
315+
process.env.npm_config_loglevel = originalLogLevel;
316+
});
317+
318+
it('should return false when extras.silent is true', () => {
319+
const result = compiler['shouldLogSwcStatus']({
320+
silent: true,
321+
} as any);
322+
expect(result).toBe(false);
323+
});
324+
325+
it('should return false when npm log level is silent', () => {
326+
process.env.npm_config_loglevel = 'silent';
327+
328+
const result = compiler['shouldLogSwcStatus']({
329+
silent: false,
330+
} as any);
331+
expect(result).toBe(false);
332+
});
333+
334+
it('should return true when silent mode is not enabled', () => {
335+
process.env.npm_config_loglevel = 'warn';
336+
337+
const result = compiler['shouldLogSwcStatus']({
338+
silent: false,
339+
} as any);
340+
expect(result).toBe(true);
341+
});
342+
});
310343
});

0 commit comments

Comments
 (0)