babel-plugin-rewrite-require
Advanced tools
Comparing version 1.9.1 to 1.13.0
27
index.js
@@ -11,2 +11,7 @@ /** | ||
* }, | ||
* "throwForModules": [ | ||
* "node-native-stuff", | ||
* "optional-dependency", | ||
* "never-available", | ||
* ], | ||
* "throwForMissingFiles": [ | ||
@@ -86,5 +91,21 @@ * "/path/to/optional/configuration.json", | ||
// If the require() argument points to a missing file that's | ||
// whitelisted, replace the require() call with an exception | ||
// being thrown. | ||
// If the require() argument is a module that's blacklisted as | ||
// never being resolvable, replace the require() call with an | ||
// exception being thrown. This mimics webpack's default behavior | ||
// for missing modules, but requires consumers to explicitly | ||
// blacklist every affected module rather than being the default. | ||
if (opts.throwForModules && | ||
opts.throwForModules.length && | ||
opts.throwForModules.indexOf(arg.value) !== -1) { | ||
nodePath.replaceWith( | ||
throwNewError(t, 'Could not resolve: ' + arg.value) | ||
); | ||
return; | ||
} | ||
// If the require() argument points to a missing file whitelisted | ||
// for being optional, replace the require() call with an exception | ||
// being thrown. This is similar to `throwForModules`, but (a) for | ||
// relative imports and (b) will not throw if the referenced file is | ||
// in fact present. | ||
if (opts.throwForMissingFiles && | ||
@@ -91,0 +112,0 @@ opts.throwForMissingFiles.length && |
{ | ||
"name": "babel-plugin-rewrite-require", | ||
"version": "1.9.1", | ||
"version": "1.13.0", | ||
"description": "Babel plugin for rewriting requires/imports", | ||
@@ -5,0 +5,0 @@ "repository": "silklabs/silk", |
# Babel plugin for rewriting requires/imports | ||
## Module aliases | ||
This plugin allows rewriting ES6 module imports and CommonJS-style | ||
@@ -15,5 +17,8 @@ `require()` calls using a simple module alias map: | ||
## Non-string literals | ||
With the following option enabled, `require()` calls that do not have | ||
a string literal argument (e.g. `require('cry'+'pto') will be replaced | ||
with an exception being thrown: | ||
a simple string literal argument will be replaced with an exception | ||
being thrown: | ||
@@ -26,2 +31,40 @@ ```json | ||
This approach is used by several browserify modules to detect whether | ||
their built-in counterparts are available (e.g. `require('cry'+'pto')`) | ||
and should be enabled if you use this Babel plugin to alias node | ||
built-in modules to browserify modules. | ||
## Optional modules | ||
A common pattern found in node modules is to check whether a certain | ||
dependency is available: | ||
```js | ||
try { | ||
require('some-optional-dependency'); | ||
} catch (ex) { | ||
// Ignore, or load polyfill, or ... | ||
} | ||
``` | ||
Because React Native's packager resolves `require()` calls during | ||
dependency resolution, it will require `'some-optional-dependency'` to | ||
be present and resolvable. If this module will never be available to | ||
your React Native app, and you want the runtime exception occur so | ||
that the `catch` clause can do its thing, you can blacklist these | ||
dependencies from ever being resolved. Instead, those `require()` | ||
calls will be replaced with an exception being thrown: | ||
```json | ||
{ | ||
"throwForModules": [ | ||
"some-optional-dependency" | ||
] | ||
} | ||
``` | ||
## Optional files | ||
If the file that an import or `require()` call would resolve to is | ||
@@ -28,0 +71,0 @@ missing, it's usually up to node or the packager (e.g. webpack) to |
6588
114
83