https://github.com/facebook/metro/pull/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 (https://github.com/react-native-community/discussions-and-proposals/issues/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.
We would just need to add allowOptionalDependencies: true to the default transformer config in https://github.com/react-native-community/cli/blob/22a3c2558c2e03cc61f088807d2b09d1567c07ca/packages/cli/src/tools/loadMetroConfig.ts#L108-L116
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.
Sure, sounds reasonable and non-breaking. Happy to accept a PR :)
@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 [email protected] (the first release that includes this feature - it shipped in March of last year) to a CLI version that is compatible with [email protected]? we're hoping to leverage this functionality to move away from .expo.js extensions
Yup, we can do it!
merged in #1350
Most helpful comment
Yup, we can do it!