-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "mode" for typescript reporter
To improve initial compilation time on babel-loader, we can now specify "write-tsbuildinfo" and "write-references" mode.
- Loading branch information
1 parent
e59a8d2
commit b700465
Showing
14 changed files
with
502 additions
and
291 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,26 @@ | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
import { Dirent, Stats } from 'fs'; | ||
|
||
/** | ||
* Interface to abstract file system implementation details. | ||
*/ | ||
interface FileSystem { | ||
// read | ||
exists(path: string): boolean; | ||
readFile(path: string, encoding?: string): string | undefined; | ||
readDir(path: string): Dirent[]; | ||
readStats(path: string): Stats | undefined; | ||
realPath(path: string): string; | ||
normalizePath(path: string): string; | ||
|
||
// write | ||
writeFile(path: string, data: string): void; | ||
deleteFile(path: string): void; | ||
createDir(path: string): void; | ||
updateTimes(path: string, atime: Date, mtime: Date): void; | ||
|
||
// cache | ||
clearCache(): void; | ||
} | ||
|
||
export { FileSystem }; |
145 changes: 145 additions & 0 deletions
145
src/typescript-reporter/file-system/PassiveFileSystem.ts
This file contains 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,145 @@ | ||
import { dirname, normalize } from 'path'; | ||
import { fs as mem } from 'memfs'; | ||
import { FileSystem } from './FileSystem'; | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
import { Dirent, Stats } from 'fs'; | ||
|
||
/** | ||
* It's an implementation of FileSystem interface which reads from the real file system, but write to the in-memory file system. | ||
* | ||
* @param caseSensitive | ||
* @param realFileSystem | ||
*/ | ||
function createPassiveFileSystem(caseSensitive = false, realFileSystem: FileSystem): FileSystem { | ||
function normalizePath(path: string): string { | ||
return caseSensitive ? normalize(path) : normalize(path).toLowerCase(); | ||
} | ||
|
||
function memExists(path: string): boolean { | ||
return mem.existsSync(normalizePath(path)); | ||
} | ||
|
||
function memReadStats(path: string): Stats | undefined { | ||
return memExists(path) ? mem.statSync(normalizePath(path)) : undefined; | ||
} | ||
|
||
function memReadFile(path: string, encoding?: string): string | undefined { | ||
if (memExists(path)) { | ||
return mem | ||
.readFileSync(normalizePath(path), { encoding: encoding as BufferEncoding }) | ||
.toString(); | ||
} | ||
} | ||
|
||
function memReadDir(path: string): Dirent[] { | ||
if (memExists(path)) { | ||
return mem.readdirSync(normalizePath(path), { withFileTypes: true }) as Dirent[]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
function exists(path: string) { | ||
return realFileSystem.exists(path) || memExists(path); | ||
} | ||
|
||
function readFile(path: string, encoding?: string) { | ||
const fsStats = realFileSystem.readStats(path); | ||
const memStats = memReadStats(path); | ||
|
||
if (fsStats && memStats) { | ||
return fsStats.mtimeMs > memStats.mtimeMs | ||
? realFileSystem.readFile(path, encoding) | ||
: memReadFile(path, encoding); | ||
} else if (fsStats) { | ||
return realFileSystem.readFile(path, encoding); | ||
} else if (memStats) { | ||
return memReadFile(path, encoding); | ||
} | ||
} | ||
|
||
function readDir(path: string) { | ||
const fsDirents = realFileSystem.readDir(path); | ||
const memDirents = memReadDir(path); | ||
|
||
// merge list of dirents from fs and mem | ||
return fsDirents | ||
.filter((fsDirent) => !memDirents.some((memDirent) => memDirent.name === fsDirent.name)) | ||
.concat(memDirents); | ||
} | ||
|
||
function readStats(path: string) { | ||
const fsStats = realFileSystem.readStats(path); | ||
const memStats = memReadStats(path); | ||
|
||
if (fsStats && memStats) { | ||
return fsStats.mtimeMs > memStats.mtimeMs ? fsStats : memStats; | ||
} else if (fsStats) { | ||
return fsStats; | ||
} else if (memStats) { | ||
return memStats; | ||
} | ||
} | ||
|
||
function createDir(path: string) { | ||
mem.mkdirSync(normalizePath(path), { recursive: true }); | ||
} | ||
|
||
function writeFile(path: string, data: string) { | ||
if (!memExists(dirname(path))) { | ||
createDir(dirname(path)); | ||
} | ||
|
||
mem.writeFileSync(normalizePath(path), data); | ||
} | ||
|
||
function deleteFile(path: string) { | ||
if (memExists(path)) { | ||
mem.unlinkSync(normalizePath(path)); | ||
} | ||
} | ||
|
||
function updateTimes(path: string, atime: Date, mtime: Date) { | ||
if (memExists(path)) { | ||
mem.utimesSync(normalizePath(path), atime, mtime); | ||
} | ||
} | ||
|
||
return { | ||
exists(path: string) { | ||
return exists(realFileSystem.realPath(path)); | ||
}, | ||
readFile(path: string, encoding?: string) { | ||
return readFile(realFileSystem.realPath(path), encoding); | ||
}, | ||
readDir(path: string) { | ||
return readDir(realFileSystem.realPath(path)); | ||
}, | ||
readStats(path: string) { | ||
return readStats(realFileSystem.realPath(path)); | ||
}, | ||
realPath(path: string) { | ||
return realFileSystem.realPath(path); | ||
}, | ||
normalizePath(path: string) { | ||
return normalizePath(path); | ||
}, | ||
writeFile(path: string, data: string) { | ||
writeFile(realFileSystem.realPath(path), data); | ||
}, | ||
deleteFile(path: string) { | ||
deleteFile(realFileSystem.realPath(path)); | ||
}, | ||
createDir(path: string) { | ||
createDir(realFileSystem.realPath(path)); | ||
}, | ||
updateTimes(path: string, atime: Date, mtime: Date) { | ||
updateTimes(realFileSystem.realPath(path), atime, mtime); | ||
}, | ||
clearCache() { | ||
realFileSystem.clearCache(); | ||
}, | ||
}; | ||
} | ||
|
||
export { createPassiveFileSystem }; |
Oops, something went wrong.