Skip to content

Commit e49079d

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): remove jest-worker direct dependency
The worker pool for differential loading and i18n processing is now managed by the `piscina` dependency. This dependency is already used within the recently added JavaScript optimizer refactoring and reduces both the number of direct dependencies and amount of code to setup the worker pools.
1 parent 1ae7a57 commit e49079d

File tree

6 files changed

+20
-74
lines changed

6 files changed

+20
-74
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@
166166
"jasmine": "^3.3.1",
167167
"jasmine-core": "~3.7.0",
168168
"jasmine-spec-reporter": "~7.0.0",
169-
"jest-worker": "27.0.2",
170169
"jquery": "^3.3.1",
171170
"jsonc-parser": "3.0.0",
172171
"karma": "~6.3.0",

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ ts_library(
154154
"@npm//glob",
155155
"@npm//https-proxy-agent",
156156
"@npm//inquirer",
157-
"@npm//jest-worker",
158157
"@npm//karma",
159158
"@npm//karma-source-map-support",
160159
"@npm//less",

packages/angular_devkit/build_angular/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"glob": "7.1.7",
3939
"https-proxy-agent": "5.0.0",
4040
"inquirer": "8.1.1",
41-
"jest-worker": "27.0.2",
4241
"karma-source-map-support": "1.4.0",
4342
"less": "4.1.1",
4443
"less-loader": "10.0.0",

packages/angular_devkit/build_angular/src/utils/action-executor.ts

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,40 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Worker as JestWorker } from 'jest-worker';
10-
import * as os from 'os';
11-
import * as path from 'path';
12-
import { serialize } from 'v8';
9+
import Piscina from 'piscina';
1310
import { BundleActionCache } from './action-cache';
1411
import { maxWorkers } from './environment-options';
1512
import { I18nOptions } from './i18n-options';
1613
import { InlineOptions, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
1714

18-
let workerFile = require.resolve('./process-bundle');
19-
workerFile =
20-
path.extname(workerFile) === '.ts' ? require.resolve('./process-bundle-bootstrap') : workerFile;
15+
const workerFile = require.resolve('./process-bundle');
2116

2217
export class BundleActionExecutor {
23-
private largeWorker?: JestWorker;
24-
private smallWorker?: JestWorker;
18+
private workerPool?: Piscina;
2519
private cache?: BundleActionCache;
2620

2721
constructor(
2822
private workerOptions: { cachePath?: string; i18n: I18nOptions },
2923
integrityAlgorithm?: string,
30-
private readonly sizeThreshold = 32 * 1024,
3124
) {
3225
if (workerOptions.cachePath) {
3326
this.cache = new BundleActionCache(workerOptions.cachePath, integrityAlgorithm);
3427
}
3528
}
3629

37-
private static executeMethod<O>(worker: JestWorker, method: string, input: unknown): Promise<O> {
38-
return (worker as unknown as Record<string, (i: unknown) => Promise<O>>)[method](input);
39-
}
40-
41-
private ensureLarge(): JestWorker {
42-
if (this.largeWorker) {
43-
return this.largeWorker;
44-
}
45-
46-
// larger files are processed in a separate process to limit memory usage in the main process
47-
return (this.largeWorker = new JestWorker(workerFile, {
48-
exposedMethods: ['process', 'inlineLocales'],
49-
setupArgs: [[...serialize(this.workerOptions)]],
50-
numWorkers: maxWorkers,
51-
}));
52-
}
53-
54-
private ensureSmall(): JestWorker {
55-
if (this.smallWorker) {
56-
return this.smallWorker;
30+
private ensureWorkerPool(): Piscina {
31+
if (this.workerPool) {
32+
return this.workerPool;
5733
}
5834

59-
// small files are processed in a limited number of threads to improve speed
60-
// The limited number also prevents a large increase in memory usage for an otherwise short operation
61-
return (this.smallWorker = new JestWorker(workerFile, {
62-
exposedMethods: ['process', 'inlineLocales'],
63-
setupArgs: [this.workerOptions],
64-
numWorkers: os.cpus().length < 2 ? 1 : 2,
65-
enableWorkerThreads: true,
66-
}));
67-
}
35+
this.workerPool = new Piscina({
36+
filename: workerFile,
37+
name: 'process',
38+
workerData: this.workerOptions,
39+
maxThreads: maxWorkers,
40+
});
6841

69-
private executeAction<O>(method: string, action: { code: string }): Promise<O> {
70-
// code.length is not an exact byte count but close enough for this
71-
if (action.code.length > this.sizeThreshold) {
72-
return BundleActionExecutor.executeMethod<O>(this.ensureLarge(), method, action);
73-
} else {
74-
return BundleActionExecutor.executeMethod<O>(this.ensureSmall(), method, action);
75-
}
42+
return this.workerPool;
7643
}
7744

7845
async process(action: ProcessBundleOptions): Promise<ProcessBundleResult> {
@@ -89,7 +56,7 @@ export class BundleActionExecutor {
8956
} catch {}
9057
}
9158

92-
return this.executeAction<ProcessBundleResult>('process', action);
59+
return this.ensureWorkerPool().run(action, { name: 'process' });
9360
}
9461

9562
processAll(actions: Iterable<ProcessBundleOptions>): AsyncIterable<ProcessBundleResult> {
@@ -99,7 +66,7 @@ export class BundleActionExecutor {
9966
async inline(
10067
action: InlineOptions,
10168
): Promise<{ file: string; diagnostics: { type: string; message: string }[]; count: number }> {
102-
return this.executeAction('inlineLocales', action);
69+
return this.ensureWorkerPool().run(action, { name: 'inlineLocales' });
10370
}
10471

10572
inlineAll(actions: Iterable<InlineOptions>) {
@@ -129,15 +96,6 @@ export class BundleActionExecutor {
12996
}
13097

13198
stop(): void {
132-
// Floating promises are intentional here
133-
// https://github.com/facebook/jest/tree/56079a5aceacf32333089cea50c64385885fee26/packages/jest-worker#end
134-
if (this.largeWorker) {
135-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
136-
this.largeWorker.end();
137-
}
138-
if (this.smallWorker) {
139-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
140-
this.smallWorker.end();
141-
}
99+
void this.workerPool?.destroy();
142100
}
143101
}

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import * as fs from 'fs';
2323
import * as path from 'path';
2424
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map';
2525
import { minify } from 'terser';
26-
import * as v8 from 'v8';
2726
import { sources } from 'webpack';
27+
import { workerData } from 'worker_threads';
2828
import { allowMangle, allowMinify, shouldBeautify } from './environment-options';
2929
import { I18nOptions } from './i18n-options';
3030

@@ -78,16 +78,7 @@ export const enum CacheKey {
7878
DownlevelMap = 3,
7979
}
8080

81-
let cachePath: string | undefined;
82-
let i18n: I18nOptions | undefined;
83-
84-
export function setup(data: number[] | { cachePath: string; i18n: I18nOptions }): void {
85-
const options = Array.isArray(data)
86-
? (v8.deserialize(Buffer.from(data)) as { cachePath: string; i18n: I18nOptions })
87-
: data;
88-
cachePath = options.cachePath;
89-
i18n = options.i18n;
90-
}
81+
const { cachePath, i18n } = (workerData || {}) as { cachePath?: string; i18n?: I18nOptions };
9182

9283
async function cachePut(
9384
content: string,
@@ -413,7 +404,7 @@ async function terserMangle(
413404
code,
414405
options.map,
415406
outputCode,
416-
(minifyOutput.map as unknown) as RawSourceMap,
407+
minifyOutput.map as unknown as RawSourceMap,
417408
options.filename || '0',
418409
code.length > FAST_SOURCEMAP_THRESHOLD,
419410
);

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6694,7 +6694,7 @@ jasminewd2@^2.1.0:
66946694
resolved "https://registry.yarnpkg.com/jasminewd2/-/jasminewd2-2.2.0.tgz#e37cf0b17f199cce23bea71b2039395246b4ec4e"
66956695
integrity sha1-43zwsX8ZnM4jvqcbIDk5Uka07E4=
66966696

6697-
jest-worker@27.0.2, jest-worker@^27.0.2:
6697+
jest-worker@^27.0.2:
66986698
version "27.0.2"
66996699
resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-27.0.2.tgz#4ebeb56cef48b3e7514552f80d0d80c0129f0b05"
67006700
integrity sha512-EoBdilOTTyOgmHXtw/cPc+ZrCA0KJMrkXzkrPGNwLmnvvlN1nj7MPrxpT7m+otSv2e1TLaVffzDnE/LB14zJMg==

0 commit comments

Comments
 (0)