rollup-plugin-jsx-remove-attributes
raw JSON → 3.1.2 verified Mon Apr 27 auth: no javascript
A Rollup and Vite plugin to remove specified JSX attributes (e.g., data-testid) from production builds, reducing bundle size. Current version 3.1.2, requires Node >=20.8.0. Supports Rollup and Vite, with Preact support added in v3.1.0. Key differentiator: simple configuration for stripping test-only attributes without Babel plugin overhead. Active development, peer deps include @rollup/pluginutils, astring, estree-walker, and Vite >=5.2. Ships TypeScript types.
Common errors
error TypeError: removeTestIdAttribute is not a function ↓
cause Using named import instead of default import
fix
Change to: import removeTestIdAttribute from 'rollup-plugin-jsx-remove-attributes'
error Error: The plugin only works when NODE_ENV is set to 'production' or specified environments. ↓
cause NODE_ENV not set or not in environments array
fix
Ensure NODE_ENV=production or adjust environments option.
error ENOENT: no such file or directory, open '.../node_modules/rollup-plugin-jsx-remove-attributes/package.json' ↓
cause Missing or incomplete installation
fix
Run npm install again and verify peer dependencies.
Warnings
breaking Requires Node >=v20.8.0; older Node versions will fail to install or run. ↓
fix Upgrade Node to v20.8.0 or later.
deprecated Version 2.x used a different API (options object keys). v3 changed to attributes array and environments option. ↓
fix Migrate to v3 API.
gotcha The plugin only runs when NODE_ENV matches an environment in the environments array (default ['production']). No attributes removed in dev. ↓
fix Set NODE_ENV or use 'environments' option to include 'development' if needed.
Install
npm install rollup-plugin-jsx-remove-attributes yarn add rollup-plugin-jsx-remove-attributes pnpm add rollup-plugin-jsx-remove-attributes Imports
- removeTestIdAttribute wrong
import { removeTestIdAttribute } from 'rollup-plugin-jsx-remove-attributes'correctimport removeTestIdAttribute from 'rollup-plugin-jsx-remove-attributes' - removeTestIdAttribute wrong
const { removeTestIdAttribute } = require('rollup-plugin-jsx-remove-attributes')correctconst removeTestIdAttribute = require('rollup-plugin-jsx-remove-attributes') - PluginOptions
import type { PluginOptions } from 'rollup-plugin-jsx-remove-attributes'
Quickstart
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import removeTestIdAttribute from 'rollup-plugin-jsx-remove-attributes';
export default defineConfig({
plugins: [
react(),
removeTestIdAttribute({
include: [/\.tsx$/],
exclude: ['node_modules/**'],
attributes: ['data-testid', 'data-cy'],
environments: ['production', 'staging'],
usage: 'vite'
})
]
});