Skip to content

Commit 28a0f78

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

File tree

4 files changed

+112
-24
lines changed

4 files changed

+112
-24
lines changed

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

Lines changed: 7 additions & 20 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

@@ -534,27 +530,18 @@ async function buildTarget(
534530

535531
switch (targetName) {
536532
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;
546533
case 'module':
547-
await buildModule({
534+
await run(targetName, {
548535
root,
549536
source: path.resolve(root, source),
550-
output: path.resolve(root, output, 'module'),
537+
output: path.resolve(root, output, targetName),
551538
exclude,
552539
options: targetOptions,
553540
report,
554541
});
555542
break;
556543
case 'typescript':
557-
await buildTypescript({
544+
await run('typescript', {
558545
root,
559546
source: path.resolve(root, source),
560547
output: path.resolve(root, output, 'typescript'),
@@ -563,19 +550,19 @@ async function buildTarget(
563550
});
564551
break;
565552
case 'codegen':
566-
await buildCodegen({
553+
await run('codegen', {
567554
root,
568555
source: path.resolve(root, source),
569556
output: path.resolve(root, output, 'typescript'),
570557
report,
571558
});
572559
break;
573560
case 'custom':
574-
await customTarget({
561+
await run('custom', {
575562
options: targetOptions,
576563
source: path.resolve(root, source),
577-
report,
578564
root,
565+
report,
579566
});
580567
break;
581568
default:

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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {
2+
Worker,
3+
isMainThread,
4+
parentPort,
5+
workerData,
6+
} from 'node:worker_threads';
7+
import type { Report, Target } from '../types';
8+
import commonjs from '../targets/commonjs';
9+
import module from '../targets/module';
10+
import typescript from '../targets/typescript';
11+
import codegen from '../targets/codegen';
12+
import custom from '../targets/custom';
13+
import kleur from 'kleur';
14+
15+
type WorkerData<T extends Target> = {
16+
target: T;
17+
data: Omit<Parameters<(typeof targets)[T]>[0], 'report'>;
18+
};
19+
20+
const targets = {
21+
commonjs,
22+
module,
23+
typescript,
24+
codegen,
25+
custom,
26+
} as const;
27+
28+
export const run = async <T extends Target>(
29+
target: T,
30+
{ report, ...data }: Parameters<(typeof targets)[T]>[0]
31+
) => {
32+
if (!isMainThread) {
33+
throw new Error('Worker can only be run from the main thread');
34+
}
35+
36+
const worker = new Worker(__filename, {
37+
workerData: {
38+
target,
39+
data,
40+
} satisfies WorkerData<T>,
41+
env: {
42+
...process.env,
43+
FORCE_COLOR: process.stdout.isTTY ? '1' : '0',
44+
},
45+
});
46+
47+
worker.on('message', (message) => {
48+
switch (message.type) {
49+
case 'info':
50+
report.info(message.message);
51+
break;
52+
case 'warn':
53+
report.warn(message.message);
54+
break;
55+
case 'error':
56+
report.error(message.message);
57+
break;
58+
case 'success':
59+
report.success(message.message);
60+
break;
61+
default:
62+
console.error('Unknown message type', message);
63+
}
64+
});
65+
66+
worker.on('error', (error) => {
67+
console.log(error);
68+
});
69+
70+
worker.on('exit', (code) => {
71+
if (code !== 0) {
72+
report.error(
73+
`Target ${kleur.blue(target)} exited with code ${kleur.red(code)}`
74+
);
75+
}
76+
});
77+
};
78+
79+
if (!isMainThread) {
80+
const { target, data } = workerData as WorkerData<Target>;
81+
82+
const report: Report = {
83+
info: (message) => parentPort?.postMessage({ type: 'info', message }),
84+
warn: (message) => parentPort?.postMessage({ type: 'warn', message }),
85+
error: (message) => parentPort?.postMessage({ type: 'error', message }),
86+
success: (message) => parentPort?.postMessage({ type: 'success', message }),
87+
};
88+
89+
if (target in targets) {
90+
// @ts-expect-error - typescript doesn't support correlated union types https://github.com/microsoft/TypeScript/issues/30581
91+
targets[target]({ ...data, report }).catch((error) => {
92+
console.log(error);
93+
process.exit(1);
94+
});
95+
} else {
96+
throw new Error(`Unknown target: ${target}`);
97+
}
98+
}

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)