babel-plugin-mock-imports

raw JSON →
1.2.0 verified Sat Apr 25 auth: no javascript

Babel plugin for redirecting import statements to different locations. Current version 1.2.0, first released as 0.0.1. Key differentiator: simple configuration via RegExp patterns, useful for mocking modules or aliasing imports. Stable API with minimal changes.

error Error: .babelrc plugin is not a string or object/fn
cause Plugins array item is incorrectly formatted.
fix
Use an array: ['mock-imports', { redirects: [...] }]
error TypeError: Cannot read property 'redirects' of undefined
cause Plugin options object is missing or incorrect.
fix
Ensure options object has 'redirects' array.
gotcha Pattern strings are evaluated as RegExp; special characters must be escaped correctly.
fix Escape regex special characters in pattern strings, or use strings that are safe for RegExp.
gotcha Plugin replaces import source strings but does not transform default/namespace imports; ensure mock modules export the same interface.
fix Ensure mock modules export the same symbols as the original modules.
npm install babel-plugin-mock-imports
yarn add babel-plugin-mock-imports
pnpm add babel-plugin-mock-imports

Redirects all .svg imports to a mock component.

module.exports = {
  plugins: [
    ['mock-imports', {
      redirects: [
        {
          pattern: '.svg$',
          location: 'path/to/mocked/react/component'
        }
      ]
    }]
  ]
};