-
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.
- Added a new interface `CommonLogger` to define common logging methods. - Implemented `workerLogger` object that uses `sendLog` function to send log messages to the parent thread.
- Loading branch information
1 parent
438ca25
commit ea5f3e5
Showing
14 changed files
with
143 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,61 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
|
||
import type { Compilation } from 'webpack'; | ||
import type { FederationConfig } from '../models'; | ||
import { type CompileTypesParams, compileTypes } from './compileTypes'; | ||
import { rewritePathsWithExposedFederatedModules } from './rewritePathsWithExposedFederatedModules'; | ||
import { sendLog, workerLogger } from './workerLogger'; | ||
|
||
type CompileTypesWorkerMessageError = { | ||
export type LogLevel = keyof Pick<Compilation['logger'], 'log' | 'info' | 'warn' | 'error'>; | ||
|
||
type CompileTypesWorkerResultMessageError = { | ||
status: 'error'; | ||
error: Error; | ||
}; | ||
|
||
export type CompileTypesWorkerMessage = | ||
export type CompileTypesWorkerMessage = CompileTypesParams & { | ||
federationConfig: FederationConfig; | ||
}; | ||
|
||
export type CompileTypesWorkerResultMessage = | ||
| { status: 'success' } | ||
| { status: 'failure' } | ||
| CompileTypesWorkerMessageError; | ||
|
||
parentPort?.on('message', (message: CompileTypesParams & { logger: Compilation['logger'] }) => { | ||
const { logger, ...params } = message; | ||
| CompileTypesWorkerResultMessageError | ||
| { status: 'log'; level: LogLevel; message: string }; | ||
|
||
parentPort?.on('message', ({ federationConfig, ...params }: CompileTypesWorkerMessage) => { | ||
try { | ||
const startTime = performance.now(); | ||
const { isSuccess, typeDefinitions } = compileTypes(params); | ||
let startTime = performance.now(); | ||
const { isSuccess, typeDefinitions } = compileTypes(params, workerLogger); | ||
|
||
if (isSuccess) { | ||
const endTime = performance.now(); | ||
const timeTakenInSeconds = (endTime - startTime) / 1000; | ||
logger.log(`Types compilation completed in ${timeTakenInSeconds.toFixed(2)} seconds`); | ||
let endTime = performance.now(); | ||
let timeTakenInSeconds = (endTime - startTime) / 1000; | ||
sendLog('log', `Types compilation completed in ${timeTakenInSeconds.toFixed(2)} seconds`); | ||
|
||
logger.log( | ||
sendLog( | ||
'log', | ||
`Replacing paths with names of exposed federate modules in typings file: ${params.outFile}`, | ||
); | ||
startTime = performance.now(); | ||
rewritePathsWithExposedFederatedModules( | ||
params.federationConfig, | ||
federationConfig, | ||
params.outFile, | ||
typeDefinitions, | ||
workerLogger, | ||
); | ||
endTime = performance.now(); | ||
timeTakenInSeconds = (endTime - startTime) / 1000; | ||
sendLog('log', `Typings file rewritten in ${timeTakenInSeconds.toFixed(2)} seconds`); | ||
|
||
parentPort?.postMessage({ status: 'success' } satisfies CompileTypesWorkerMessage); | ||
parentPort?.postMessage({ status: 'success' } satisfies CompileTypesWorkerResultMessage); | ||
} else { | ||
parentPort?.postMessage({ status: 'failure' } satisfies CompileTypesWorkerMessage); | ||
parentPort?.postMessage({ status: 'failure' } satisfies CompileTypesWorkerResultMessage); | ||
} | ||
} catch (error) { | ||
parentPort?.postMessage({ | ||
status: 'error', | ||
error: error as Error, | ||
} satisfies CompileTypesWorkerMessageError); | ||
} satisfies CompileTypesWorkerResultMessageError); | ||
} | ||
}); |
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
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,15 +1,15 @@ | ||
import ts from 'typescript'; | ||
|
||
import { getLogger } from '../../helpers'; | ||
import type { CommonLogger } from '../../models'; | ||
|
||
export function reportCompileDiagnostic(diagnostic: ts.Diagnostic): void { | ||
const logger = getLogger(); | ||
export function reportCompileDiagnostic( | ||
diagnostic: ts.Diagnostic, | ||
logger: CommonLogger = getLogger(), | ||
): void { | ||
const { line } = diagnostic.file!.getLineAndCharacterOfPosition(diagnostic.start!); | ||
logger.log( | ||
'TS Error', | ||
diagnostic.code, | ||
':', | ||
ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine), | ||
`TS Error ${diagnostic.code}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine)}`, | ||
); | ||
logger.log(' at', `${diagnostic.file!.fileName}:${line + 1}`, '\n'); | ||
logger.log(` at ${diagnostic.file!.fileName}:${line + 1}`); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
|
||
import type { CommonLogger } from '../models'; | ||
import type { LogLevel } from './compileTypesWorker'; | ||
|
||
export function sendLog(level: LogLevel, message: string) { | ||
parentPort?.postMessage({ status: 'log', level, message }); | ||
} | ||
|
||
export const workerLogger: CommonLogger = { | ||
log: (message: string) => sendLog('log', message), | ||
info: (message: string) => sendLog('info', message), | ||
warn: (message: string) => sendLog('warn', message), | ||
error: (message: string) => sendLog('error', message), | ||
}; |
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,6 @@ | ||
export type CommonLogger = { | ||
log: (message: string) => void; | ||
info: (message: string) => void; | ||
warn: (message: string) => void; | ||
error: (message: string) => void; | ||
}; |
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
Oops, something went wrong.