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

Commit 259f437

Browse files
committed
feat(@ngtools/webpack): return Buffer for file system requests
1 parent 0ab89e1 commit 259f437

File tree

3 files changed

+84
-30
lines changed

3 files changed

+84
-30
lines changed

packages/ngtools/webpack/src/compiler_host.ts

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,77 @@ 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("");
86117
}
118+
set bufferContent(buf: virtualFs.FileBuffer) {
119+
this._bufferContent = buf;
120+
this._content = null;
121+
this.resetMetadata();
122+
}
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
}
99-
100135
return this._sourceFile;
101136
}
102137

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

105-
get size() { return this._content.length; }
145+
get size() { return this.content.length; }
106146
}
107147

108-
109148
export class WebpackCompilerHost implements ts.CompilerHost {
110149
private _syncHost: virtualFs.SyncDelegateHost;
111150
private _files: {[path: string]: VirtualFileStats | null} = Object.create(null);
@@ -149,8 +188,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
149188
}
150189
}
151190

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

155194
let p = dirname(fileName);
156195
while (p && !this._directories[p]) {
@@ -211,25 +250,39 @@ export class WebpackCompilerHost implements ts.CompilerHost {
211250
}
212251

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

216268
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-
}
269+
if (stats) {
270+
return stats;
271+
}
272+
273+
try {
274+
const fileBuffer = this._syncHost.read(p);
275+
if (fileBuffer) {
276+
const stats = VirtualFileStats.createFromBuffer(p, fileBuffer);
277+
if (this._cache) {
278+
this._cacheFile(p, stats);
224279
}
225280

226-
return result;
227-
} catch (e) {
228-
return undefined;
281+
return stats;
229282
}
283+
} catch(e) {
284+
return undefined;
230285
}
231-
232-
return stats.content;
233286
}
234287

235288
stat(path: string): VirtualStats | null {
@@ -344,7 +397,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
344397
_sourceFiles?: ReadonlyArray<ts.SourceFile>,
345398
): void => {
346399
const p = this.resolve(fileName);
347-
this._setFileContent(p, data);
400+
const stats = VirtualFileStats.createFromString(p, data);
401+
this._cacheFile(p, stats);
348402
};
349403
}
350404

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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)