-
Notifications
You must be signed in to change notification settings - Fork 633
[heft-typescript@next] Add "useTranspilerWorker" option #4120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iclanton
merged 4 commits into
microsoft:release/heft-0.50.0-rc
from
dmichon-msft:typescript-split-transpile
May 22, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 +1,3 @@ | ||
console.log('dostuff'); | ||
|
||
export {}; |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -55,5 +55,7 @@ | |
// "excludeGlobs": [ | ||
// "some/path/*.css" | ||
// ] | ||
} | ||
}, | ||
|
||
"useTranspilerWorker": true | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 +1,3 @@ | ||
console.log('dostuff'); | ||
|
||
export {}; |
This file contains hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
heft-plugins/heft-typescript-plugin/src/TranspilerWorker.ts
This file contains hidden or 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,104 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
import { parentPort, workerData } from 'node:worker_threads'; | ||
|
||
import type * as TTypescript from 'typescript'; | ||
import type { | ||
ITranspilationErrorMessage, | ||
ITranspilationRequestMessage, | ||
ITranspilationSuccessMessage, | ||
ITypescriptWorkerData | ||
} from './types'; | ||
import type { ExtendedTypeScript } from './internalTypings/TypeScriptInternals'; | ||
import { configureProgramForMultiEmit } from './configureProgramForMultiEmit'; | ||
|
||
const typedWorkerData: ITypescriptWorkerData = workerData; | ||
|
||
const ts: ExtendedTypeScript = require(typedWorkerData.typeScriptToolPath); | ||
dmichon-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function handleMessage(message: ITranspilationRequestMessage | false): void { | ||
if (!message) { | ||
process.exit(0); | ||
dmichon-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
try { | ||
const response: ITranspilationSuccessMessage = runTranspiler(message); | ||
parentPort!.postMessage(response); | ||
} catch (err) { | ||
const errorResponse: ITranspilationErrorMessage = { | ||
requestId: message.requestId, | ||
type: 'error', | ||
result: { | ||
message: err.message, | ||
...Object.fromEntries(Object.entries(err)) | ||
} | ||
}; | ||
parentPort!.postMessage(errorResponse); | ||
} | ||
} | ||
|
||
function runTranspiler(message: ITranspilationRequestMessage): ITranspilationSuccessMessage { | ||
const { requestId, compilerOptions, moduleKindsToEmit, fileNames } = message; | ||
|
||
const fullySkipTypeCheck: boolean = | ||
compilerOptions.importsNotUsedAsValues === ts.ImportsNotUsedAsValues.Error; | ||
|
||
for (const [option, value] of Object.entries(ts.getDefaultCompilerOptions())) { | ||
if (compilerOptions[option] === undefined) { | ||
compilerOptions[option] = value; | ||
} | ||
} | ||
|
||
const { target: rawTarget } = compilerOptions; | ||
|
||
for (const option of ts.transpileOptionValueCompilerOptions) { | ||
compilerOptions[option.name] = option.transpileOptionValue; | ||
} | ||
|
||
compilerOptions.suppressOutputPathCheck = true; | ||
compilerOptions.skipDefaultLibCheck = true; | ||
compilerOptions.preserveValueImports = true; | ||
|
||
const sourceFileByPath: Map<string, TTypescript.SourceFile> = new Map(); | ||
|
||
for (const fileName of fileNames) { | ||
const sourceText: string | undefined = ts.sys.readFile(fileName); | ||
if (sourceText) { | ||
const sourceFile: TTypescript.SourceFile = ts.createSourceFile(fileName, sourceText, rawTarget!); | ||
sourceFile.hasNoDefaultLib = fullySkipTypeCheck; | ||
sourceFileByPath.set(fileName, sourceFile); | ||
} | ||
} | ||
|
||
const newLine: string = ts.getNewLineCharacter(compilerOptions); | ||
|
||
const compilerHost: TTypescript.CompilerHost = { | ||
getSourceFile: (fileName: string) => sourceFileByPath.get(fileName), | ||
writeFile: ts.sys.writeFile, | ||
getDefaultLibFileName: () => 'lib.d.ts', | ||
useCaseSensitiveFileNames: () => true, | ||
getCanonicalFileName: (fileName: string) => fileName, | ||
getCurrentDirectory: () => '', | ||
getNewLine: () => newLine, | ||
fileExists: (fileName: string) => sourceFileByPath.has(fileName), | ||
readFile: () => '', | ||
directoryExists: () => true, | ||
getDirectories: () => [] | ||
}; | ||
|
||
const program: TTypescript.Program = ts.createProgram(fileNames, compilerOptions, compilerHost); | ||
|
||
configureProgramForMultiEmit(program, ts, moduleKindsToEmit, 'transpile'); | ||
|
||
const result: TTypescript.EmitResult = program.emit(undefined, undefined, undefined, undefined, undefined); | ||
|
||
const response: ITranspilationSuccessMessage = { | ||
requestId, | ||
type: 'success', | ||
result | ||
}; | ||
|
||
return response; | ||
} | ||
|
||
parentPort!.on('message', handleMessage); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.