{"id":19218,"library":"babel-plugin-transform-remove-imports","title":"babel-plugin-transform-remove-imports","description":"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.","status":"active","version":"1.8.1","language":"javascript","source_language":"en","source_url":"https://github.com/uiwjs/babel-plugin-transform-remove-imports","tags":["javascript","babel-plugin","kkt-ssr","uiw","ssr","typescript"],"install":[{"cmd":"npm install babel-plugin-transform-remove-imports","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-remove-imports","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-remove-imports","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for Babel plugin environment","package":"@babel/core","optional":false}],"imports":[{"note":"CommonJS is used as Babel plugins are typically configured via .babelrc or babel.config.js using require(). ESM default import is not supported in CJS context.","wrong":"import plugin from 'babel-plugin-transform-remove-imports'","symbol":"default","correct":"module.exports = require('babel-plugin-transform-remove-imports')"},{"note":"No default export; direct require() imports the plugin function.","wrong":"const plugin = require('babel-plugin-transform-remove-imports').default","symbol":"require('babel-plugin-transform-remove-imports')","correct":"const plugin = require('babel-plugin-transform-remove-imports')"},{"note":"TypeScript types are shipped but not documented. Use type import from 'babel-plugin-transform-remove-imports/types' only if types are needed.","wrong":"","symbol":"PluginOptions","correct":"const { PluginOptions } = require('babel-plugin-transform-remove-imports/types')"}],"quickstart":{"code":"// babel.config.js\nmodule.exports = {\n  plugins: [\n    [\n      require('babel-plugin-transform-remove-imports'),\n      {\n        test: '^lodash(\\\\/.*)?$',  // regex to match imports to remove\n      }\n    ]\n  ]\n};\n\n// Input:\nimport _ from 'lodash';\nimport merge from 'lodash/merge';\nimport { map } from 'lodash';\nimport something from 'other-lib';\n\n// Output:\nimport something from 'other-lib';","lang":"javascript","description":"Configures Babel to remove all imports matching pattern '^lodash(\\\\/.*)?$' (e.g., 'lodash' and 'lodash/merge')."},"warnings":[{"fix":"Explicitly keep required side-effect imports by adjusting the test regex or using a whitelist approach.","message":"Plugin removes import statements without checking for side effects. If a module has side effects (e.g., polyfills), removing its import may break functionality.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use double backslashes in JSON config strings: '^lodash\\\\/.*$'. Alternatively, use a RegExp object in JavaScript config.","message":"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).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to v1.5.0 or later if you need to remove CommonJS require() calls.","message":"Removal of `require()` calls was introduced in v1.5.0. Before that, only ES import statements were removed.","severity":"breaking","affected_versions":"<1.5.0"},{"fix":"Evaluate alternatives: babel-plugin-import-remove (more features) or implement a custom Babel visitor for precise control.","message":"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.","severity":"deprecated","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.5.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the 'test' property is provided and is a string or RegExp. Example: { test: '^lodash$' }","cause":"Missing or misconfigured 'test' option in plugin config.","error":"TypeError: Cannot read properties of undefined (reading 'test')"},{"fix":"Double escape backslashes: '^lodash(\\\\/.*)?$'","cause":"Unescaped backslash in JSON config string.","error":"SyntaxError: Invalid regular expression: /^lodash(\\/.*)?$/: Unterminated group"},{"fix":"Update @babel/core to version 7 or later. Run: npm install @babel/core@^7.0.0","cause":"Babel version mismatch; plugin requires @babel/core 7.x.","error":"Error: Requires Babel \"^7.0.0-0\" but was loaded with \"0.0.0\"."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}