-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtypescript-builder.ts
More file actions
128 lines (115 loc) · 4.26 KB
/
typescript-builder.ts
File metadata and controls
128 lines (115 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {BuildOutput, SampleFile} from '../shared/worker-api.js';
import {TypesFetcher} from './types-fetcher.js';
import {PackageJson} from './util.js';
import {makeLspDiagnostic} from './diagnostic.js';
import {WorkerContext} from './worker-context.js';
const PROCESSED_FILE_ENDINGS = ['.ts', '.jsx', '.tsx'];
export async function* processTypeScriptFiles(
workerContext: WorkerContext,
results: AsyncIterable<BuildOutput> | Iterable<BuildOutput>
): AsyncIterable<BuildOutput> {
// Instantiate langservice variables for ease of access
const langService = workerContext.languageServiceContext.service;
const langServiceHost = workerContext.languageServiceContext.serviceHost;
let packageJson: PackageJson | undefined;
const compilerInputs = [];
for await (const result of results) {
if (result.kind !== 'file') continue;
// Collect filetypes that need to be compiled. They will be handled later on in the process.
if (
PROCESSED_FILE_ENDINGS.some((ending) => result.file.name.endsWith(ending))
) {
compilerInputs.push(result.file);
continue;
}
// Everything that reaches this point should be usable out of the box without compiling.
// Therefore we can just yield the result to the caller.
yield result;
// Even though we don't need to compile javascript files, we want to append them to our
// compilerinputs to get completions and diagnostics for those files.
if (result.file.name.endsWith('.js')) {
compilerInputs.push(result.file);
continue;
}
if (result.file.name === 'package.json') {
try {
packageJson = JSON.parse(result.file.content) as PackageJson;
} catch (e) {
// A bit hacky, but BareModuleTransformer already emits a diagnostic
// for this case, so we don't need another one.
}
}
}
if (compilerInputs.length === 0) {
return;
}
// Immediately resolve local project files, and begin fetching types (but
// don't wait for them).
const loadedFiles = new Map<string, string>();
const inputFiles = compilerInputs.map((file) => ({
file,
url: new URL(file.name, self.origin).href,
}));
for (const {file, url} of inputFiles) {
loadedFiles.set(url, file.content);
}
// Sync the new loaded files with the servicehost.
// If the file is missing, it's added, if the file is modified,
// the modification data and versioning will be handled by the servicehost.
// If a file is removed, it will be removed from the file list
langServiceHost.sync(loadedFiles);
const program = langService.getProgram();
if (program === undefined) {
throw new Error('Unexpected error: program was undefined');
}
for (const {file, url} of inputFiles) {
for (const tsDiagnostic of langService.getSyntacticDiagnostics(url)) {
yield {
kind: 'diagnostic',
filename: file.name,
diagnostic: makeLspDiagnostic(tsDiagnostic),
};
}
const sourceFile = program.getSourceFile(url);
let compiled: SampleFile | undefined = undefined;
program!.emit(sourceFile, (url, content) => {
compiled = {
name: new URL(url).pathname.slice(1),
content,
contentType: 'text/javascript',
};
});
if (compiled !== undefined) {
yield {kind: 'file', file: compiled};
}
}
// Wait for all typings to be fetched, and then retrieve slower semantic
// diagnostics.
const typings = await TypesFetcher.fetchTypes(
workerContext.cdn,
workerContext.importMapResolver,
packageJson,
inputFiles.map((file) => file.file.content),
workerContext.languageServiceContext.compilerOptions.lib
);
for (const [path, content] of typings.files) {
// TypeScript is going to look for these files as paths relative to our
// source files, so we need to add them to our filesystem with those URLs.
const url = new URL(`node_modules/${path}`, self.origin).href;
langServiceHost.updateFileContentIfNeeded(url, content);
}
for (const {file, url} of inputFiles) {
for (const tsDiagnostic of langService.getSemanticDiagnostics(url)) {
yield {
kind: 'diagnostic',
filename: file.name,
diagnostic: makeLspDiagnostic(tsDiagnostic),
};
}
}
}