babel-plugin-transform-remove-imports

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

A Babel plugin to remove specific import/require declarations during transpilation. Current stable version is 1.8.1 (2024). Release cadence is irregular; updates address bug fixes and support for require(). Key differentiator: allows conditional removal of imports based on regex patterns, useful for SSR builds or tree-shaking. Small bundle size (~3KB). Peer dependency on @babel/core ^7.0.0. Ships TypeScript types.

error TypeError: Cannot read properties of undefined (reading 'test')
cause Missing or misconfigured 'test' option in plugin config.
fix
Ensure the 'test' property is provided and is a string or RegExp. Example: { test: '^lodash$' }
error SyntaxError: Invalid regular expression: /^lodash(\/.*)?$/: Unterminated group
cause Unescaped backslash in JSON config string.
fix
Double escape backslashes: '^lodash(\\/.*)?$'
error Error: Requires Babel "^7.0.0-0" but was loaded with "0.0.0".
cause Babel version mismatch; plugin requires @babel/core 7.x.
fix
Update @babel/core to version 7 or later. Run: npm install @babel/core@^7.0.0
gotcha Plugin removes import statements without checking for side effects. If a module has side effects (e.g., polyfills), removing its import may break functionality.
fix Explicitly keep required side-effect imports by adjusting the test regex or using a whitelist approach.
gotcha The `test` option is a string (regex pattern) or RegExp object. If passing a string, ensure proper escaping of backslashes in JSON/babel config (e.g., use '^lodash\/.*$' as string).
fix Use double backslashes in JSON config strings: '^lodash\\/.*$'. Alternatively, use a RegExp object in JavaScript config.
breaking Removal of `require()` calls was introduced in v1.5.0. Before that, only ES import statements were removed.
fix Upgrade to v1.5.0 or later if you need to remove CommonJS require() calls.
deprecated No deprecation warnings as of v1.8.1, but the plugin is not actively maintained (last release 2024, few updates). Consider using babel-plugin-import-remove or custom visitor if more features needed.
fix Evaluate alternatives: babel-plugin-import-remove (more features) or implement a custom Babel visitor for precise control.
gotcha Plugin removes both `import` and `require()` for the same module if pattern matches. This can accidentally remove dynamic require() calls if they match the pattern.
fix Use a more specific test pattern to avoid catching dynamic requires. For example, if only static imports need removal, use a pattern that includes 'import' but not 'require'? Not directly supported; consider using regex negative lookahead.
npm install babel-plugin-transform-remove-imports
yarn add babel-plugin-transform-remove-imports
pnpm add babel-plugin-transform-remove-imports

Configures Babel to remove all imports matching pattern '^lodash(\\/.*)?$' (e.g., 'lodash' and 'lodash/merge').

// babel.config.js
module.exports = {
  plugins: [
    [
      require('babel-plugin-transform-remove-imports'),
      {
        test: '^lodash(\\/.*)?$',  // regex to match imports to remove
      }
    ]
  ]
};

// Input:
import _ from 'lodash';
import merge from 'lodash/merge';
import { map } from 'lodash';
import something from 'other-lib';

// Output:
import something from 'other-lib';