Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set transformer.allowOptionalDependencies to true by default #1348

Closed
brentvatne opened this issue Jan 16, 2021 · 4 comments
Closed

Set transformer.allowOptionalDependencies to true by default #1348

brentvatne opened this issue Jan 16, 2021 · 4 comments

Comments

@brentvatne
Copy link
Contributor

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

@thymikee
Copy link
Member

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

@brentvatne
Copy link
Contributor Author

brentvatne commented Jan 20, 2021

@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

@thymikee
Copy link
Member

Yup, we can do it!

@brentvatne
Copy link
Contributor Author

merged in #1350

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

No branches or pull requests

2 participants