-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add optional dependency support (#511)
Summary: **Summary** This PR added support for the common optional dependency pattern: enclose require() within try/catch block, which is already supported by webpack. While the lack of optional dependency support is not new, due to [lean-core](facebook/react-native#23313) initiative, it has become obvious that we should just address this to provide better customer-experience. See the excellent discussion in react-native-community/discussions-and-proposals#120. **Change Outline** The changes are mainly in 3 areas: 1. visit try/catch block and mark optional dependencies (`collectDependencies.js`) 1. during dependency resolve, if the optional dependency fails to resolve, just ignore it. (`traverseDependencies.js`) 1. add a config (`optionalDependency`, defaults to true) to disable and customize (exclude dependency from being optional) (`metro-config`) The rest are just tunneling through the new config option, added/modified tests. **Test plan** tested the new create react-native app with `react-native-vector-icons` and optional `react-native-community/toolbar-android` (in try/catch block from `react-native-vector-icons` ) : - without the optional dependency support, metro complained `react-native-community/toolbar-android` not found and abort - with the optional dependency, the app starts up without any problem. **Discussion** - if we found `import()` in the try block, should we also consider it "optional"? The common pattern is to use `require()` but one would think `import()` should be just the same, no? This PR considered any dependency found in the try block "optional", but that can be changed easily. - I put the new config `optionalDependency` in the config.resolver, not sure if it is best to be in resolver or transformer? - should we add some kind of warning message for omitted optional dependency? verbose flag? Pull Request resolved: #511 Reviewed By: rickhanlonii Differential Revision: D20280664 Pulled By: cpojer fbshipit-source-id: 8c4abebfbf2a2f4bfa65f64e2916c1e9182977f0
- Loading branch information
1 parent
32af77c
commit e54819c
Showing
9 changed files
with
343 additions
and
18 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
Oops, something went wrong.