Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit be24fed

Browse files
sis0k0Brocco
authored andcommitted
feat(@ngtools/webpack): return Buffer for file system requests
1 parent d05133c commit be24fed

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

packages/ngtools/webpack/src/compiler_host.ts

+80-23
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,78 @@ export class VirtualDirStats extends VirtualStats {
7474

7575
export class VirtualFileStats extends VirtualStats {
7676
private _sourceFile: ts.SourceFile | null;
77-
constructor(_fileName: string, private _content: string) {
77+
private _content: string | null;
78+
private _bufferContent: virtualFs.FileBuffer | null;
79+
80+
constructor(_fileName: string) {
7881
super(_fileName);
7982
}
8083

81-
get content() { return this._content; }
84+
static createFromString(_fileName: string, _content: string) {
85+
const stats = new VirtualFileStats(_fileName);
86+
stats.content = _content;
87+
88+
return stats;
89+
}
90+
91+
static createFromBuffer(_fileName: string, _buffer: virtualFs.FileBuffer) {
92+
const stats = new VirtualFileStats(_fileName);
93+
stats.bufferContent = _buffer;
94+
95+
return stats;
96+
}
97+
98+
get content() {
99+
if (!this._content && this.bufferContent) {
100+
this._content = virtualFs.fileBufferToString(this.bufferContent);
101+
}
102+
103+
return this._content || '';
104+
}
82105
set content(v: string) {
83106
this._content = v;
84-
this._mtime = new Date();
85-
this._sourceFile = null;
107+
this._bufferContent = null;
108+
this.resetMetadata();
109+
}
110+
111+
get bufferContent() {
112+
if (!this._bufferContent && this._content) {
113+
this._bufferContent = virtualFs.stringToFileBuffer(this._content);
114+
}
115+
116+
return this._bufferContent || virtualFs.stringToFileBuffer('');
117+
}
118+
set bufferContent(buf: virtualFs.FileBuffer) {
119+
this._bufferContent = buf;
120+
this._content = null;
121+
this.resetMetadata();
86122
}
123+
87124
setSourceFile(sourceFile: ts.SourceFile) {
88125
this._sourceFile = sourceFile;
89126
}
90127
getSourceFile(languageVersion: ts.ScriptTarget, setParentNodes: boolean) {
91128
if (!this._sourceFile) {
92-
// console.log(this._path)
93129
this._sourceFile = ts.createSourceFile(
94130
workaroundResolve(this._path),
95-
this._content,
131+
this.content,
96132
languageVersion,
97133
setParentNodes);
98134
}
99135

100136
return this._sourceFile;
101137
}
102138

139+
private resetMetadata(): void {
140+
this._mtime = new Date();
141+
this._sourceFile = null;
142+
}
143+
103144
isFile() { return true; }
104145

105-
get size() { return this._content.length; }
146+
get size() { return this.content.length; }
106147
}
107148

108-
109149
export class WebpackCompilerHost implements ts.CompilerHost {
110150
private _syncHost: virtualFs.SyncDelegateHost;
111151
private _files: {[path: string]: VirtualFileStats | null} = Object.create(null);
@@ -149,8 +189,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
149189
}
150190
}
151191

152-
private _setFileContent(fileName: Path, content: string) {
153-
this._files[fileName] = new VirtualFileStats(fileName, content);
192+
private _cacheFile(fileName: string, stats: VirtualFileStats) {
193+
this._files[fileName] = stats;
154194

155195
let p = dirname(fileName);
156196
while (p && !this._directories[p]) {
@@ -211,25 +251,41 @@ export class WebpackCompilerHost implements ts.CompilerHost {
211251
}
212252

213253
readFile(fileName: string): string | undefined {
254+
const stats = this.findVirtualFile(fileName);
255+
256+
return stats && stats.content;
257+
}
258+
259+
readFileBuffer(fileName: string): Buffer | undefined {
260+
const stats = this.findVirtualFile(fileName);
261+
if (stats) {
262+
const buffer = Buffer.from(stats.bufferContent);
263+
264+
return buffer;
265+
}
266+
}
267+
268+
private findVirtualFile(fileName: string): VirtualFileStats | undefined {
214269
const p = this.resolve(fileName);
215270

216271
const stats = this._files[p];
217-
if (!stats) {
218-
try {
219-
const result = virtualFs.fileBufferToString(this._syncHost.read(p));
220-
if (result !== undefined) {
221-
if (this._cache) {
222-
this._setFileContent(p, result);
223-
}
272+
if (stats) {
273+
return stats;
274+
}
275+
276+
try {
277+
const fileBuffer = this._syncHost.read(p);
278+
if (fileBuffer) {
279+
const stats = VirtualFileStats.createFromBuffer(p, fileBuffer);
280+
if (this._cache) {
281+
this._cacheFile(p, stats);
224282
}
225283

226-
return result;
227-
} catch (e) {
228-
return undefined;
284+
return stats;
229285
}
286+
} catch (e) {
287+
return undefined;
230288
}
231-
232-
return stats.content;
233289
}
234290

235291
stat(path: string): VirtualStats | null {
@@ -344,7 +400,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
344400
_sourceFiles?: ReadonlyArray<ts.SourceFile>,
345401
): void => {
346402
const p = this.resolve(fileName);
347-
this._setFileContent(p, data);
403+
const stats = VirtualFileStats.createFromString(p, data);
404+
this._cacheFile(p, stats);
348405
};
349406
}
350407

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
1818
private _webpackCompilerHost: WebpackCompilerHost,
1919
) { }
2020

21-
private _readFileSync(path: string): string | null {
21+
private _readFileSync(path: string): Buffer | null {
2222
if (this._webpackCompilerHost.fileExists(path)) {
23-
return this._webpackCompilerHost.readFile(path) || null;
23+
return this._webpackCompilerHost.readFileBuffer(path) || null;
2424
}
2525

2626
return null;
@@ -51,7 +51,7 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
5151
this._inputFileSystem.readdir(path, callback);
5252
}
5353

54-
readFile(path: string, callback: Callback<string>): void {
54+
readFile(path: string, callback: Callback<string | Buffer>): void {
5555
const result = this._readFileSync(path);
5656
if (result) {
5757
callback(null, result);
@@ -78,7 +78,7 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
7878
return this._inputFileSystem.readdirSync(path);
7979
}
8080

81-
readFileSync(path: string): string {
81+
readFileSync(path: string): string | Buffer {
8282
const result = this._readFileSync(path);
8383

8484
return result || this._inputFileSystem.readFileSync(path);

packages/ngtools/webpack/src/webpack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ export interface NormalModuleFactoryRequest {
5151
export interface InputFileSystem {
5252
stat(path: string, callback: Callback<Stats>): void;
5353
readdir(path: string, callback: Callback<string[]>): void;
54-
readFile(path: string, callback: Callback<string>): void;
54+
readFile(path: string, callback: Callback<string | Buffer>): void;
5555
// tslint:disable-next-line:no-any
5656
readJson(path: string, callback: Callback<any>): void;
5757
readlink(path: string, callback: Callback<string>): void;
5858
statSync(path: string): Stats;
5959
readdirSync(path: string): string[];
60-
readFileSync(path: string): string;
60+
readFileSync(path: string): string | Buffer;
6161
// tslint:disable-next-line:no-any
6262
readJsonSync(path: string): any;
6363
readlinkSync(path: string): string;

0 commit comments

Comments
 (0)