rollup-plugin-require-context

raw JSON →
1.0.1 verified Mon Apr 27 auth: no javascript

A Rollup plugin that resolves webpack-style require-context calls, enabling dynamic module loading in Rollup bundles. Current stable version is 1.0.1, with irregular releases. Key differentiators: allows migration from webpack to Rollup without rewriting require-context usage, supports RegExp filtering and recursive directory traversal. Alternative to manual import() or glob imports.

error Error: Could not resolve require.context from 'path/to/file.js'
cause The directory or regex pattern does not match any existing files or the path is malformed.
fix
Ensure directory exists relative to the file and the regex is valid. Use absolute path if needed.
error TypeError: requireContext is not a function
cause Incorrect import style; possibly using named import when only default exists.
fix
Use default import: import requireContext from 'rollup-plugin-require-context'
gotcha The plugin resolves require-context at build time; dynamic expressions (e.g., require.context('../' + folder)) may not work correctly.
fix Use static directory and RegExp patterns. Avoid dynamic variables in the directory argument.
gotcha Recursive mode (true) includes all subdirectories; may include unintended files or cause large bundles.
fix Specify a narrow directory or use a strict RegExp filter.
npm install rollup-plugin-require-context
yarn add rollup-plugin-require-context
pnpm add rollup-plugin-require-context

Shows basic setup of the plugin in a Rollup config to enable require-context resolution.

import requireContext from 'rollup-plugin-require-context';

export default {
  input: 'main.js',
  output: { file: 'bundle.js', format: 'iife' },
  plugins: [
    requireContext({
      // options (optional)
    })
  ]
};