|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import {basename, dirname} from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
| 5 | + |
| 6 | +export interface OnErrorFn { |
| 7 | + (message: string): void; |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | +const dev = Math.floor(Math.random() * 10000); |
| 12 | + |
| 13 | + |
| 14 | +export class VirtualStats implements fs.Stats { |
| 15 | + protected _ctime = new Date(); |
| 16 | + protected _mtime = new Date(); |
| 17 | + protected _atime = new Date(); |
| 18 | + protected _btime = new Date(); |
| 19 | + protected _dev = dev; |
| 20 | + protected _ino = Math.floor(Math.random() * 100000); |
| 21 | + protected _mode = parseInt('777', 8); // RWX for everyone. |
| 22 | + protected _uid = process.env['UID'] || 0; |
| 23 | + protected _gid = process.env['GID'] || 0; |
| 24 | + |
| 25 | + constructor(protected _path: string) {} |
| 26 | + |
| 27 | + isFile() { return false; } |
| 28 | + isDirectory() { return false; } |
| 29 | + isBlockDevice() { return false; } |
| 30 | + isCharacterDevice() { return false; } |
| 31 | + isSymbolicLink() { return false; } |
| 32 | + isFIFO() { return false; } |
| 33 | + isSocket() { return false; } |
| 34 | + |
| 35 | + get dev() { return this._dev; } |
| 36 | + get ino() { return this._ino; } |
| 37 | + get mode() { return this._mode; } |
| 38 | + get nlink() { return 1; } // Default to 1 hard link. |
| 39 | + get uid() { return this._uid; } |
| 40 | + get gid() { return this._gid; } |
| 41 | + get rdev() { return 0; } |
| 42 | + get size() { return 0; } |
| 43 | + get blksize() { return 512; } |
| 44 | + get blocks() { return Math.ceil(this.size / this.blksize); } |
| 45 | + get atime() { return this._atime; } |
| 46 | + get mtime() { return this._mtime; } |
| 47 | + get ctime() { return this._ctime; } |
| 48 | + get birthtime() { return this._btime; } |
| 49 | +} |
| 50 | + |
| 51 | +export class VirtualDirStats extends VirtualStats { |
| 52 | + constructor(_fileName: string) { |
| 53 | + super(_fileName); |
| 54 | + } |
| 55 | + |
| 56 | + isDirectory() { return true; } |
| 57 | + |
| 58 | + get size() { return 1024; } |
| 59 | +} |
| 60 | + |
| 61 | +export class VirtualFileStats extends VirtualStats { |
| 62 | + private _sourceFile: ts.SourceFile; |
| 63 | + constructor(_fileName: string, private _content: string) { |
| 64 | + super(_fileName); |
| 65 | + } |
| 66 | + |
| 67 | + get content() { return this._content; } |
| 68 | + set content(v: string) { |
| 69 | + this._content = v; |
| 70 | + this._mtime = new Date(); |
| 71 | + } |
| 72 | + getSourceFile(languageVersion: ts.ScriptTarget, setParentNodes: boolean) { |
| 73 | + if (!this._sourceFile) { |
| 74 | + this._sourceFile = ts.createSourceFile( |
| 75 | + this._path, |
| 76 | + this._content, |
| 77 | + languageVersion, |
| 78 | + setParentNodes); |
| 79 | + } |
| 80 | + |
| 81 | + return this._sourceFile; |
| 82 | + } |
| 83 | + |
| 84 | + isFile() { return true; } |
| 85 | + |
| 86 | + get size() { return this._content.length; } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +export class WebpackCompilerHost implements ts.CompilerHost { |
| 91 | + private _delegate: ts.CompilerHost; |
| 92 | + private _cachedFiles: {[path: string]: VirtualFileStats} = Object.create(null); |
| 93 | + private _directories: {[path: string]: VirtualDirStats} = Object.create(null); |
| 94 | + |
| 95 | + constructor(private _options: ts.CompilerOptions, private _setParentNodes = true) { |
| 96 | + this._delegate = ts.createCompilerHost(this._options, this._setParentNodes); |
| 97 | + } |
| 98 | + |
| 99 | + private _setFileContent(fileName: string, content: string) { |
| 100 | + this._cachedFiles[fileName] = new VirtualFileStats(fileName, content); |
| 101 | + |
| 102 | + let p = dirname(fileName); |
| 103 | + while (p && !this._directories[p]) { |
| 104 | + this._directories[p] = new VirtualDirStats(p); |
| 105 | + p = dirname(p); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + populateWebpackResolver(resolver: any) { |
| 110 | + const fs = resolver.fileSystem; |
| 111 | + |
| 112 | + for (const fileName of Object.keys(this._cachedFiles)) { |
| 113 | + const stats = this._cachedFiles[fileName]; |
| 114 | + fs._statStorage.data[fileName] = [null, stats]; |
| 115 | + fs._readFileStorage.data[fileName] = [null, stats.content]; |
| 116 | + } |
| 117 | + for (const path of Object.keys(this._directories)) { |
| 118 | + const stats = this._directories[path]; |
| 119 | + const files = Object.keys(this._directories) |
| 120 | + .filter(fileName => dirname(fileName) == path) |
| 121 | + .map(path => basename(path)); |
| 122 | + |
| 123 | + fs._statStorage.data[path] = [null, stats]; |
| 124 | + fs._readdirStorage.data[path] = [null, files]; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fileExists(fileName: string): boolean { |
| 129 | + return fileName in this._cachedFiles || this._delegate.fileExists(fileName); |
| 130 | + } |
| 131 | + |
| 132 | + readFile(fileName: string): string { |
| 133 | + return (fileName in this._cachedFiles) |
| 134 | + ? this._cachedFiles[fileName].content |
| 135 | + : this._delegate.readFile(fileName); |
| 136 | + } |
| 137 | + |
| 138 | + directoryExists(directoryName: string): boolean { |
| 139 | + return (directoryName in this._directories) || this._delegate.directoryExists(directoryName); |
| 140 | + } |
| 141 | + |
| 142 | + getDirectories(path: string): string[] { |
| 143 | + return this._delegate.getDirectories(path); |
| 144 | + } |
| 145 | + |
| 146 | + getSourceFile(fileName: string, languageVersion: ts.ScriptTarget, onError?: OnErrorFn) { |
| 147 | + if (!(fileName in this._cachedFiles)) { |
| 148 | + return this._delegate.getSourceFile(fileName, languageVersion, onError); |
| 149 | + } |
| 150 | + |
| 151 | + return this._cachedFiles[fileName].getSourceFile(languageVersion, this._setParentNodes); |
| 152 | + } |
| 153 | + |
| 154 | + getCancellationToken() { |
| 155 | + return this._delegate.getCancellationToken(); |
| 156 | + } |
| 157 | + |
| 158 | + getDefaultLibFileName(options: ts.CompilerOptions) { |
| 159 | + return this._delegate.getDefaultLibFileName(options); |
| 160 | + } |
| 161 | + |
| 162 | + writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: OnErrorFn) { |
| 163 | + this._setFileContent(fileName, data); |
| 164 | + } |
| 165 | + |
| 166 | + getCurrentDirectory(): string { |
| 167 | + return this._delegate.getCurrentDirectory(); |
| 168 | + } |
| 169 | + |
| 170 | + getCanonicalFileName(fileName: string): string { |
| 171 | + return this._delegate.getCanonicalFileName(fileName); |
| 172 | + } |
| 173 | + |
| 174 | + useCaseSensitiveFileNames(): boolean { |
| 175 | + return this._delegate.useCaseSensitiveFileNames(); |
| 176 | + } |
| 177 | + |
| 178 | + getNewLine(): string { |
| 179 | + return this._delegate.getNewLine(); |
| 180 | + } |
| 181 | +} |
0 commit comments