Skip to content

Commit

Permalink
Use ReportableEvent for bundle save logs
Browse files Browse the repository at this point in the history
Summary:
Removes uses of `console.log` in favour of reportable events for building bundles programmatically or through the CLI.

Fixes `metro build --json` printing non-JSON output, etc.

Changelog:
```
**[Fix]** Don't use console.log directly in bundle builds
```

Reviewed By: rubennorte

Differential Revision: D65949862

fbshipit-source-id: 77fe698c15dc6ce499a1fa0e1ebc0fb39ba7814e
  • Loading branch information
robhogan authored and facebook-github-bot committed Nov 14, 2024
1 parent 282e00a commit 91d74d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/metro/src/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export type RunBuildOptions = {
onComplete?: () => void,
onProgress?: (transformedFileCount: number, totalFileCount: number) => void,
minify?: boolean,
output?: {
output?: $ReadOnly<{
build: (
MetroServer,
RequestOptions,
Expand All @@ -114,10 +114,10 @@ export type RunBuildOptions = {
...
},
OutputOptions,
(...args: Array<string>) => void,
(logMessage: string) => void,
) => Promise<mixed>,
...
},
}>,
platform?: string,
sourceMap?: boolean,
sourceMapUrl?: string,
Expand Down Expand Up @@ -377,7 +377,6 @@ exports.runBuild = async (
onComplete,
onProgress,
minify = true,
// $FlowFixMe[incompatible-variance] frozen objects are readonly
output = outputBundle,
out,
platform = 'web',
Expand Down Expand Up @@ -429,8 +428,12 @@ exports.runBuild = async (
platform,
};

// eslint-disable-next-line no-console
await output.save(metroBundle, outputOptions, console.log);
await output.save(metroBundle, outputOptions, message =>
config.reporter.update({
type: 'bundle_save_log',
message,
}),
);
}

return metroBundle;
Expand Down
3 changes: 3 additions & 0 deletions packages/metro/src/lib/TerminalReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ class TerminalReporter {
case 'bundle_build_done':
this._logBundleBuildDone(event.buildID);
break;
case 'bundle_save_log':
this.terminal.log('LOG:' + event.message);
break;
case 'bundle_build_failed':
this._logBundleBuildFailed(event.buildID);
break;
Expand Down
5 changes: 5 additions & 0 deletions packages/metro/src/lib/reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export type ReportableEvent =
type: 'bundle_build_failed',
...
}
| {
type: 'bundle_save_log',
message: string,
...
}
| {
buildID: string,
bundleDetails: BundleDetails,
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/shared/output/bundle.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function saveBundleAndMap(
...
},
options: OutputOptions,
log: (...args: Array<string>) => void,
log: string => void,
): Promise<mixed> {
const {
bundleOutput,
Expand Down

0 comments on commit 91d74d0

Please sign in to comment.