Skip to content

Commit 0ca8721

Browse files
committed
refactor: use chalk for better compat
1 parent 90800b4 commit 0ca8721

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

packages/metro-core/src/commands/bundle-host/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import util from 'node:util';
2+
import chalk from 'chalk';
33
import Server from 'metro/src/Server';
44
import type { RequestOptions } from 'metro/src/shared/types';
55
import type { ModuleFederationConfigNormalized } from '../../types';
@@ -45,7 +45,7 @@ async function bundleFederatedHost(
4545
const hostEntryFilepath = global.__METRO_FEDERATION_HOST_ENTRY_PATH;
4646
if (!hostEntryFilepath) {
4747
logger.error(
48-
`${util.styleText('red', 'error')} Cannot determine the host entrypoint path.`,
48+
`${chalk.red('error')} Cannot determine the host entrypoint path.`,
4949
);
5050
throw new CLIError('Bundling failed');
5151
}

packages/metro-core/src/commands/bundle-remote/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'node:fs';
22
import path from 'node:path';
33
import { pathToFileURL } from 'node:url';
4-
import util from 'node:util';
4+
import chalk from 'chalk';
55
import { mergeConfig } from 'metro';
66
import Server from 'metro/src/Server';
77
import type { OutputOptions, RequestOptions } from 'metro/src/shared/types';
@@ -113,7 +113,7 @@ async function bundleFederatedRemote(
113113
const federationConfig = global.__METRO_FEDERATION_CONFIG;
114114
if (!federationConfig) {
115115
logger.error(
116-
`${util.styleText('red', 'error')} Module Federation configuration is missing.`,
116+
`${chalk.red('error')} Module Federation configuration is missing.`,
117117
);
118118
logger.info(
119119
"Import the plugin 'withModuleFederation' " +
@@ -127,7 +127,7 @@ async function bundleFederatedRemote(
127127
const containerEntryFilepath = global.__METRO_FEDERATION_REMOTE_ENTRY_PATH;
128128
if (!containerEntryFilepath) {
129129
logger.error(
130-
`${util.styleText('red', 'error')} Cannot determine the container entry file path.`,
130+
`${chalk.red('error')} Cannot determine the container entry file path.`,
131131
);
132132
logger.info(
133133
'To bundle a container, you need to expose at least one module ' +
@@ -140,21 +140,21 @@ async function bundleFederatedRemote(
140140
const manifestFilepath = global.__METRO_FEDERATION_MANIFEST_PATH;
141141
if (!manifestFilepath) {
142142
logger.error(
143-
`${util.styleText('red', 'error')} Cannot determine the manifest file path.`,
143+
`${chalk.red('error')} Cannot determine the manifest file path.`,
144144
);
145145
throw new CLIError('Bundling failed');
146146
}
147147

148148
if (rawConfig.resolver.platforms.indexOf(args.platform) === -1) {
149149
logger.error(
150-
`${util.styleText('red', 'error')}: Invalid platform ${
151-
args.platform ? `"${util.styleText('bold', args.platform)}" ` : ''
150+
`${chalk.red('error')}: Invalid platform ${
151+
args.platform ? `"${chalk.bold(args.platform)}" ` : ''
152152
}selected.`,
153153
);
154154

155155
logger.info(
156156
`Available platforms are: ${rawConfig.resolver.platforms
157-
.map((x) => `"${util.styleText('bold', x)}"`)
157+
.map((x) => `"${chalk.bold(x)}"`)
158158
.join(
159159
', ',
160160
)}. If you are trying to bundle for an out-of-tree platform, it may not be installed.`,
@@ -314,7 +314,7 @@ async function bundleFederatedRemote(
314314

315315
try {
316316
logger.info(
317-
`${util.styleText('blue', 'Processing remote container and exposed modules')}`,
317+
`${chalk.blue('Processing remote container and exposed modules')}`,
318318
);
319319

320320
for (const { requestOpts, saveBundleOpts, targetDir } of requests) {
@@ -338,11 +338,11 @@ async function bundleFederatedRemote(
338338
// );
339339
}
340340

341-
logger.info(`${util.styleText('blue', 'Processing manifest')}`);
341+
logger.info(`${chalk.blue('Processing manifest')}`);
342342
const manifestOutputFilepath = path.resolve(outputDir, 'mf-manifest.json');
343343
await fs.copyFile(manifestFilepath, manifestOutputFilepath);
344344
logger.info(
345-
`Done writing MF Manifest to:\n${util.styleText('dim', manifestOutputFilepath)}`,
345+
`Done writing MF Manifest to:\n${chalk.dim(manifestOutputFilepath)}`,
346346
);
347347
} finally {
348348
// incomplete types - this should be awaited

packages/metro-core/src/commands/utils/save-bundle-and-map.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { promises as fs } from 'node:fs';
2-
import util from 'node:util';
2+
import chalk from 'chalk';
33
import type { MixedSourceMap } from 'metro-source-map';
44
import relativizeSourceMapInline from 'metro/src/lib/relativizeSourceMap';
55
import type { OutputOptions } from 'metro/src/shared/types';
@@ -28,7 +28,7 @@ export async function saveBundleAndMap(
2828
const writeFns = [];
2929

3030
writeFns.push(async () => {
31-
log(`Writing bundle output to:\n${util.styleText('dim', bundleOutput)}`);
31+
log(`Writing bundle output to:\n${chalk.dim(bundleOutput)}`);
3232
await fs.writeFile(bundleOutput, bundle.code, encoding);
3333
log('Done writing bundle output');
3434
});
@@ -43,9 +43,7 @@ export async function saveBundleAndMap(
4343
}
4444

4545
writeFns.push(async () => {
46-
log(
47-
`Writing sourcemap output to:\n${util.styleText('dim', sourcemapOutput)}`,
48-
);
46+
log(`Writing sourcemap output to:\n${chalk.dim(sourcemapOutput)}`);
4947
await fs.writeFile(sourcemapOutput, map);
5048
log('Done writing sourcemap output');
5149
});

packages/metro-core/src/plugin/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import util from 'node:util';
2+
import chalk from 'chalk';
33
import type { ConfigT } from 'metro-config';
44
import type {
55
ModuleFederationConfig,
@@ -45,18 +45,16 @@ export function withModuleFederation(
4545
}
4646

4747
console.warn(
48-
util.styleText(
49-
'yellow',
48+
chalk.yellow(
5049
'Warning: Module Federation build is disabled for this command.\n',
5150
) +
52-
util.styleText(
53-
'yellow',
51+
chalk.yellow(
5452
'To enable Module Federation, please use one of the dedicated bundle commands:\n',
5553
) +
56-
` ${util.styleText('dim', '•')} bundle-mf-host` +
57-
util.styleText('dim', ' - for bundling a host application\n') +
58-
` ${util.styleText('dim', '•')} bundle-mf-remote` +
59-
util.styleText('dim', ' - for bundling a remote application\n'),
54+
` ${chalk.dim('•')} bundle-mf-host` +
55+
chalk.dim(' - for bundling a host application\n') +
56+
` ${chalk.dim('•')} bundle-mf-remote` +
57+
chalk.dim(' - for bundling a remote application\n'),
6058
);
6159

6260
return config;

0 commit comments

Comments
 (0)