
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
babel-plugin-optional-require
Advanced tools
Babel plugin to transpile object styles to template literal
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.
Install the plugin:
yarn add --dev babel-plugin-optional-require
Then include it in your .babelrc
:
{
"plugins": [
"optional-require"
]
}
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: []
.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.
FAQs
Babel plugin to transpile object styles to template literal
The npm package babel-plugin-optional-require receives a total of 3,413 weekly downloads. As such, babel-plugin-optional-require popularity was classified as popular.
We found that babel-plugin-optional-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.