babel-plugin-optional-require
Advanced tools
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. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2853
38
40
+ Addedresolve-from@^4.0.0
+ Addedresolve-from@4.0.0(transitive)
- Removedresolve-cwd@^2.0.0
- Removedresolve-cwd@2.0.0(transitive)
- Removedresolve-from@3.0.0(transitive)