Skip to content

package.json#exports should be traceable with traceResolutionΒ #51001

@Andarist

Description

@Andarist

Suggestion

πŸ” Search Terms

exports, resolution, nodeResolution, traceResolution, esm, cjs

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

--traceResolution should include the package.json#exports resolution in the output.

πŸ“ƒ Motivation

This algorithm is quite complex and involved and thus any insights that users can get from TS as to what is matched and why would be very much appreciated.

Compare the output for the classic moduleResolution:

classic: quite detailed mentions of all steps
======== Resolving module 'tiny-invariant' from '/test__/tiny-invariant-test/test.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'tiny-invariant' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/test__/tiny-invariant-test/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/test__/node_modules/tiny-invariant/package.json'.
'package.json' does not have a 'typesVersions' field.
File '/test__/node_modules/tiny-invariant.ts' does not exist.
File '/test__/node_modules/tiny-invariant.tsx' does not exist.
File '/test__/node_modules/tiny-invariant.d.ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'dist/tiny-invariant.d.ts' that references '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts'.
File '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts' exist - use it as a name resolution result.
Resolving real path for '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts', result '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts'.
======== Module name 'tiny-invariant' was successfully resolved to '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts' with Package ID 'tiny-invariant/dist/tiny-invariant.d.ts@1.3.0'. ========

with the output for the moduleResolution: node16 and the existing export field in a package:

node16: seemingly random "jump" from lack of `typesVersions` to the resolved `.d.ts`
======== Resolving module 'tiny-invariant' from '/test__/tiny-invariant-test/test.ts'. ========
Explicitly specified module resolution kind: 'Node16'.
File '/test__/tiny-invariant-test/package.json' does not exist according to earlier cached lookups.
File '/test__/package.json' exists according to earlier cached lookups.
Loading module 'tiny-invariant' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/test__/tiny-invariant-test/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/test__/node_modules/tiny-invariant/package.json'.
'package.json' does not have a 'typesVersions' field.
File '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts' exist - use it as a name resolution result.
Resolving real path for '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts', result '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts'.
======== Module name 'tiny-invariant' was successfully resolved to '/test__/node_modules/tiny-invariant/dist/tiny-invariant.d.ts' with Package ID 'tiny-invariant/dist/tiny-invariant.d.ts@1.3.0'. ========

cc @andrewbranch

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions