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

babel-plugin-rewrite-require

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-rewrite-require - npm Package Compare versions

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 &&

2

package.json
{
"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

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