-
Notifications
You must be signed in to change notification settings - Fork 12k
/
compiler_host.ts
462 lines (390 loc) · 12.9 KB
/
compiler_host.ts
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Path, getSystemPath, isAbsolute, join, normalize, virtualFs } from '@angular-devkit/core';
import { Stats } from 'fs';
import * as ts from 'typescript';
import { NgccProcessor } from './ngcc_processor';
import { WebpackResourceLoader } from './resource_loader';
import { forwardSlashPath, workaroundResolve } from './utils';
export interface OnErrorFn {
(message: string): void;
}
const dev = Math.floor(Math.random() * 10000);
export class WebpackCompilerHost implements ts.CompilerHost {
private _syncHost: virtualFs.SyncDelegateHost;
private _innerMemoryHost: virtualFs.SimpleMemoryHost;
private _memoryHost: virtualFs.SyncDelegateHost;
private _changedFiles = new Set<string>();
private _readResourceFiles = new Set<string>();
private _basePath: Path;
private _resourceLoader?: WebpackResourceLoader;
private _sourceFileCache = new Map<string, ts.SourceFile>();
private _virtualFileExtensions = [
'.js',
'.js.map',
'.ngfactory.js',
'.ngfactory.js.map',
'.ngsummary.json',
];
private _virtualStyleFileExtensions = [
'.shim.ngstyle.js',
'.shim.ngstyle.js.map',
];
constructor(
private _options: ts.CompilerOptions,
basePath: string,
host: virtualFs.Host,
private readonly cacheSourceFiles: boolean,
private readonly directTemplateLoading = false,
private readonly ngccProcessor?: NgccProcessor,
private readonly moduleResolutionCache?: ts.ModuleResolutionCache,
) {
this._syncHost = new virtualFs.SyncDelegateHost(host);
this._innerMemoryHost = new virtualFs.SimpleMemoryHost();
this._memoryHost = new virtualFs.SyncDelegateHost(this._innerMemoryHost);
this._basePath = normalize(basePath);
}
private get virtualFiles(): Path[] {
return [...((this._memoryHost.delegate as {}) as { _cache: Map<Path, {}> })._cache.keys()];
}
reset() {
this._innerMemoryHost.reset();
this._changedFiles.clear();
this._readResourceFiles.clear();
this._sourceFileCache.clear();
this._resourceLoader = undefined;
}
denormalizePath(path: string) {
return getSystemPath(normalize(path));
}
resolve(path: string): Path {
const p = normalize(path);
if (isAbsolute(p)) {
return p;
} else {
return join(this._basePath, p);
}
}
resetChangedFileTracker() {
this._changedFiles.clear();
}
getChangedFilePaths(): string[] {
return [...this._changedFiles];
}
getNgFactoryPaths(): string[] {
return (
this.virtualFiles
.filter(fileName => fileName.endsWith('.ngfactory.js') || fileName.endsWith('.ngstyle.js'))
// These paths are used by the virtual file system decorator so we must denormalize them.
.map(path => this.denormalizePath(path))
);
}
invalidate(fileName: string): void {
const fullPath = this.resolve(fileName);
this._sourceFileCache.delete(fullPath);
if (this.ngccProcessor) {
// Delete the ngcc processor cache using the TS-format file names.
this.ngccProcessor.invalidate(forwardSlashPath(fileName));
}
let exists = false;
try {
exists = this._syncHost.isFile(fullPath);
if (exists) {
this._changedFiles.add(workaroundResolve(fullPath));
}
} catch {}
// File doesn't exist anymore and is not a factory, so we should delete the related
// virtual files.
if (
!exists &&
fullPath.endsWith('.ts') &&
!(fullPath.endsWith('.ngfactory.ts') || fullPath.endsWith('.shim.ngstyle.ts'))
) {
this._virtualFileExtensions.forEach(ext => {
const virtualFile = (fullPath.slice(0, -3) + ext) as Path;
if (this._memoryHost.exists(virtualFile)) {
this._memoryHost.delete(virtualFile);
}
});
}
if (fullPath.endsWith('.ts')) {
return;
}
// In case resolveJsonModule and allowJs we also need to remove virtual emitted files
// both if they exists or not.
if (
(fullPath.endsWith('.js') || fullPath.endsWith('.json')) &&
!/(\.(ngfactory|ngstyle)\.js|ngsummary\.json)$/.test(fullPath)
) {
if (this._memoryHost.exists(fullPath)) {
this._memoryHost.delete(fullPath);
}
return;
}
if (!exists) {
// At this point we're only looking at resource files (html/css/scss/etc).
// If the original was deleted, we should delete the virtual files too.
// If the original wasn't deleted we should leave them to be overwritten, because webpack
// might begin the loading process before our plugin has re-emitted them.
for (const ext of this._virtualStyleFileExtensions) {
const virtualFile = (fullPath + ext) as Path;
if (this._memoryHost.exists(virtualFile)) {
this._memoryHost.delete(virtualFile);
}
}
}
}
fileExists(fileName: string, delegate = true): boolean {
const p = this.resolve(fileName);
if (this._memoryHost.isFile(p)) {
return true;
}
if (!delegate) {
return false;
}
let exists = false;
try {
exists = this._syncHost.isFile(p);
} catch {}
return exists;
}
readFile(fileName: string): string | undefined {
const filePath = this.resolve(fileName);
try {
let content: ArrayBuffer;
if (this._memoryHost.isFile(filePath)) {
content = this._memoryHost.read(filePath);
} else {
content = this._syncHost.read(filePath);
}
// strip BOM
return virtualFs.fileBufferToString(content).replace(/^\uFEFF/, '');
} catch {
return undefined;
}
}
readFileBuffer(fileName: string): Buffer {
const filePath = this.resolve(fileName);
if (this._memoryHost.isFile(filePath)) {
return Buffer.from(this._memoryHost.read(filePath));
} else {
const content = this._syncHost.read(filePath);
return Buffer.from(content);
}
}
private _makeStats(stats: virtualFs.Stats<Partial<Stats>>): Stats {
return {
isFile: () => stats.isFile(),
isDirectory: () => stats.isDirectory(),
isBlockDevice: () => (stats.isBlockDevice && stats.isBlockDevice()) || false,
isCharacterDevice: () => (stats.isCharacterDevice && stats.isCharacterDevice()) || false,
isFIFO: () => (stats.isFIFO && stats.isFIFO()) || false,
isSymbolicLink: () => (stats.isSymbolicLink && stats.isSymbolicLink()) || false,
isSocket: () => (stats.isSocket && stats.isSocket()) || false,
dev: stats.dev === undefined ? dev : stats.dev,
ino: stats.ino === undefined ? Math.floor(Math.random() * 100000) : stats.ino,
mode: stats.mode === undefined ? parseInt('777', 8) : stats.mode,
nlink: stats.nlink === undefined ? 1 : stats.nlink,
uid: stats.uid || 0,
gid: stats.gid || 0,
rdev: stats.rdev || 0,
size: stats.size,
blksize: stats.blksize === undefined ? 512 : stats.blksize,
blocks: stats.blocks === undefined ? Math.ceil(stats.size / 512) : stats.blocks,
atime: stats.atime,
atimeMs: stats.atime.getTime(),
mtime: stats.mtime,
mtimeMs: stats.mtime.getTime(),
ctime: stats.ctime,
ctimeMs: stats.ctime.getTime(),
birthtime: stats.birthtime,
birthtimeMs: stats.birthtime.getTime(),
};
}
stat(path: string): Stats | null {
const p = this.resolve(path);
let stats: virtualFs.Stats<Partial<Stats>> | Stats | null = null;
try {
stats = this._memoryHost.stat(p) || this._syncHost.stat(p);
} catch {}
if (!stats) {
return null;
}
if (stats instanceof Stats) {
return stats;
}
return this._makeStats(stats);
}
directoryExists(directoryName: string): boolean {
const p = this.resolve(directoryName);
try {
return this._memoryHost.isDirectory(p) || this._syncHost.isDirectory(p);
} catch {
return false;
}
}
getDirectories(path: string): string[] {
const p = this.resolve(path);
let delegated: string[];
try {
delegated = this._syncHost.list(p).filter(x => {
try {
return this._syncHost.isDirectory(join(p, x));
} catch {
return false;
}
});
} catch {
delegated = [];
}
let memory: string[];
try {
memory = this._memoryHost.list(p).filter(x => {
try {
return this._memoryHost.isDirectory(join(p, x));
} catch {
return false;
}
});
} catch {
memory = [];
}
return [...new Set([...delegated, ...memory])];
}
getSourceFile(fileName: string, languageVersion: ts.ScriptTarget, onError?: OnErrorFn) {
const p = this.resolve(fileName);
try {
const cached = this._sourceFileCache.get(p);
if (cached) {
return cached;
}
const content = this.readFile(fileName);
if (content !== undefined) {
const sf = ts.createSourceFile(workaroundResolve(fileName), content, languageVersion, true);
if (this.cacheSourceFiles) {
this._sourceFileCache.set(p, sf);
}
return sf;
}
} catch (e) {
if (onError) {
onError(e.message);
}
}
return undefined;
}
getDefaultLibFileName(options: ts.CompilerOptions) {
return ts.createCompilerHost(options).getDefaultLibFileName(options);
}
// This is due to typescript CompilerHost interface being weird on writeFile. This shuts down
// typings in WebStorm.
get writeFile() {
return (
fileName: string,
data: string,
_writeByteOrderMark: boolean,
onError?: (message: string) => void,
_sourceFiles?: ReadonlyArray<ts.SourceFile>,
): void => {
const p = this.resolve(fileName);
try {
this._memoryHost.write(p, virtualFs.stringToFileBuffer(data));
} catch (e) {
if (onError) {
onError(e.message);
}
}
};
}
getCurrentDirectory(): string {
return workaroundResolve(this._basePath);
}
getCanonicalFileName(fileName: string): string {
const path = workaroundResolve(this.resolve(fileName));
return this.useCaseSensitiveFileNames() ? path : path.toLowerCase();
}
useCaseSensitiveFileNames(): boolean {
return !process.platform.startsWith('win32');
}
getNewLine(): string {
return '\n';
}
setResourceLoader(resourceLoader: WebpackResourceLoader) {
this._resourceLoader = resourceLoader;
}
readResource(fileName: string) {
// These paths are meant to be used by the loader so we must denormalize them
const denormalizedFileName = workaroundResolve(fileName);
this._readResourceFiles.add(denormalizedFileName);
if (this.directTemplateLoading && (fileName.endsWith('.html') || fileName.endsWith('.svg'))) {
return this.readFile(fileName);
}
if (this._resourceLoader) {
return this._resourceLoader.get(denormalizedFileName);
} else {
return this.readFile(fileName);
}
}
getModifiedResourceFiles(): Set<string> {
const modifiedFiles = new Set<string>();
for (const changedFile of this._changedFiles) {
const denormalizedFileName = workaroundResolve(changedFile);
if (this._readResourceFiles.has(denormalizedFileName)) {
modifiedFiles.add(denormalizedFileName);
}
if (!this._resourceLoader) {
continue;
}
for (const resourcePath of this._resourceLoader.getAffectedResources(denormalizedFileName)) {
modifiedFiles.add(resourcePath);
}
}
return modifiedFiles;
}
trace(message: string) {
// tslint:disable-next-line: no-console
console.log(message);
}
resolveModuleNames(
moduleNames: string[],
containingFile: string,
): (ts.ResolvedModule | undefined)[] {
return moduleNames.map(moduleName => {
const { resolvedModule } = ts.resolveModuleName(
moduleName,
workaroundResolve(containingFile),
this._options,
this,
this.moduleResolutionCache,
);
if (this._options.enableIvy && resolvedModule && this.ngccProcessor) {
this.ngccProcessor.processModule(moduleName, resolvedModule);
}
return resolvedModule;
});
}
resolveTypeReferenceDirectives(
typeReferenceDirectiveNames: string[],
containingFile: string,
redirectedReference?: ts.ResolvedProjectReference,
): (ts.ResolvedTypeReferenceDirective | undefined)[] {
return typeReferenceDirectiveNames.map(moduleName => {
const { resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(
moduleName,
workaroundResolve(containingFile),
this._options,
this,
redirectedReference,
);
if (this._options.enableIvy && resolvedTypeReferenceDirective && this.ngccProcessor) {
this.ngccProcessor.processModule(moduleName, resolvedTypeReferenceDirective);
}
return resolvedTypeReferenceDirective;
});
}
}