npm.io
0.1.0 • Published 6d ago

unplugin-strip-attrs

Licence
MIT
Version
0.1.0
Deps
1
Size
33 kB
Vulns
0
Weekly
8

unplugin-strip-attrs

CI NPM VERSION NPM DOWNLOADS LICENSE

Remove testing-related attributes (such as data-testid and data-cy) from templates during build.

It supports JSX/TSX, Vue, Svelte, HTML, and also Astro/MDX by default.

Install

npm i unplugin-strip-attrs -D
yarn add unplugin-strip-attrs -D
pnpm add unplugin-strip-attrs -D

Usage

Vite
// vite.config.ts
import StripAttrs from 'unplugin-strip-attrs/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [StripAttrs()],
})


unloader
// unloader.config.ts
import { defineConfig } from 'unloader'
import StripAttrs from 'unplugin-strip-attrs/unloader'

export default defineConfig({
  plugins: [StripAttrs()],
})


Rollup
// rollup.config.js
import { defineConfig } from 'rollup'
import StripAttrs from 'unplugin-strip-attrs/rollup'

export default defineConfig({
  plugins: [StripAttrs()],
})


Rolldown/TsDown
// rolldown.config.ts/tsdown.config.ts
import { defineConfig } from 'rolldown'
// import { defineConfig } from 'tsdown'
import StripAttrs from 'unplugin-strip-attrs/rolldown'

export default defineConfig({
  plugins: [StripAttrs()],
})


esbuild
// esbuild.config.js
import { build } from 'esbuild'

build({
  plugins: [require('unplugin-strip-attrs/esbuild')()],
})


Webpack
// webpack.config.js
module.exports = {
  plugins: [require('unplugin-strip-attrs/webpack')()],
}


Rspack
// rspack.config.ts
import { defineConfig } from '@rspack/cli'
import StripAttrs from 'unplugin-strip-attrs/rspack'

export default defineConfig({
  plugins: [StripAttrs()],
})


Farm
// farm.config.ts
import { defineConfig } from '@farmfe/core'
import StripAttrs from 'unplugin-strip-attrs/farm'

export default defineConfig({
  plugins: [StripAttrs()],
})


What It Does

  • Removes common testing attributes: data-testid and data-cy.
  • Processes these file types by default: jsx, tsx, vue, svelte, html, astro, mdx
  • Supports extending removable attributes via attrs (string or RegExp)
  • Supports preserving attributes by value pattern via keepValues
  • Supports advanced keep rules via keepAttrs (string, RegExp, or callback)
  • Supports skipping specific tags via ignoreTagNames (default includes template)

Options

export interface Options {
  enforce?: 'post' | 'pre'
  include?: FilterPattern
  exclude?: FilterPattern

  // Attributes to remove
  attrs?: Array<string | RegExp>

  // Keep attributes even when they match attrs
  keepAttrs?: Array<
    | string
    | RegExp
    | ((ctx: {
        id: string
        attrName: string
        attrValue: string | undefined
      }) => boolean)
  >

  // Keep attributes when value matches one of these patterns
  keepValues?: Array<string | RegExp>

  // Skip stripping in specific tag names
  ignoreTagNames?: Array<string | RegExp>

  // Whether attribute matching is case-sensitive
  caseSensitive?: boolean

  root?: string
}

Default Options

{
  enforce: 'post',
  include: [/\.(?:[cm]?[jt]sx|vue|svelte|html|astro|mdx)$/i],
  exclude: [/\.d\.ts$/],
  attrs: [
    'data-testid',
    'data-cy',
  ],
  keepAttrs: [],
  keepValues: [],
  ignoreTagNames: ['template'],
  caseSensitive: false,
}

Examples

Extend removable attributes
import StripAttrs from 'unplugin-strip-attrs/vite'

export default {
  plugins: [
    StripAttrs({
      attrs: ['data-testid', 'data-cy', /^data-e2e-/],
    }),
  ],
}
Keep attributes by value pattern
import StripAttrs from 'unplugin-strip-attrs/vite'

export default {
  plugins: [
    StripAttrs({
      keepValues: [/^keep:/],
    }),
  ],
}

Input:

<button
  data-testid="keep:primary"
  data-cy="remove-me"
>
  Save
</button>

Output:

<button data-testid="keep:primary">Save</button>
Keep attributes with callback rules
import StripAttrs from 'unplugin-strip-attrs/vite'

export default {
  plugins: [
    StripAttrs({
      keepAttrs: [ctx => ctx.attrName === 'data-cy'],
      ignoreTagNames: ['template'],
    }),
  ],
}
Extend supported template file types
import StripAttrs from 'unplugin-strip-attrs/vite'

export default {
  plugins: [
    StripAttrs({
      include: [/\.(?:[cm]?[jt]sx|vue|svelte|html|astro|mdx|hbs|handlebars)$/i],
    }),
  ],
}

License

MIT License 2026-PRESENT ntnyq

Keywords