Skip to content

Commit 530a530

Browse files
authored
Merge pull request #26590 from Microsoft/buildRefactoring
Refactor ts build and report watch status when doing --build --watch
2 parents 6c2e851 + 868cf3e commit 530a530

File tree

16 files changed

+883
-617
lines changed

16 files changed

+883
-617
lines changed

src/compiler/commandLineParser.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,11 @@ namespace ts {
815815
}
816816

817817
function getOptionNameMap(): OptionNameMap {
818-
if (optionNameMapCache) {
819-
return optionNameMapCache;
820-
}
818+
return optionNameMapCache || (optionNameMapCache = createOptionNameMap(optionDeclarations));
819+
}
821820

821+
/*@internal*/
822+
export function createOptionNameMap(optionDeclarations: ReadonlyArray<CommandLineOption>): OptionNameMap {
822823
const optionNameMap = createMap<CommandLineOption>();
823824
const shortOptionNames = createMap<string>();
824825
forEach(optionDeclarations, option => {
@@ -828,8 +829,7 @@ namespace ts {
828829
}
829830
});
830831

831-
optionNameMapCache = { optionNameMap, shortOptionNames };
832-
return optionNameMapCache;
832+
return { optionNameMap, shortOptionNames };
833833
}
834834

835835
/* @internal */
@@ -979,7 +979,12 @@ namespace ts {
979979
}
980980

