forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_host.ts
323 lines (269 loc) · 9.84 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
import * as ts from 'typescript';
import {basename, dirname, join} from 'path';
import * as fs from 'fs';
export interface OnErrorFn {
(message: string): void;
}
const dev = Math.floor(Math.random() * 10000);
export class VirtualStats implements fs.Stats {
protected _ctime = new Date();
protected _mtime = new Date();
protected _atime = new Date();
protected _btime = new Date();
protected _dev = dev;
protected _ino = Math.floor(Math.random() * 100000);
protected _mode = parseInt('777', 8); // RWX for everyone.
protected _uid = process.env['UID'] || 0;
protected _gid = process.env['GID'] || 0;
constructor(protected _path: string) {}
isFile() { return false; }
isDirectory() { return false; }
isBlockDevice() { return false; }
isCharacterDevice() { return false; }
isSymbolicLink() { return false; }
isFIFO() { return false; }
isSocket() { return false; }
get dev() { return this._dev; }
get ino() { return this._ino; }
get mode() { return this._mode; }
get nlink() { return 1; } // Default to 1 hard link.
get uid() { return this._uid; }
get gid() { return this._gid; }
get rdev() { return 0; }
get size() { return 0; }
get blksize() { return 512; }
get blocks() { return Math.ceil(this.size / this.blksize); }
get atime() { return this._atime; }
get mtime() { return this._mtime; }
get ctime() { return this._ctime; }
get birthtime() { return this._btime; }
}
export class VirtualDirStats extends VirtualStats {
constructor(_fileName: string) {
super(_fileName);
}
isDirectory() { return true; }
get size() { return 1024; }
}
export class VirtualFileStats extends VirtualStats {
private _sourceFile: ts.SourceFile;
constructor(_fileName: string, private _content: string) {
super(_fileName);
}
get content() { return this._content; }
set content(v: string) {
this._content = v;
this._mtime = new Date();
this._sourceFile = null;
}
setSourceFile(sourceFile: ts.SourceFile) {
this._sourceFile = sourceFile;
}
getSourceFile(languageVersion: ts.ScriptTarget, setParentNodes: boolean) {
if (!this._sourceFile) {
this._sourceFile = ts.createSourceFile(
this._path,
this._content,
languageVersion,
setParentNodes);
}
return this._sourceFile;
}
isFile() { return true; }
get size() { return this._content.length; }
}
export class WebpackCompilerHost implements ts.CompilerHost {
private _delegate: ts.CompilerHost;
private _files: {[path: string]: VirtualFileStats} = Object.create(null);
private _directories: {[path: string]: VirtualDirStats} = Object.create(null);
private _changedFiles: {[path: string]: boolean} = Object.create(null);
private _changedDirs: {[path: string]: boolean} = Object.create(null);
private _basePath: string;
private _setParentNodes: boolean;
private _cache = false;
constructor(private _options: ts.CompilerOptions, basePath: string) {
this._setParentNodes = true;
this._delegate = ts.createCompilerHost(this._options, this._setParentNodes);
this._basePath = this._normalizePath(basePath);
}
private _normalizePath(path: string) {
return path.replace(/\\/g, '/');
}
private _resolve(path: string) {
path = this._normalizePath(path);
if (path[0] == '.') {
return this._normalizePath(join(this.getCurrentDirectory(), path));
} else if (path[0] == '/' || path.match(/^\w:\//)) {
return path;
} else {
return this._normalizePath(join(this._basePath, path));
}
}
private _setFileContent(fileName: string, content: string) {
this._files[fileName] = new VirtualFileStats(fileName, content);
let p = dirname(fileName);
while (p && !this._directories[p]) {
this._directories[p] = new VirtualDirStats(p);
this._changedDirs[p] = true;
p = dirname(p);
}
this._changedFiles[fileName] = true;
}
get dirty() {
return Object.keys(this._changedFiles).length > 0;
}
enableCaching() {
this._cache = true;
}
populateWebpackResolver(resolver: any) {
const fs = resolver.fileSystem;
if (!this.dirty) {
return;
}
/**
* storageDataSetter is a temporary hack to address these two issues:
* - https://github.com/angular/angular-cli/issues/7113
* - https://github.com/angular/angular-cli/issues/7136
*
* This way we set values correctly in both a Map (enhanced-resove>=3.4.0) and
* object (enhanced-resolve >= 3.1.0 <3.4.0).
*
* The right solution is to create a virtual filesystem by decorating the filesystem,
* instead of injecting data into the private cache of the filesystem.
*
* Doing it the right way should fix other related bugs, but meanwhile we hack it since:
* - it's affecting a lot of users.
* - the real solution is non-trivial.
*/
function storageDataSetter(data: Map<string, any> | {[k: string]: any}, k: string, v: any) {
if (data instanceof Map) {
data.set(k, v);
} else {
data[k] = v;
}
}
const isWindows = process.platform.startsWith('win');
for (const fileName of this.getChangedFilePaths()) {
const stats = this._files[fileName];
if (stats) {
// If we're on windows, we need to populate with the proper path separator.
const path = isWindows ? fileName.replace(/\//g, '\\') : fileName;
// fs._statStorage.data[path] = [null, stats];
// fs._readFileStorage.data[path] = [null, stats.content];
storageDataSetter(fs._statStorage.data, path, [null, stats]);
storageDataSetter(fs._readFileStorage.data, path, [null, stats.content]);
} else {
// Support removing files as well.
const path = isWindows ? fileName.replace(/\//g, '\\') : fileName;
// fs._statStorage.data[path] = [new Error(), null];
// fs._readFileStorage.data[path] = [new Error(), null];
storageDataSetter(fs._statStorage.data, path, [new Error(), null]);
storageDataSetter(fs._readFileStorage.data, path, [new Error(), null]);
}
}
for (const dirName of Object.keys(this._changedDirs)) {
const stats = this._directories[dirName];
const dirs = this.getDirectories(dirName);
const files = this.getFiles(dirName);
// If we're on windows, we need to populate with the proper path separator.
const path = isWindows ? dirName.replace(/\//g, '\\') : dirName;
// fs._statStorage.data[path] = [null, stats];
// fs._readdirStorage.data[path] = [null, files.concat(dirs)];
storageDataSetter(fs._statStorage.data, path, [null, stats]);
storageDataSetter(fs._readFileStorage.data, path, [null, files.concat(dirs)]);
}
}
resetChangedFileTracker() {
this._changedFiles = Object.create(null);
this._changedDirs = Object.create(null);
}
getChangedFilePaths(): string[] {
return Object.keys(this._changedFiles);
}
invalidate(fileName: string): void {
fileName = this._resolve(fileName);
if (fileName in this._files) {
this._files[fileName] = null;
this._changedFiles[fileName] = true;
}
}
fileExists(fileName: string): boolean {
fileName = this._resolve(fileName);
return this._files[fileName] != null || this._delegate.fileExists(fileName);
}
readFile(fileName: string): string {
fileName = this._resolve(fileName);
if (this._files[fileName] == null) {
const result = this._delegate.readFile(fileName);
if (result !== undefined && this._cache) {
this._setFileContent(fileName, result);
return result;
} else {
return result;
}
}
return this._files[fileName].content;
}
directoryExists(directoryName: string): boolean {
directoryName = this._resolve(directoryName);
return (this._directories[directoryName] != null)
|| this._delegate.directoryExists(directoryName);
}
getFiles(path: string): string[] {
path = this._resolve(path);
return Object.keys(this._files)
.filter(fileName => dirname(fileName) == path)
.map(path => basename(path));
}
getDirectories(path: string): string[] {
path = this._resolve(path);
const subdirs = Object.keys(this._directories)
.filter(fileName => dirname(fileName) == path)
.map(path => basename(path));
let delegated: string[];
try {
delegated = this._delegate.getDirectories(path);
} catch (e) {
delegated = [];
}
return delegated.concat(subdirs);
}
getSourceFile(fileName: string, languageVersion: ts.ScriptTarget, _onError?: OnErrorFn) {
fileName = this._resolve(fileName);
if (this._files[fileName] == null) {
const content = this.readFile(fileName);
if (!this._cache) {
return ts.createSourceFile(fileName, content, languageVersion, this._setParentNodes);
}
}
return this._files[fileName].getSourceFile(languageVersion, this._setParentNodes);
}
getCancellationToken() {
return this._delegate.getCancellationToken();
}
getDefaultLibFileName(options: ts.CompilerOptions) {
return this._delegate.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?: ts.SourceFile[]): void => {
fileName = this._resolve(fileName);
this._setFileContent(fileName, data);
};
}
getCurrentDirectory(): string {
return this._basePath !== null ? this._basePath : this._delegate.getCurrentDirectory();
}
getCanonicalFileName(fileName: string): string {
fileName = this._resolve(fileName);
return this._delegate.getCanonicalFileName(fileName);
}
useCaseSensitiveFileNames(): boolean {
return this._delegate.useCaseSensitiveFileNames();
}
getNewLine(): string {
return this._delegate.getNewLine();
}
}