Socket
Socket
Sign inDemoInstall

babel-plugin-optional-require

Package Overview
Dependencies
1
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

13

index.js
/* @flow */
const resolve = require('resolve-cwd');
const dirname = require('path').dirname;
const resolve = require('resolve-from');
module.exports = function rewire(babel) {
module.exports = function rewire(babel /*: any */) {
const t = babel.types;

@@ -10,3 +11,3 @@

visitor: {
TryStatement(path) {
TryStatement(path /*: any */, state /*: any */) {
path.get('block').traverse({

@@ -22,5 +23,9 @@ CallExpression(p) {

const name = p.node.arguments[0].value;
const cwd =
state.file && state.file.opts && state.file.opts.filename
? dirname(state.file.opts.filename)
: process.cwd();
try {
resolve(name);
resolve(cwd, name);
} catch (e) {

@@ -27,0 +32,0 @@ p.replaceWith(

{
"name": "babel-plugin-optional-require",
"version": "0.1.0",
"version": "0.1.1",
"description": "Babel plugin to transpile object styles to template literal",

@@ -28,4 +28,4 @@ "main": "index.js",

"dependencies": {
"resolve-cwd": "^2.0.0"
"resolve-from": "^4.0.0"
}
}

@@ -5,2 +5,4 @@ # babel-plugin-optional-require

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

@@ -34,4 +36,6 @@

} catch (e) {
// handle failure from loading the module
// 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.
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc