From 880d8b86ab83d008aa02277f893dce876f55ca83 Mon Sep 17 00:00:00 2001 From: vinubabu323 Date: Sat, 28 Mar 2026 20:14:20 +0530 Subject: [PATCH] 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 --- actions/build.action.ts | 2 ++ commands/build.command.ts | 2 ++ commands/context/build.context.ts | 1 + commands/context/start.context.ts | 1 + commands/start.command.ts | 2 ++ lib/compiler/swc/swc-compiler.ts | 15 ++++++++-- test/lib/compiler/swc/swc-compiler.spec.ts | 35 +++++++++++++++++++++- 7 files changed, 55 insertions(+), 3 deletions(-) diff --git a/actions/build.action.ts b/actions/build.action.ts index ae777358d..9260a706a 100644 --- a/actions/build.action.ts +++ b/actions/build.action.ts @@ -178,6 +178,7 @@ export class BuildAction extends AbstractAction { ) { const { SwcCompiler } = await import('../lib/compiler/swc/swc-compiler.js'); const swc = new SwcCompiler(this.pluginsLoader); + const isSilent = !!options.silent; await swc.run( configuration, @@ -194,6 +195,7 @@ export class BuildAction extends AbstractAction { ), tsOptions, assetsManager: this.assetsManager, + silent: isSilent, }, onSuccess, ); diff --git a/commands/build.command.ts b/commands/build.command.ts index 4dd425033..249c8a519 100644 --- a/commands/build.command.ts +++ b/commands/build.command.ts @@ -20,6 +20,7 @@ export class BuildCommand extends AbstractCommand { 'Use webpack for compilation (deprecated option, use --builder instead).', ) .option('--type-check', 'Enable type checking (when SWC is used).') + .option('--silent', 'Suppress informational compiler logs.') .option('--webpackPath [path]', 'Path to webpack configuration.') .option('--tsc', 'Use typescript compiler for compilation.') .option( @@ -52,6 +53,7 @@ export class BuildCommand extends AbstractCommand { webpackPath: options.webpackPath, builder: options.builder, typeCheck: options.typeCheck, + silent: !!options.silent, preserveWatchOutput: !!options.preserveWatchOutput && !!options.watch && diff --git a/commands/context/build.context.ts b/commands/context/build.context.ts index d1e43c9c9..d9bea879e 100644 --- a/commands/context/build.context.ts +++ b/commands/context/build.context.ts @@ -8,6 +8,7 @@ export interface BuildCommandContext { webpackPath?: string; builder?: string; typeCheck?: boolean; + silent?: boolean; preserveWatchOutput: boolean; all: boolean; } diff --git a/commands/context/start.context.ts b/commands/context/start.context.ts index bd48debd0..8749d6d6e 100644 --- a/commands/context/start.context.ts +++ b/commands/context/start.context.ts @@ -8,6 +8,7 @@ export interface StartCommandContext { webpackPath?: string; builder?: string; typeCheck?: boolean; + silent?: boolean; preserveWatchOutput: boolean; debug?: boolean | string; exec?: string; diff --git a/commands/start.command.ts b/commands/start.command.ts index 8add79ad7..2e1eb3c0a 100644 --- a/commands/start.command.ts +++ b/commands/start.command.ts @@ -31,6 +31,7 @@ export class StartCommand extends AbstractCommand { ) .option('--webpackPath [path]', 'Path to webpack configuration.') .option('--type-check', 'Enable type checking (when SWC is used).') + .option('--silent', 'Suppress informational compiler logs.') .option('--tsc', 'Use typescript compiler for compilation.') .option( '--sourceRoot [sourceRoot]', @@ -82,6 +83,7 @@ export class StartCommand extends AbstractCommand { webpackPath: options.webpackPath, builder: options.builder, typeCheck: options.typeCheck, + silent: !!options.silent, preserveWatchOutput: !!options.preserveWatchOutput && !!options.watch && diff --git a/lib/compiler/swc/swc-compiler.ts b/lib/compiler/swc/swc-compiler.ts index 1511e0ad9..0cdfef604 100644 --- a/lib/compiler/swc/swc-compiler.ts +++ b/lib/compiler/swc/swc-compiler.ts @@ -34,6 +34,7 @@ export type SwcCompilerExtras = { typeCheck: boolean; assetsManager: AssetsManager; tsOptions: ts.CompilerOptions; + silent?: boolean; }; export class SwcCompiler extends BaseCompiler { @@ -163,7 +164,9 @@ export class SwcCompiler extends BaseCompiler { extras: SwcCompilerExtras, swcrcFilePath?: string, ) { - process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...'))); + if (this.shouldLogSwcStatus(extras)) { + process.nextTick(() => console.log(SWC_LOG_PREFIX, cyan('Running...'))); + } const swcCli = this.loadSwcCliBinary(); const swcRcFile = await this.getSwcRcFileContentIfExists(swcrcFilePath); @@ -202,7 +205,15 @@ export class SwcCompiler extends BaseCompiler { await swcCli.default(swcCliOpts); } - private loadSwcCliBinary(): any { + private shouldLogSwcStatus(extras: SwcCompilerExtras): boolean { + if (extras.silent) { + return false; + } + const npmLogLevel = process.env.npm_config_loglevel?.toLowerCase(); + return npmLogLevel !== 'silent'; + } + + private loadSwcCliBinary() { try { return require('@swc/cli/lib/swc/dir'); } catch { diff --git a/test/lib/compiler/swc/swc-compiler.spec.ts b/test/lib/compiler/swc/swc-compiler.spec.ts index d1a751383..fc2533948 100644 --- a/test/lib/compiler/swc/swc-compiler.spec.ts +++ b/test/lib/compiler/swc/swc-compiler.spec.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { PluginsLoader } from '../../../../lib/compiler/plugins/plugins-loader.js'; import { SwcCompiler } from '../../../../lib/compiler/swc/swc-compiler.js'; @@ -307,4 +307,37 @@ describe('SWC Compiler', () => { expect(closeWatchersMock).toHaveBeenCalledTimes(2); }); }); + + describe('shouldLogSwcStatus', () => { + const originalLogLevel = process.env.npm_config_loglevel; + + afterEach(() => { + process.env.npm_config_loglevel = originalLogLevel; + }); + + it('should return false when extras.silent is true', () => { + const result = compiler['shouldLogSwcStatus']({ + silent: true, + } as any); + expect(result).toBe(false); + }); + + it('should return false when npm log level is silent', () => { + process.env.npm_config_loglevel = 'silent'; + + const result = compiler['shouldLogSwcStatus']({ + silent: false, + } as any); + expect(result).toBe(false); + }); + + it('should return true when silent mode is not enabled', () => { + process.env.npm_config_loglevel = 'warn'; + + const result = compiler['shouldLogSwcStatus']({ + silent: false, + } as any); + expect(result).toBe(true); + }); + }); });