-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: switch from child_process to worker_threads
- Remove unused import of `rewritePathsWithExposedFederatedModules` in `make-federated-types.ts` - Update `compileTypesAsync` function in `compileTypesAsync.ts` to accept a `loggerHint` parameter and use the `getLogger` function from `helpers` - Remove `compileWorker.js` file as it is no longer needed - Update `plugin.ts` to use the updated `compileTypesAsync` function and pass the `federationConfig` parameter
- Loading branch information
1 parent
3b13ae8
commit 7ea11b3
Showing
7 changed files
with
106 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,57 @@ | ||
import { type ChildProcess, fork } from 'node:child_process'; | ||
import path from 'node:path'; | ||
import { Worker, parentPort } from 'node:worker_threads'; | ||
|
||
import type { CompileTypesParams, CompileTypesResult } from './compileTypes'; | ||
import { getLogger } from '../helpers'; | ||
import type { CompileTypesParams } from './compileTypes'; | ||
import type { CompileTypesWorkerMessage } from './compileTypesWorker'; | ||
|
||
let currentWorker: ChildProcess | null = null; | ||
let worker: Worker | null = null; | ||
|
||
export function compileTypesAsync(params: CompileTypesParams, loggerHint = ''): Promise<void> { | ||
const logger = getLogger(); | ||
|
||
export function compileTypesAsync(params: CompileTypesParams): Promise<CompileTypesResult> { | ||
return new Promise((resolve, reject) => { | ||
if (currentWorker) { | ||
currentWorker.kill(); | ||
if (worker) { | ||
logger.log('Terminating existing worker process'); | ||
worker.terminate(); | ||
} | ||
|
||
const workerPath = path.join(__dirname, 'compileWorker.js'); | ||
currentWorker = fork(workerPath); | ||
|
||
currentWorker.on('message', (result: CompileTypesResult) => { | ||
resolve(result); | ||
currentWorker?.kill(); | ||
currentWorker = null; | ||
worker = new Worker(workerPath); | ||
|
||
worker.on('message', (result: CompileTypesWorkerMessage) => { | ||
switch (result.status) { | ||
case 'success': | ||
resolve(); | ||
break; | ||
case 'failure': | ||
logger.warn('[Worker]: Failed to compile types for exposed modules.', loggerHint); | ||
reject(new Error('Failed to compile types for exposed modules.')); | ||
break; | ||
case 'error': | ||
logger.warn('[Worker]: Error compiling types for exposed modules.', loggerHint); | ||
reject(result.error); | ||
break; | ||
} | ||
worker?.terminate(); | ||
worker = null; | ||
}); | ||
|
||
currentWorker.on('error', error => { | ||
worker.on('error', error => { | ||
logger.warn('[Worker]: Unexpected error.', loggerHint); | ||
logger.log(error); | ||
reject(error); | ||
currentWorker?.kill(); | ||
currentWorker = null; | ||
worker?.terminate(); | ||
worker = null; | ||
}); | ||
|
||
currentWorker.on('exit', code => { | ||
worker.on('exit', code => { | ||
if (code !== 0 && code !== null) { | ||
reject(new Error(`Worker process exited with code ${code}`)); | ||
reject(new Error(`[Worker]: Process exited with code ${code}`)); | ||
} | ||
currentWorker = null; | ||
worker = null; | ||
}); | ||
|
||
currentWorker.send(params); | ||
parentPort?.postMessage({ ...params, logger }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
|
||
import type { Compilation } from 'webpack'; | ||
import { type CompileTypesParams, compileTypes } from './compileTypes'; | ||
import { rewritePathsWithExposedFederatedModules } from './rewritePathsWithExposedFederatedModules'; | ||
|
||
type CompileTypesWorkerMessageError = { | ||
status: 'error'; | ||
error: Error; | ||
}; | ||
|
||
export type CompileTypesWorkerMessage = | ||
| { status: 'success' } | ||
| { status: 'failure' } | ||
| CompileTypesWorkerMessageError; | ||
|
||
parentPort?.on('message', (message: CompileTypesParams & { logger: Compilation['logger'] }) => { | ||
const { logger, ...params } = message; | ||
|
||
try { | ||
const startTime = performance.now(); | ||
const { isSuccess, typeDefinitions } = compileTypes(params); | ||
|
||
if (isSuccess) { | ||
const endTime = performance.now(); | ||
const timeTakenInSeconds = (endTime - startTime) / 1000; | ||
logger.log(`Types compilation completed in ${timeTakenInSeconds.toFixed(2)} seconds`); | ||
|
||
logger.log( | ||
`Replacing paths with names of exposed federate modules in typings file: ${params.outFile}`, | ||
); | ||
rewritePathsWithExposedFederatedModules( | ||
params.federationConfig, | ||
params.outFile, | ||
typeDefinitions, | ||
); | ||
|
||
parentPort?.postMessage({ status: 'success' } satisfies CompileTypesWorkerMessage); | ||
} else { | ||
parentPort?.postMessage({ status: 'failure' } satisfies CompileTypesWorkerMessage); | ||
} | ||
} catch (error) { | ||
parentPort?.postMessage({ | ||
status: 'error', | ||
error: error as Error, | ||
} satisfies CompileTypesWorkerMessageError); | ||
} | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters