Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/satya164/babel-plugin-optional-require
Babel plugin to optionaly require modules
https://github.com/satya164/babel-plugin-optional-require
babel babel-plugin bundler hacktoberfest
Last synced: 9 days ago
JSON representation
Babel plugin to optionaly require modules
- Host: GitHub
- URL: https://github.com/satya164/babel-plugin-optional-require
- Owner: satya164
- Created: 2018-04-18T12:05:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-17T17:30:22.000Z (almost 6 years ago)
- Last Synced: 2024-10-12T17:42:25.123Z (24 days ago)
- Topics: babel, babel-plugin, bundler, hacktoberfest
- Language: JavaScript
- Homepage:
- Size: 147 KB
- Stars: 53
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# babel-plugin-optional-require
Babel plugin to optionaly require modules. Useful with a bundler like [Metro](https://github.com/facebook/metro) which doesn't support optional `require` statements.
This is primarily useful if you want add an dependency to your library that's optional and you want users to be able to opt-out of it to save bundle size. You can also use it in apps to load configuration files only if they exist.
## Usage
Install the plugin:
```sh
yarn add --dev babel-plugin-optional-require
```Then include it in your `.babelrc`:
```json
{
"plugins": [
"optional-require"
]
}
```## Options
- `builtins: boolean`: Whether to resolve Node builtins. Default: `false`.
- `blacklist: string[]`: List of modules we assume to be unavailable without resolving. Default: `[]`.
- `whitelist: string[]`: List of modules we assume to be available without resolving. Default: `[]`.## Example
To optionally `require` a module, you need to wrap it in `try/catch`:
```js
let a;try {
a = require('optional-module');
} catch (e) {
// Handle failure from loading the module
}
```If the module `optional-module` doesn't exist, the `require` call will be replaced with an IIFE that throws an error, so you can catch it and handle it at runtime. Otherwise, the code is left unchanged.