981981
/** @internal */
982-
export function getOptionFromName(optionName: string, allowShort = false): CommandLineOption | undefined {
982+
export function getOptionFromName(optionName: string, allowShort?: boolean): CommandLineOption | undefined {
983+
return getOptionDeclarationFromName(getOptionNameMap, optionName, allowShort);
984+
}
985+
986+
/*@internal*/
987+
export function getOptionDeclarationFromName(getOptionNameMap: () => OptionNameMap, optionName: string, allowShort = false): CommandLineOption | undefined {
983988
optionName = optionName.toLowerCase();
984989
const { optionNameMap, shortOptionNames } = getOptionNameMap();
985990
// Try to translate short option names to their full equivalents.

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,11 @@
28962896
"category": "Error",
28972897
"code": 5071
28982898
},
2899+
"Unknown build option '{0}'.": {
2900+
"category": "Error",
2901+
"code": 5072
2902+
},
2903+
28992904

29002905
"Generates a sourcemap for each corresponding '.d.ts' file.": {
29012906
"category": "Message",

src/compiler/program.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,24 @@ namespace ts {
6767
}
6868

6969
export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
70+
return createCompilerHostWorker(options, setParentNodes);
71+
}
72+
/*@internal*/
73+
// TODO(shkamat): update this after reworking ts build API
74+
export function createCompilerHostWorker(options: CompilerOptions, setParentNodes?: boolean, system = sys): CompilerHost {
7075
const existingDirectories = createMap<boolean>();
7176

7277
function getCanonicalFileName(fileName: string): string {
7378
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
7479
// otherwise use toLowerCase as a canonical form.
75-
return sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
80+
return system.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
7681
}
7782

7883
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile | undefined {
7984
let text: string | undefined;
8085
try {
8186
performance.mark("beforeIORead");
82-
text = sys.readFile(fileName, options.charset);
87+
text = system.readFile(fileName, options.charset);
8388
performance.mark("afterIORead");
8489
performance.measure("I/O Read", "beforeIORead", "afterIORead");
8590
}
@@ -97,7 +102,7 @@ namespace ts {
97102
if (existingDirectories.has(directoryPath)) {
98103
return true;
99104
}
100-
if (sys.directoryExists(directoryPath)) {
105+
if (system.directoryExists(directoryPath)) {
101106
existingDirectories.set(directoryPath, true);
102107
return true;
103108
}
@@ -108,7 +113,7 @@ namespace ts {
108113
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
109114
const parentDirectory = getDirectoryPath(directoryPath);
110115
ensureDirectoriesExist(parentDirectory);
111-
sys.createDirectory(directoryPath);
116+
system.createDirectory(directoryPath);
112117
}
113118
}
114119

@@ -119,8 +124,8 @@ namespace ts {
119124
outputFingerprints = createMap<OutputFingerprint>();
120125
}
121126

122-
const hash = sys.createHash!(data); // TODO: GH#18217
123-
const mtimeBefore = sys.getModifiedTime!(fileName); // TODO: GH#18217
127+
const hash = system.createHash!(data); // TODO: GH#18217
128+
const mtimeBefore = system.getModifiedTime!(fileName); // TODO: GH#18217
124129

125130
if (mtimeBefore) {
126131
const fingerprint = outputFingerprints.get(fileName);
@@ -133,9 +138,9 @@ namespace ts {
133138
}
134139
}
135140

136-
sys.writeFile(fileName, data, writeByteOrderMark);
141+
system.writeFile(fileName, data, writeByteOrderMark);
137142

138-
const mtimeAfter = sys.getModifiedTime!(fileName) || missingFileModifiedTime; // TODO: GH#18217
143+
const mtimeAfter = system.getModifiedTime!(fileName) || missingFileModifiedTime; // TODO: GH#18217
139144

140145
outputFingerprints.set(fileName, {
141146
hash,
@@ -149,11 +154,11 @@ namespace ts {
149154
performance.mark("beforeIOWrite");
150155
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
151156

152-
if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) {
157+
if (isWatchSet(options) && system.createHash && system.getModifiedTime) {
153158
writeFileIfUpdated(fileName, data, writeByteOrderMark);
154159
}
155160
else {
156-
sys.writeFile(fileName, data, writeByteOrderMark);
161+
system.writeFile(fileName, data, writeByteOrderMark);
157162
}
158163

159164
performance.mark("afterIOWrite");
@@ -167,32 +172,29 @@ namespace ts {
167172
}
168173

169174
function getDefaultLibLocation(): string {
170-
return getDirectoryPath(normalizePath(sys.getExecutingFilePath()));
175+
return getDirectoryPath(normalizePath(system.getExecutingFilePath()));
171176
}
172177

173-
const newLine = getNewLineCharacter(options);
174-
const realpath = sys.realpath && ((path: string) => sys.realpath!(path));
178+
const newLine = getNewLineCharacter(options, () => system.newLine);
179+
const realpath = system.realpath && ((path: string) => system.realpath!(path));
175180

176181
return {
177182
getSourceFile,
178183
getDefaultLibLocation,
179184
getDefaultLibFileName: options => combinePaths(getDefaultLibLocation(), getDefaultLibFileName(options)),
180185
writeFile,
181-
getCurrentDirectory: memoize(() => sys.getCurrentDirectory()),
182-
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
186+
getCurrentDirectory: memoize(() => system.getCurrentDirectory()),
187+
useCaseSensitiveFileNames: () => system.useCaseSensitiveFileNames,
183188
getCanonicalFileName,
184189
getNewLine: () => newLine,
185-
fileExists: fileName => sys.fileExists(fileName),
186-
readFile: fileName => sys.readFile(fileName),
187-
trace: (s: string) => sys.write(s + newLine),
188-
directoryExists: directoryName => sys.directoryExists(directoryName),
189-
getEnvironmentVariable: name => sys.getEnvironmentVariable ? sys.getEnvironmentVariable(name) : "",
190-
getDirectories: (path: string) => sys.getDirectories(path),
190+
fileExists: fileName => system.fileExists(fileName),
191+
readFile: fileName => system.readFile(fileName),
192+
trace: (s: string) => system.write(s + newLine),
193+
directoryExists: directoryName => system.directoryExists(directoryName),
194+
getEnvironmentVariable: name => system.getEnvironmentVariable ? system.getEnvironmentVariable(name) : "",
195+
getDirectories: (path: string) => system.getDirectories(path),
191196
realpath,
192-
readDirectory: (path, extensions, include, exclude, depth) => sys.readDirectory(path, extensions, include, exclude, depth),
193-
getModifiedTime: sys.getModifiedTime && (path => sys.getModifiedTime!(path)),
194-
setModifiedTime: sys.setModifiedTime && ((path, date) => sys.setModifiedTime!(path, date)),
195-
deleteFile: sys.deleteFile && (path => sys.deleteFile!(path))
197+
readDirectory: (path, extensions, include, exclude, depth) => system.readDirectory(path, extensions, include, exclude, depth)
196198
};
197199
}
198200

0 commit comments

Comments
 (0)