facebook / metro

🚇 The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.24k stars 626 forks source link

Collect isESMImport and propagate through to ResolutionContext #1376

Open robhogan opened 1 month ago

robhogan commented 1 month ago

Summary

Correct package.json#exports and package.json#imports support requires that we assert an "import" or "require" condition.

From the Node.js (where this spec originated) docs:

 "import" - matches when the package is loaded via import or import(), or via any top-level import or resolve operation by the ECMAScript module loader. Applies regardless of the module format of the target file. Always mutually exclusive with "require".
"require" - matches when the package is loaded via require(). The referenced file should be loadable with require() although the condition matches regardless of the module format of the target file. Expected formats include CommonJS, JSON, native addons, and ES modules if --experimental-require-module is enabled. Always mutually exclusive with "import".

Currently, we don't distinguish between import-ish and require-ish dependencies. This modifies collectDependencies to add isESMImport to the collected DependencyData and dependency key (so that for example import('foo') and require('foo') in the same file are considered distinct edges with potentially different resolutions).

Test Plan: Modified unit tests Tested E2E further down the stack

Changelog: [Internal]

facebook-github-bot commented 1 month ago

@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.