Skip to content

Commit a0ed46b

Browse files
committed
refactor(@angular-devkit/build-angular): use Node.js builtin instead of custom removeColor helper
The Node.js `node:util` builtin contains a helper (`stripVTControlCharacters`) that can be used to remove ANSI color characters from a string. Usage removes the need for a custom helper within the package.
1 parent b55bbde commit a0ed46b

File tree

2 files changed

+6
-11
lines changed
  • packages/angular_devkit/build_angular/src

2 files changed

+6
-11
lines changed

packages/angular_devkit/build_angular/src/tools/webpack/utils/stats.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
1010
import { logging } from '@angular-devkit/core';
1111
import assert from 'node:assert';
1212
import * as path from 'node:path';
13+
import { stripVTControlCharacters } from 'node:util';
1314
import { Configuration, StatsCompilation } from 'webpack';
1415
import { Schema as BrowserBuilderOptions } from '../../../builders/browser/schema';
1516
import { normalizeOptimization } from '../../../utils';
1617
import { BudgetCalculatorResult } from '../../../utils/bundle-calculator';
17-
import { colors as ansiColors, removeColor } from '../../../utils/color';
18+
import { colors as ansiColors } from '../../../utils/color';
1819
import { markAsyncChunksNonInitial } from './async-chunks';
1920
import { WebpackStatsOptions, getStatsOptions, normalizeExtraEntryPoints } from './helpers';
2021

@@ -310,7 +311,7 @@ function generateTableText(bundleInfo: (string | number)[][], colors: boolean):
310311
}
311312

312313
const currentLongest = (longest[i] ??= 0);
313-
const currentItemLength = removeColor(currentItem).length;
314+
const currentItemLength = stripVTControlCharacters(currentItem).length;
314315
if (currentLongest < currentItemLength) {
315316
longest[i] = currentItemLength;
316317
}
@@ -330,7 +331,7 @@ function generateTableText(bundleInfo: (string | number)[][], colors: boolean):
330331
continue;
331332
}
332333

333-
const currentItemLength = removeColor(currentItem).length;
334+
const currentItemLength = stripVTControlCharacters(currentItem).length;
334335
const stringPad = ' '.repeat(longest[i] - currentItemLength);
335336
// Values in columns at index 2 and 3 (Raw and Estimated sizes) are always right aligned.
336337
item[i] = i >= 2 ? stringPad + currentItem : currentItem + stringPad;

packages/angular_devkit/build_angular/src/utils/color.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import * as ansiColors from 'ansi-colors';
10-
import { WriteStream } from 'tty';
10+
import { WriteStream } from 'node:tty';
1111

1212
function supportColor(): boolean {
1313
if (process.env.FORCE_COLOR !== undefined) {
@@ -30,18 +30,12 @@ function supportColor(): boolean {
3030
}
3131

3232
if (process.stdout instanceof WriteStream) {
33-
return process.stdout.getColorDepth() > 1;
33+
return process.stdout.hasColors();
3434
}
3535

3636
return false;
3737
}
3838

39-
export function removeColor(text: string): string {
40-
// This has been created because when colors.enabled is false unstyle doesn't work
41-
// see: https://github.com/doowb/ansi-colors/blob/a4794363369d7b4d1872d248fc43a12761640d8e/index.js#L38
42-
return text.replace(ansiColors.ansiRegex, '');
43-
}
44-
4539
// Create a separate instance to prevent unintended global changes to the color configuration
4640
const colors = ansiColors.create();
4741
colors.enabled = supportColor();

0 commit comments

Comments
 (0)