Socket
Socket
Sign inDemoInstall

babel-plugin-require-context-hook

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-require-context-hook

Babel plugin to replicate Webpack require.context


Version published
Weekly downloads
262K
increased by17.77%
Maintainers
1
Install size
4.43 kB
Created
Weekly downloads
 

Package description

What is babel-plugin-require-context-hook?

The babel-plugin-require-context-hook package is a Babel plugin that allows you to use Webpack's require.context in environments where it is not natively available, such as in Node.js or Jest. This can be particularly useful for testing or server-side rendering scenarios.

What are babel-plugin-require-context-hook's main functionalities?

Mocking require.context in Node.js

This feature allows you to mock Webpack's require.context in a Node.js environment. By registering the hook, you can use require.context to dynamically require files based on a pattern.

const requireContext = require('babel-plugin-require-context-hook/register');
requireContext();

const context = require.context('./path/to/files', false, /\.js$/);
context.keys().forEach(context);

Using require.context in Jest tests

This feature allows you to use require.context in Jest tests. By registering the hook, you can dynamically require test files based on a pattern, making it easier to manage and run tests.

const requireContext = require('babel-plugin-require-context-hook/register');
requireContext();

// In your Jest test file
const context = require.context('../src/components', true, /\.test\.js$/);
context.keys().forEach(context);

Other packages similar to babel-plugin-require-context-hook

Readme

Source

babel-plugin-require-context-hook

Usage

babelrc:

"plugins": [
	"require-context-hook"
]

Wherever you configure babel-register (your source file, a register script, etc):

require('babel-plugin-require-context-hook/register')();

How it works

The register script babel-plugin-require-context-hook/register implements the function require.context with an extra parameter-- the directory in which the calling file resides-- and places that function on the global scope.

The Babel plugin babel-plugin-require-context-hook rewrites all calls to require.context into calls to this global function, passing in __dirname as the extra parameter.

Keywords

FAQs

Last updated on 06 Jun 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc