Skip to content

Commit

Permalink
Improve loadModuleFromFile code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed May 31, 2016
1 parent fa16a99 commit a0546a9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,24 +628,25 @@ namespace ts {
}
}

if (state.skipTsx)
extensions = filter(extensions, ext => ext !== "tsx");

// First try to keep/add an extension: importing "./foo.ts" can be matched by a file "./foo.ts", and "./foo" by "./foo.d.ts"
const keepOrAddExtension = forEach(extensions, ext =>
tryLoad(fileExtensionIs(candidate, ext) ? candidate : candidate + ext));
const keepOrAddExtension = forEach(extensions, ext => {
if (state.skipTsx && (ext === ".jsx" || ext === ".tsx")) {
return;
}
return tryLoad(fileExtensionIs(candidate, ext) ? candidate : candidate + ext);
});
if (keepOrAddExtension) {
return keepOrAddExtension;
}

// Then try stripping a ".js" or ".jsx" extension and replacing it with a different one, e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts"
// Then try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one, e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts"
return forEach(supportedJavascriptExtensions, jsExt => {
if (state.skipTsx && jsExt === ".jsx") {
return;
}
const extensionless = tryRemoveExtension(candidate, jsExt);
if (extensionless !== undefined) {
return forEach(supportedTypeScriptExtensions, ext => {
if (ext !== jsExt)
return tryLoad(extensionless + ext);
});
return forEach(supportedTypeScriptExtensions, tsExt => tryLoad(extensionless + tsExt));
}
});

Expand Down

0 comments on commit a0546a9

Please sign in to comment.