Skip to content

Commit 053f3d7

Browse files
committed
feat: run tasks in parallel with workers
1 parent 09e8647 commit 053f3d7

File tree

4 files changed

+114
-29
lines changed

4 files changed

+114
-29
lines changed

packages/react-native-builder-bob/src/index.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import { cosmiconfig } from 'cosmiconfig';
77
import isGitDirty from 'is-git-dirty';
88
import prompts, { type PromptObject } from './utils/prompts';
99
import * as logger from './utils/logger';
10-
import buildCommonJS from './targets/commonjs';
11-
import buildModule from './targets/module';
12-
import buildTypescript from './targets/typescript';
13-
import buildCodegen from './targets/codegen';
14-
import customTarget from './targets/custom';
1510
import type { Options, Target } from './types';
11+
import { run } from './utils/workerize';
1612

1713
type ArgName = 'target';
1814

@@ -530,51 +526,36 @@ async function buildTarget(
530526
const targetName = Array.isArray(target) ? target[0] : target;
531527
const targetOptions = Array.isArray(target) ? target[1] : undefined;
532528

533-
const report = logger.grouped(targetName);
534-
535529
switch (targetName) {
536530
case 'commonjs':
537-
await buildCommonJS({
538-
root,
539-
source: path.resolve(root, source),
540-
output: path.resolve(root, output, 'commonjs'),
541-
exclude,
542-
options: targetOptions,
543-
report,
544-
});
545-
break;
546531
case 'module':
547-
await buildModule({
532+
await run(targetName, {
548533
root,
549534
source: path.resolve(root, source),
550-
output: path.resolve(root, output, 'module'),
535+
output: path.resolve(root, output, targetName),
551536
exclude,
552537
options: targetOptions,
553-
report,
554538
});
555539
break;
556540
case 'typescript':
557-
await buildTypescript({
541+
await run('typescript', {
558542
root,
559543
source: path.resolve(root, source),
560544
output: path.resolve(root, output, 'typescript'),
561545
options: targetOptions,
562-
report,
563546
});
564547
break;
565548
case 'codegen':
566-
await buildCodegen({
549+
await run('codegen', {
567550
root,
568551
source: path.resolve(root, source),
569552
output: path.resolve(root, output, 'typescript'),
570-
report,
571553
});
572554
break;
573555
case 'custom':
574-
await customTarget({
556+
await run('custom', {
575557
options: targetOptions,
576558
source: path.resolve(root, source),
577-
report,
578559
root,
579560
});
580561
break;

packages/react-native-builder-bob/src/targets/codegen/patches/patchCodegenAndroidPackage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs-extra';
22
import path from 'path';
33
import type { Report } from '../../../types';
4+
import kleur from 'kleur';
45

56
export const CODEGEN_DOCS =
67
'https://reactnative.dev/docs/the-new-architecture/using-codegen#configuring-codegen';
@@ -50,7 +51,9 @@ export async function patchCodegenAndroidPackage(
5051
// If this issue is ever fixed in react-native, this check will prevent the patching from running.
5152
if (!(await fs.pathExists(codegenJavaPath))) {
5253
report.info(
53-
`Could not find ${codegenJavaPath}. Skipping patching codegen java files.`
54+
`Could not find ${kleur.blue(
55+
path.relative(projectPath, codegenJavaPath)
56+
)}. Skipping patching codegen java files.`
5457
);
5558
return;
5659
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import {
2+
Worker,
3+
isMainThread,
4+
parentPort,
5+
workerData,
6+
} from 'node:worker_threads';
7+
import type { Report, Target } from '../types';
8+
import * as logger from './logger';
9+
import commonjs from '../targets/commonjs';
10+
import module from '../targets/module';
11+
import typescript from '../targets/typescript';
12+
import codegen from '../targets/codegen';
13+
import custom from '../targets/custom';
14+
import kleur from 'kleur';
15+
16+
type WorkerData<T extends Target> = {
17+
target: T;
18+
data: Omit<Parameters<(typeof targets)[T]>[0], 'report'>;
19+
};
20+
21+
const targets = {
22+
commonjs,
23+
module,
24+
typescript,
25+
codegen,
26+
custom,
27+
} as const;
28+
29+
export const run = async <T extends Target>(
30+
target: T,
31+
data: Omit<Parameters<(typeof targets)[T]>[0], 'report'>
32+
) => {
33+
if (!isMainThread) {
34+
throw new Error('Worker can only be run from the main thread');
35+
}
36+
37+
const worker = new Worker(__filename, {
38+
workerData: {
39+
target,
40+
data,
41+
} satisfies WorkerData<T>,
42+
env: {
43+
...process.env,
44+
FORCE_COLOR: process.stdout.isTTY ? '1' : '0',
45+
},
46+
});
47+
48+
const report = logger.grouped(target);
49+
50+
worker.on('message', (message) => {
51+
switch (message.type) {
52+
case 'info':
53+
report.info(message.message);
54+
break;
55+
case 'warn':
56+
report.warn(message.message);
57+
break;
58+
case 'error':
59+
report.error(message.message);
60+
break;
61+
case 'success':
62+
report.success(message.message);
63+
break;
64+
default:
65+
console.error('Unknown message type', message);
66+
}
67+
});
68+
69+
worker.on('error', (error) => {
70+
console.log(error);
71+
});
72+
73+
worker.on('exit', (code) => {
74+
if (code !== 0) {
75+
report.error(
76+
`Target ${kleur.blue(target)} exited with code ${kleur.red(code)}`
77+
);
78+
}
79+
});
80+
};
81+
82+
if (!isMainThread) {
83+
const { target, data } = workerData as WorkerData<Target>;
84+
85+
const report: Report = {
86+
info: (message) => parentPort?.postMessage({ type: 'info', message }),
87+
warn: (message) => parentPort?.postMessage({ type: 'warn', message }),
88+
error: (message) => parentPort?.postMessage({ type: 'error', message }),
89+
success: (message) => parentPort?.postMessage({ type: 'success', message }),
90+
};
91+
92+
if (target in targets) {
93+
// @ts-expect-error - typescript doesn't support correlated union types https://github.com/microsoft/TypeScript/issues/30581
94+
targets[target]({ ...data, report }).catch((error) => {
95+
console.log(error);
96+
process.exit(1);
97+
});
98+
} else {
99+
throw new Error(`Unknown target: ${target}`);
100+
}
101+
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,9 +4689,9 @@ __metadata:
46894689
linkType: hard
46904690

46914691
"caniuse-lite@npm:^1.0.30001406, caniuse-lite@npm:^1.0.30001640":
4692-
version: 1.0.30001645
4693-
resolution: "caniuse-lite@npm:1.0.30001645"
4694-
checksum: a4808bac31fdcdf183ce12f8c86d101e515b2df3423ae4284b930b493809ae88b3396b52ca2a197a3de3c94046ee5384cc9f0efeff5ccfb7c8cd385229527596
4692+
version: 1.0.30001704
4693+
resolution: "caniuse-lite@npm:1.0.30001704"
4694+
checksum: ac8c1b625198c9fdf03f55ef631a0917d29d368ded8e8b2e6a3111cb7433c30b2ef16a6dc44077fb6176e76881a51818d55f24efd04478f5e98b6724b64a8f5c
46954695
languageName: node
46964696
linkType: hard
46974697

0 commit comments

Comments
 (0)