Skip to content

Set transformer.allowOptionalDependencies to true by default #1348

@brentvatne

Description

@brentvatne

Describe the Feature

facebook/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

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