Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit d344196

Browse files
committed
Merge pull request #65 from davidmiani/master
Fixes issues under windows with typescript 1.6, and moduleResolution flags
2 parents b211544 + c17b487 commit d344196

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/host.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ export class Host implements ts.LanguageServiceHost {
8585
}
8686

8787
getScriptFileNames() {
88-
return Object.keys(this.state.files);
88+
return this.state.allFileNames();
8989
}
9090

9191
getScriptVersion(fileName: string) {
92-
if (this.state.files[fileName]) {
93-
return this.state.files[fileName].version.toString();
92+
if (this.state.getFile(fileName)) {
93+
return this.state.getFile(fileName).version.toString();
9494
}
9595
}
9696

9797
getScriptSnapshot(fileName) {
98-
let file = this.state.files[fileName];
98+
let file = this.state.getFile(fileName);
9999
if (file) {
100100
return this.state.ts.ScriptSnapshot.fromString(file.text);
101101
}
@@ -169,7 +169,7 @@ export class State {
169169
fs: typeof fs;
170170
compilerInfo: ICompilerInfo;
171171
host: Host;
172-
files: {[fileName: string]: IFile} = {};
172+
private files: {[fileName: string]: IFile} = {};
173173
services: ts.LanguageService;
174174
options: ICompilerOptions;
175175
program: ts.Program;
@@ -227,7 +227,21 @@ export class State {
227227
this.program = this.services.getProgram();
228228
}
229229

230+
allFileNames() {
231+
return Object.keys(this.files);
232+
}
233+
234+
/**
235+
* Returns all the files in this state.
236+
* Don't add new files using this value (eg `allFiles()[newFilePath] = ...`), just use it as a
237+
* read only reference (as otherwise the paths won't be normalized correctly)
238+
*/
239+
allFiles() {
240+
return this.files;
241+
}
242+
230243
emit(fileName: string): IEmitOutput {
244+
fileName = this.normalizePath(fileName);
231245

232246
if (!this.program) {
233247
this.program = this.services.getProgram();
@@ -270,6 +284,7 @@ export class State {
270284
}
271285

272286
updateFile(fileName: string, text: string, checked: boolean = false): boolean {
287+
fileName = this.normalizePath(fileName);
273288
let prevFile = this.files[fileName];
274289
let version = 0;
275290
let changed = true;
@@ -291,37 +306,49 @@ export class State {
291306
}
292307

293308
addFile(fileName: string, text: string): void {
309+
fileName = this.normalizePath(fileName);
294310
this.files[fileName] = {
295311
text: text,
296312
version: 0
297313
}
298314
}
299315

316+
getFile(fileName: string) {
317+
fileName = this.normalizePath(fileName);
318+
return this.files[fileName];
319+
}
320+
300321
hasFile(fileName: string): boolean {
322+
fileName = this.normalizePath(fileName);
301323
return this.files.hasOwnProperty(fileName);
302324
}
303325

304326
readFile(fileName: string): Promise<string> {
327+
fileName = this.normalizePath(fileName);
305328
let readFile = Promise.promisify(this.fs.readFile.bind(this.fs));
306329
return readFile(fileName).then(function (buf) {
307330
return buf.toString('utf8');
308331
});
309332
}
310333

311334
readFileSync(fileName: string): string {
335+
fileName = this.normalizePath(fileName);
312336
// Use global fs here, because local doesn't contain `readFileSync`
313337
return fs.readFileSync(fileName, {encoding: 'utf-8'});
314338
}
315339

316340
readFileAndAdd(fileName: string): Promise<any> {
341+
fileName = this.normalizePath(fileName);
317342
return this.readFile(fileName).then((text) => this.addFile(fileName, text));
318343
}
319344

320345
readFileAndUpdate(fileName: string, checked: boolean = false): Promise<boolean> {
346+
fileName = this.normalizePath(fileName);
321347
return this.readFile(fileName).then((text) => this.updateFile(fileName, text, checked));
322348
}
323349

324350
readFileAndUpdateSync(fileName: string, checked: boolean = false): boolean {
351+
fileName = this.normalizePath(fileName);
325352
let text = this.readFileSync(fileName);
326353
return this.updateFile(fileName, text, checked);
327354
}

src/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
203203
tsConfigFiles = configFile.config.files || tsConfigFiles;
204204
}
205205
}
206+
if (typeof options.moduleResolution === "string") {
207+
var moduleTypes = {
208+
"node": tsImpl.ModuleResolutionKind.NodeJs,
209+
"classic": tsImpl.ModuleResolutionKind.Classic
210+
};
211+
options.moduleResolution = moduleTypes[options.moduleResolution];
212+
213+
}
206214

207215
if (typeof options.emitRequireType === 'undefined') {
208216
options.emitRequireType = true;
@@ -340,7 +348,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
340348

341349
if (forkChecker) {
342350
let payload = {
343-
files: state.files,
351+
files: state.allFiles(),
344352
resolutionCache: state.host.moduleResolutionHost.resolutionCache
345353
};
346354

@@ -363,7 +371,7 @@ function ensureInstance(webpack: IWebPack, options: ICompilerOptions, instanceNa
363371
}
364372

365373
let phantomImports = [];
366-
Object.keys(state.files).forEach((fileName) => {
374+
state.allFileNames().forEach((fileName) => {
367375
if (!instance.compiledFiles[fileName]) {
368376
phantomImports.push(fileName)
369377
}

0 commit comments

Comments
 (0)