Registry / devops / babel-plugin-require-context-hook-babel7

babel-plugin-require-context-hook-babel7

JSON →
library1.0.0jsnpmunverified

A Babel plugin that replicates Webpack's `require.context` functionality for use with `babel-register`. Version 1.0.0 is stable; the plugin rewrites `require.context()` calls to use a global polyfill that must be registered at runtime via `require('babel-plugin-require-context-hook/register')()`. It depends on `@babel/core ^7.2.2`. This is a minimal, focused alternative to full Webpack configurations for projects needing dynamic require in Node.js.

npm install babel-plugin-require-context-hook-babel7
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REQUI
B
babel-plugin-require-context-hook-babel7
devopsjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ContextPolyfill
require('babel-plugin-require-context-hook/register')()
import 'babel-plugin-require-context-hook/register'
The register script must be required and called as a function. ES imports will not invoke the function.
require.context
require.context(directory, useSubdirectories, regExp)
require.context(directory)
All three arguments are required in the polyfill; the original Webpack signature allows omitting. The plugin rewrites calls to include __dirname.
babel-plugin syntax
"plugins": ["require-context-hook"]
"plugins": ["babel-plugin-require-context-hook"]
In .babelrc, use the short name (without 'babel-plugin-') to avoid double prefixing.

Demonstrates setup: add plugin to .babelrc, register polyfill at entry point, then use require.context with three arguments.

// .babelrc file { "plugins": ["require-context-hook"] } // entry.js - must run before any module that uses require.context require('babel-plugin-require-context-hook/register')(); // usage in a module const modules = require.context('./components', true, /\.js$/); modules.keys().forEach(key => { const mod = modules(key); console.log('Loaded module:', mod); }); // Then run with: node -r @babel/register entry.js
Debug
Known issues
gotchaRegister function must be called before any require.context usage. If called after, the global polyfill is not set.
fix
Call require('babel-plugin-require-context-hook/register')() at the very top of your entry file, before requiring any module that uses require.context.
affects: >=1.0.0
gotchaMissing third argument (regExp) in require.context call will cause a runtime error because the polyfill expects exactly three arguments.
fix
Always provide all three arguments: require.context(directory, useSubdirectories, regExp). If you don't care about a filter, use /^./.
affects: >=1.0.0
breakingThe 'babel-plugin-' prefix in .babelrc plugins list may cause double registration and break plugin detection.
fix
Use the short plugin name: "require-context-hook" without the 'babel-plugin-' prefix.
affects: <=1.0.0
Errors
Common errors & fixes
Error: require.context is not a function
The register script has not been called, or was called after modules using require.context were loaded.
fix
Add require('babel-plugin-require-context-hook/register')() at the very top of your entry file, before any other require calls.
TypeError: Cannot read property 'replace' of undefined
The third argument (regExp) to require.context is missing; the polyfill regex conversion expects a regex string.
fix
Provide a third argument to require.context, e.g., require.context('./', true, /\.js$/).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
@babel/corerequiredRequired as a peer dependency for Babel plugin functionality.
Agent activity
12 hits · last 30 days
node
12
Resources
babel-plugin-require-context-hook-babel7 — npm install babel-plugin-require-context-hook-babel7 · libregistry