Skip to content

Set transformer.allowOptionalDependencies to true by default #1348

Closed

Description

@brentvatne

Describe the Feature

react/metro#511 added support for allowOptionalDependencies to Metro. This allows us write code like this:

try {
  require('does-not-exist');
} catch {
  console.log('could not load does-not-exist')
}

This is particularly useful for library authors, and would have been helpful during the "lean core" efforts (react-native-community/discussions-and-proposals#120) but will continue to be useful in environments where libraries want to either: a) support multiple implementations of some primitive and automatically switch between them depending on which one the developer uses b) provide some behavior in their library that works if the developer has installed an optional peer dependency or fallback to something else and/or provide some warning otherwise.

Possible Implementations

We would just need to add allowOptionalDependencies: true to the default transformer config in

transformer: {
babelTransformerPath: require.resolve(
'metro-react-native-babel-transformer',
),
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
asyncRequireModulePath: require.resolve(
'metro-runtime/src/modules/asyncRequire',
),
},

This should not break any existing apps, but libraries that adopt optional imports will only work on newer versions of Metro/this CLI that use this config.

Related Issues

Activity

  1. thymikee commented on Jan 19, 2021

    @thymikee
    Member

    Sure, sounds reasonable and non-breaking. Happy to accept a PR :)

  2. brentvatne commented on Jan 20, 2021

    @brentvatne
    ContributorAuthor

    @thymikee - will open a PR for this shortly!

    one more question - do you think it would be possible to backport this and a version bump to depend on metro@0.59 (the first release that includes this feature - it shipped in March of last year) to a CLI version that is compatible with react-native@0.63? we're hoping to leverage this functionality to move away from .expo.js extensions

  3. thymikee commented on Jan 20, 2021

    @thymikee
    Member

    Yup, we can do it!

  4. brentvatne commented on Feb 6, 2021

    @brentvatne
    ContributorAuthor

    merged in #1350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions