In node.js, import.meta.resolve takes a module specifier (for our purposes: a relative or absolute path, or a bare module specifier with an optional subpath) and returns a path on disk.
Is there a function that performs the inverse operation? Given an abspath to a file on disk, I want to "deresolve" it to a package export's relative path as referenced in the exports map
"exports": {
".": "./bundle.js",
"./": "./dist/"
}
Given ./dist/a.js, how to I get to ./a.js?
There are some constraints for my version of the problem which may help in implementing the solution:
- We can assume that the package is on disk
- We take disk paths relative to the package root as input.
- We already know the path to the package's package.json
- The TypeScript API is available
- We can require node.js 18.16+