Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-optional-require

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-optional-require

Babel plugin to transpile object styles to template literal

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.6K
decreased by-8.48%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-optional-require

Babel plugin to optionaly require modules. Useful with a bundler like 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:

yarn add --dev babel-plugin-optional-require

Then include it in your .babelrc:

{
  "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:

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.

Keywords

FAQs

Package last updated on 23 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc