Revert "Refactor the package.json property lookup"

This reverts commit 0e16657468d94e151927a3a23406a25d0b9c75f7.
This commit is contained in:
Mohamed Hegazy 2016-12-21 15:45:37 -08:00
parent 0e16657468
commit 12f1d19fa4
3 changed files with 19 additions and 28 deletions

View File

@ -67,13 +67,23 @@ namespace ts {
}
/** Reads from "main" or "types"/"typings" depending on `extensions`. */
function tryReadPackageJsonMainAndTypes(packageJsonPath: string, baseDirectory: string, state: ModuleResolutionState): {types: string, main: string} {
function tryReadPackageJsonMainOrTypes(extensions: Extensions, packageJsonPath: string, baseDirectory: string, state: ModuleResolutionState): string {
const jsonContent = readJson(packageJsonPath, state.host);
return {
types: tryReadFromField("typings") || tryReadFromField("types"),
main: tryReadFromField("main")
};
switch (extensions) {
case Extensions.DtsOnly:
case Extensions.TypeScript:
return tryReadFromField("typings") || tryReadFromField("types");
case Extensions.JavaScript:
if (typeof jsonContent.main === "string") {
if (state.traceEnabled) {
trace(state.host, Diagnostics.No_types_specified_in_package_json_so_returning_main_value_of_0, jsonContent.main);
}
return normalizePath(combinePaths(baseDirectory, jsonContent.main));
}
return undefined;
}
function tryReadFromField(fieldName: string) {
if (hasProperty(jsonContent, fieldName)) {
@ -700,21 +710,7 @@ namespace ts {
if (state.traceEnabled) {
trace(state.host, Diagnostics.Found_package_json_at_0, packageJsonPath);
}
const {types, main} = tryReadPackageJsonMainAndTypes(packageJsonPath, candidate, state);
let mainOrTypesFile;
if (extensions === Extensions.DtsOnly || extensions === Extensions.TypeScript) {
mainOrTypesFile = types;
}
else if (extensions === Extensions.JavaScript) {
mainOrTypesFile = main;
if (main) {
if (state.traceEnabled) {
trace(state.host, Diagnostics.No_types_specified_in_package_json_so_returning_main_value_of_0, main);
}
}
}
const mainOrTypesFile = tryReadPackageJsonMainOrTypes(extensions, packageJsonPath, candidate, state);
if (mainOrTypesFile) {
const onlyRecordFailures = !directoryProbablyExists(getDirectoryPath(mainOrTypesFile), state.host);
// A package.json "typings" may specify an exact filename, or may choose to omit an extension.
@ -731,10 +727,8 @@ namespace ts {
return resolved;
}
// A package.json "main" may specify an exact filename, or may choose to omit an extension.
// We tried the ts extensions erlier, now try the js extensions
if (main && extensions === Extensions.JavaScript) {
const resolved = tryAddingExtensions(main, Extensions.JavaScript, failedLookupLocations, onlyRecordFailures, state);
if (extensions === Extensions.JavaScript) {
const resolved = tryAddingExtensions(mainOrTypesFile, Extensions.JavaScript, failedLookupLocations, onlyRecordFailures, state);
if (resolved) {
return resolved;
}

View File

@ -6,7 +6,6 @@
"File '/node_modules/normalize.css.tsx' does not exist.",
"File '/node_modules/normalize.css.d.ts' does not exist.",
"Found 'package.json' at '/node_modules/normalize.css/package.json'.",
"'package.json' has 'main' field 'normalize.css' that references '/node_modules/normalize.css/normalize.css'.",
"'package.json' does not have a 'types' or 'main' field.",
"File '/node_modules/normalize.css/index.ts' does not exist.",
"File '/node_modules/normalize.css/index.tsx' does not exist.",
@ -18,8 +17,7 @@
"File '/node_modules/normalize.css.js' does not exist.",
"File '/node_modules/normalize.css.jsx' does not exist.",
"Found 'package.json' at '/node_modules/normalize.css/package.json'.",
"'package.json' has 'main' field 'normalize.css' that references '/node_modules/normalize.css/normalize.css'.",
"No types specified in 'package.json', so returning 'main' value of '/node_modules/normalize.css/normalize.css'",
"No types specified in 'package.json', so returning 'main' value of 'normalize.css'",
"File '/node_modules/normalize.css/normalize.css' exist - use it as a name resolution result.",
"File '/node_modules/normalize.css/normalize.css' has an unsupported extension, so skipping it.",
"File '/node_modules/normalize.css/normalize.css.ts' does not exist.",

View File

@ -22,7 +22,6 @@
"File '/node_modules/foo.js' does not exist.",
"File '/node_modules/foo.jsx' does not exist.",
"Found 'package.json' at '/node_modules/foo/package.json'.",
"'package.json' has 'types' field 'foo.js' that references '/node_modules/foo/foo.js'.",
"'package.json' does not have a 'types' or 'main' field.",
"File '/node_modules/foo/index.js' does not exist.",
"File '/node_modules/foo/index.jsx' does not exist.",