Allow package.json "main" to specify a directory#13678
Conversation
| const jsonContent = readJson(packageJsonPath, state.host); | ||
| const file = ts ? tryReadFromField("typings") || tryReadFromField("types") : tryReadFromField("main"); | ||
| if (!file && state.traceEnabled) { | ||
| trace(state.host, Diagnostics.package_json_does_not_have_a_0_field, ts ? "types" : "main"); |
There was a problem hiding this comment.
it looks that we'll get this error even in case if package.json has field with some valid name but incorrect type of the value (tryReadFromField in this case will return undefined) - it is a bit misleading
| } | ||
|
|
||
| function nodeLoadModuleByRelativeName(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState): Resolved | undefined { | ||
| function nodeLoadModuleByRelativeName(extensions: Extensions, candidate: string, failedLookupLocations: Push<string>, onlyRecordFailures: boolean, state: ModuleResolutionState, considerPackageJson = true): Resolved | undefined { |
There was a problem hiding this comment.
minor nit: can we convert it to an explicit value? sometimes when reading the code default values for parameters can slip out since they don't appear on the call site
| trace(state.host, Diagnostics.Found_package_json_at_0, packageJsonPath); | ||
| } | ||
|
|
||
| const ts = extensions !== Extensions.JavaScript; |
There was a problem hiding this comment.
ts is not particularly meaningful, can you give it a better name (i.e. loadWithTsExtensions)?
| // Even if extensions is DtsOnly, we can still look up a .ts file as a result of package.json "types" | ||
| const nextExtensions = extensions === Extensions.DtsOnly ? Extensions.TypeScript : extensions; | ||
| // Don't do package.json lookup recursively, because Node.js' package lookup doesn't. | ||
| return nodeLoadModuleByRelativeName(nextExtensions, file, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ false); |
There was a problem hiding this comment.
that looks a bit odd: does it mean that i.e. for .ts or .js extensions we'll repeat the whole resolution process trying to resolve the same file again?
There was a problem hiding this comment.
We'll repeat the process trying to resolve the file specified in the package.json. But considerPackageJson is false this time, so it won't repeat forever.
Fixes #12198
Re-do of #12268