Allow resolving subfolders with package.json files, and improve CJS interop #143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are two changes in here, but they overlapped in their code changes a little bit so it was easiest to open it as one PR instead of dealing with conflicts.
import 'preact/hooks'
, that already works (before this PR), because Preact uses an exports map, which tells us how to resolve the./hooks
part. But many packages don't use exports maps yet, so they instead use the "old version" of this: in this case, they would create ahooks
folder with a nearly-emptypackage.json
in it, with amain
ormodule
field pointing to the file that they want to import. Before this PR, pleasantest didn't support this (it only traversed through the main package.json, e.g.node_modules/preact/package.json
). Now, if an import resolves to a folder, recursion happens to resolve through a package.json in that folder (if it exists).react
,react-dom
,prop-types
), they are really difficult to determine. Here's what the entry points for react and react-dom look like:module.exports = require(...
lines, and would then read the require'd files and statically determine the exports from those. But forprop-types
, it doesn't work because there is a function call which makes the static analysis of the require'd file break:module.exports = require(...
, and replaced it with an include-list of modules that should berequire
'd (at compile time) to determine their exports. This is used forprop-types
,react
, andreact-dom
. We can easily add more to that list down the road. If facebook eventually decides to add ESM to their packages, this should continue working without the compile-timerequire
because it checks if the package is CJS before requiring it.