-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distinguish source ESM imports and propagate isESMImport to Resolutio…
…nContext (#1376) Summary: Correct `package.json#exports` and `package.json#imports` support requires that we assert an `"import"` or `"require"` [condition](https://nodejs.org/docs/latest-v22.x/api/packages.html#conditional-exports). 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), and exposes it as an (optional - for backwards compatibility) field on `ResolutionContext`. Changelog: ``` - **[Feature]**: Add `isESMImport` to `ResolutionContext` to distinguish resolution based on whether the dependency is a `require()` or static/async `import`. ``` Pull Request resolved: #1376 Test Plan: New unit tests for `collectDependencies` E2E after using this to assert conditions for `unstable_enablePackageExports`, see D70462551: {F1975581656} ```js1 jest xplat/js/tools/metro/packages/metro/src/ModuleGraph/worker/__tests__/collectDependencies-test.js -t isESMImport Determining test suites to run... PASS xplat/js/tools/metro/packages/metro/src/ModuleGraph/worker/__tests__/collectDependencies-test.js ○ skipped collects unique dependency identifiers and transforms the AST ○ skipped uses dependencyMapName parameter as-is if provided ○ skipped collects asynchronous dependencies ○ skipped collects asynchronous dependencies with keepRequireNames: false ○ skipped distinguishes sync and async dependencies on the same module ○ skipped distinguishes sync and async dependencies on the same module; reverse order ○ skipped exposes a string as `dependencyMapName` even without collecting dependencies ○ skipped ignores require functions defined defined by lower scopes ○ skipped collects imports ○ skipped collects export from ○ skipped records locations of dependencies ○ skipped integration: records locations of inlined dependencies (Metro ESM) ○ skipped integration: records locations of inlined dependencies (Babel ESM) ○ skipped uses the dependency transformer specified in the options to transform the dependency calls ○ skipped collects require.resolveWeak calls require.context ○ skipped does not extract/transform if feature is disabled ○ skipped can omit 2nd-4th arguments ○ skipped can pass undefined for 2nd-4th arguments ○ skipped can understand constant assignments ○ skipped can understand regex constant assignments ○ skipped distinguishes require from require.context ○ skipped distinguishes require.context based on path ○ skipped distinguishes require.context based on trailing slash in path ○ skipped distinguishes between backslash and slash in path ○ skipped distinguishes require.context based on `recursive` ○ skipped distinguishes require.context based on filter pattern ○ skipped distinguishes require.context based on filter flags ○ skipped distinguishes require.context based on mode ○ skipped asserts invalid first argument ○ skipped asserts invalid second argument ○ skipped asserts invalid third argument ○ skipped asserts invalid fourth argument ○ skipped asserts invalid fourth argument enum value ○ skipped asserts too many arguments ○ skipped asserts no arguments import() prefetching ○ skipped collects prefetch calls ○ skipped keepRequireNames: false ○ skipped distinguishes between import and prefetch dependncies on the same module require.unstable_importMaybeSync() ○ skipped collects require.unstable_importMaybeSync calls ○ skipped keepRequireNames: false ○ skipped distinguishes between require.unstable_importMaybeSync and prefetch dependencies on the same module Evaluating static arguments ○ skipped supports template literals as arguments ○ skipped supports template literals with static interpolations ○ skipped throws template literals with dyncamic interpolations ○ skipped throws on tagged template literals ○ skipped supports multiple static strings concatenated ○ skipped supports concatenating strings and template literasl ○ skipped supports using static variables in require statements ○ skipped throws when requiring non-strings ○ skipped throws at runtime when requiring non-strings with special option optional dependencies ○ skipped dependency in try-block within 1-level will be optional ○ skipped nested try-block follows the inner-most scope ○ skipped can handle single-line statement ○ skipped independent of sibling context ○ skipped ignores require functions defined by lower scopes ○ skipped supports using static variables in require statements ○ skipped can exclude optional dependency ○ skipped collapses optional and non-optional requires of the same module isESMImport ✓ distinguishes require calls, static imports and async imports (17 ms) Test Suites: 1 passed, 1 total Tests: 58 skipped, 1 passed, 59 total Snapshots: 0 total Time: 0.501 s, estimated 1 s Ran all test suites matching /xplat\/js\/tools\/metro\/packages\/metro\/src\/ModuleGraph\/worker\/__tests__\/collectDependencies-test.js/i with tests matching "isESMImport". ``` Reviewed By: huntie Differential Revision: D64981658 Pulled By: robhogan fbshipit-source-id: 6ac3fa84a65fa126ce8dba85ce2093d103706ad0
- Loading branch information
1 parent
f0d4a00
commit d187fb2
Showing
26 changed files
with
457 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.