-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(build): Use WebWorker when removing private fields
- Loading branch information
Showing
4 changed files
with
136 additions
and
56 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as ts from 'typescript' | ||
|
||
type UUID = number // ReturnType<typeof crypto.randomUUID> | ||
|
||
export type WorkerInput = { | ||
file: string | ||
taskId: UUID | ||
} | ||
|
||
export type WorkerOutput = | ||
| { | ||
type: 'success' | ||
value: string | ||
taskId: UUID | ||
} | ||
| { | ||
type: 'error' | ||
value: unknown | ||
taskId: UUID | ||
} | ||
|
||
const removePrivateTransformer = <T extends ts.Node>(ctx: ts.TransformationContext) => { | ||
const visit: ts.Visitor = (node) => { | ||
if (ts.isClassDeclaration(node)) { | ||
const newMembers = node.members.filter((elem) => { | ||
if (ts.isPropertyDeclaration(elem) || ts.isMethodDeclaration(elem)) { | ||
for (const modifier of elem.modifiers ?? []) { | ||
if (modifier.kind === ts.SyntaxKind.PrivateKeyword) { | ||
return false | ||
} | ||
} | ||
} | ||
if (elem.name && ts.isPrivateIdentifier(elem.name)) { | ||
return false | ||
} | ||
return true | ||
}) | ||
return ts.factory.createClassDeclaration( | ||
node.modifiers, | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
newMembers | ||
) | ||
} | ||
return ts.visitEachChild(node, visit, ctx) | ||
} | ||
|
||
return (node: T) => { | ||
const visited = ts.visitNode(node, visit) | ||
if (!visited) { | ||
throw new Error('The result visited is undefined.') | ||
} | ||
return visited | ||
} | ||
} | ||
|
||
export const removePrivateFields = (tsPath: string) => { | ||
const program = ts.createProgram([tsPath], { | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.ESNext, | ||
}) | ||
const file = program.getSourceFile(tsPath) | ||
|
||
const transformed = ts.transform(file!, [removePrivateTransformer]) | ||
const printer = ts.createPrinter() | ||
const transformedSourceFile = transformed.transformed[0] as ts.SourceFile | ||
const code = printer.printFile(transformedSourceFile) | ||
transformed.dispose() | ||
return code | ||
} | ||
|
||
declare const self: Worker | ||
|
||
if (globalThis.self) { | ||
self.addEventListener('message', function (e) { | ||
const { file, taskId } = e.data as WorkerInput | ||
|
||
try { | ||
const result = removePrivateFields(file) | ||
self.postMessage({ type: 'success', value: result, taskId } satisfies WorkerOutput) | ||
} catch (e) { | ||
console.error(e) | ||
self.postMessage({ type: 'error', value: e, taskId } satisfies WorkerOutput) | ||
} | ||
}) | ||
} |
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,52 +1,41 @@ | ||
import * as ts from 'typescript' | ||
import { cpus } from 'node:os' | ||
import type { WorkerInput, WorkerOutput } from './remove-private-fields-worker' | ||
|
||
const removePrivateTransformer = <T extends ts.Node>(ctx: ts.TransformationContext) => { | ||
const visit: ts.Visitor = (node) => { | ||
if (ts.isClassDeclaration(node)) { | ||
const newMembers = node.members.filter((elem) => { | ||
if (ts.isPropertyDeclaration(elem) || ts.isMethodDeclaration(elem)) { | ||
for (const modifier of elem.modifiers ?? []) { | ||
if (modifier.kind === ts.SyntaxKind.PrivateKeyword) { | ||
return false | ||
} | ||
} | ||
} | ||
if (elem.name && ts.isPrivateIdentifier(elem.name)) { | ||
return false | ||
} | ||
return true | ||
}) | ||
return ts.factory.createClassDeclaration( | ||
node.modifiers, | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
newMembers | ||
) | ||
} | ||
return ts.visitEachChild(node, visit, ctx) | ||
} | ||
const workers = Array.from({ length: Math.ceil(cpus().length / 2) }).map( | ||
() => new Worker(`${import.meta.dirname}/remove-private-fields-worker.ts`), | ||
{ type: 'module' } | ||
) | ||
let workerIndex = 0 | ||
let taskId = 0 | ||
|
||
return (node: T) => { | ||
const visited = ts.visitNode(node, visit) | ||
if (!visited) { | ||
throw new Error('The result visited is undefined.') | ||
} | ||
return visited | ||
} | ||
} | ||
export async function removePrivateFields(file: string): Promise<string> { | ||
const currentTaskId = taskId++ | ||
const worker = workers[workerIndex] | ||
workerIndex = (workerIndex + 1) % workers.length | ||
|
||
export const removePrivateFields = (tsPath: string) => { | ||
const program = ts.createProgram([tsPath], { | ||
target: ts.ScriptTarget.ESNext, | ||
module: ts.ModuleKind.ESNext, | ||
return new Promise<string>((resolve, reject) => { | ||
const abortController = new AbortController() | ||
worker.addEventListener( | ||
'message', | ||
({ data: { type, value, taskId } }: { data: WorkerOutput }) => { | ||
if (taskId === currentTaskId) { | ||
if (type === 'success') { | ||
resolve(value) | ||
} else { | ||
reject(value) | ||
} | ||
|
||
abortController.abort() | ||
} | ||
}, | ||
{ signal: abortController.signal } | ||
) | ||
worker.postMessage({ file, taskId: currentTaskId } satisfies WorkerInput) | ||
}) | ||
const file = program.getSourceFile(tsPath) | ||
} | ||
|
||
const transformed = ts.transform(file!, [removePrivateTransformer]) | ||
const printer = ts.createPrinter() | ||
const transformedSourceFile = transformed.transformed[0] as ts.SourceFile | ||
const code = printer.printFile(transformedSourceFile) | ||
transformed.dispose() | ||
return code | ||
export function cleanupWorkers() { | ||
for (const worker of workers) { | ||
worker.terminate() | ||
} | ||
} |