Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions actions/build.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -194,6 +195,7 @@ export class BuildAction extends AbstractAction {
),
tsOptions,
assetsManager: this.assetsManager,
silent: isSilent,
},
onSuccess,
);
Expand Down
2 changes: 2 additions & 0 deletions commands/build.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions commands/context/build.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface BuildCommandContext {
webpackPath?: string;
builder?: string;
typeCheck?: boolean;
silent?: boolean;
preserveWatchOutput: boolean;
all: boolean;
}
1 change: 1 addition & 0 deletions commands/context/start.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface StartCommandContext {
webpackPath?: string;
builder?: string;
typeCheck?: boolean;
silent?: boolean;
preserveWatchOutput: boolean;
debug?: boolean | string;
exec?: string;
Expand Down
2 changes: 2 additions & 0 deletions commands/start.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand Down Expand Up @@ -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 &&
Expand Down
15 changes: 13 additions & 2 deletions lib/compiler/swc/swc-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type SwcCompilerExtras = {
typeCheck: boolean;
assetsManager: AssetsManager;
tsOptions: ts.CompilerOptions;
silent?: boolean;
};

export class SwcCompiler extends BaseCompiler {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 34 additions & 1 deletion test/lib/compiler/swc/swc-compiler.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
});