Babel Plugin Remove Object Properties
raw JSON → 1.0.2 verified Fri May 01 auth: no javascript
Babel plugin for stripping object properties from AST nodes that match a supplied regex pattern. The current stable version is 1.0.2, released with low release cadence. It works during Babel's transpilation phase, removing nodes of type 'ObjectProperty' via regex. Unlike generic Babel visitors or AST manipulation utilities, this plugin provides a simple configurable approach to remove object properties (e.g., data-* attributes in React). Suitable for cleaning test attributes from production builds. Has minimal dependencies and is distributed on npm.
Common errors
error TypeError: Cannot read properties of undefined (reading 'type') ↓
cause Plugin not receiving options object correctly in .babelrc.
fix
Use double array syntax: "plugins": [["remove-object-properties", { "regexp": "data-*" }]]
error Error: [BABEL] unknown: Cannot find module 'babel-plugin-remove-object-properties' ↓
cause Plugin not installed or not in node_modules.
fix
Run npm install babel-plugin-remove-object-properties --save-dev
error The regexp option must be a string ↓
cause Passing regex literal instead of string in config.
fix
Use string form: "regexp": "data-*" (not /data-*/)
Warnings
gotcha The regex is applied to the property key, not the entire property string. Ensure regex matches exactly the property name (e.g., 'data-*' matches 'data-' followed by any chars, but not 'data-test' without wildcard? Actually 'data-*' matching? Use '^data-' to be safe. ↓
fix Use a proper regex like '^data-' to match keys starting with 'data-'.
gotcha Plugin removes all object properties matching the regex, including non-JSX object properties; be aware it's not limited to JSX attributes. ↓
fix Restrict usage to specific files or conditions if unwanted removal occurs.
deprecated The package has not been updated since 2017; may not work with Babel 7 or newer without adjustments. ↓
fix Test with your Babel version; consider using modern alternatives like babel-plugin-strip-sourcemap or custom visitor.
Install
npm install babel-plugin-remove-object-properties yarn add babel-plugin-remove-object-properties pnpm add babel-plugin-remove-object-properties Imports
- default wrong
import babelPluginRemoveObjectProperties from 'babel-plugin-remove-object-properties'correctNo import needed; configured via .babelrc or programmatic API. - plugin reference in .babelrc wrong
"plugins": ["babel-plugin-remove-object-properties"]correct"plugins": [["remove-object-properties", { "regexp": "data-*" }]] - programmatic usage wrong
require('babel-plugin-remove-object-properties')({ regexp: 'data-*' })correctrequire('@babel/core').transform(code, { plugins: [[require('babel-plugin-remove-object-properties'), { regexp: 'data-*' }]] })
Quickstart
// In .babelrc or babel.config.js
// Remove all properties matching regex 'data-*'
{
"plugins": [["remove-object-properties", { "regexp": "data-*" }]]
}
// Before transpilation:
<div data-id="test">Hello</div>
// After transpilation:
<div>Hello</div>