Svelte / SvelteKit preprocessor to remove specified attributes (e.g., data-testid, data-cy) at build time. Supports spread syntax and can be enabled via environment variables.
- 🗑️ Remove attributes at build time (e.g.,
data-testid) - 🔄 Supports spread syntax (
{...props}) - 🌍 Environment variable-based toggling
pnpm add -D svelte-remove-attributes-preprocessorAdd the preprocessor to your svelte.config.js:
import { removeAttributesPreprocessor } from 'svelte-remove-attributes-preprocessor';
export default {
preprocess: [
removeAttributesPreprocessor({
attributes: ['data-testid']
})
]
};Enable/disable attribute removal via environment variable:
import { removeAttributesPreprocessor } from 'svelte-remove-attributes-preprocessor';
export default {
preprocess: [
removeAttributesPreprocessor({
envVar: 'STRIP_ATTRIBUTES', // default: 'STRIP_ATTRIBUTES'
expected: '1', // default: '1'
attributes: ['data-testid']
})
]
};Then set the environment variable when building:
STRIP_ATTRIBUTES=1 pnpm buildFor SvelteKit, add it to vite.config.js or svelte.config.js:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { removeAttributesPreprocessor } from 'svelte-remove-attributes-preprocessor';
export default defineConfig({
plugins: [sveltekit()],
// ... other config
});
// In svelte.config.js
import adapter from '@sveltejs/adapter-auto';
import { removeAttributesPreprocessor } from 'svelte-remove-attributes-preprocessor';
export default {
kit: {
adapter: adapter()
},
preprocess: [
removeAttributesPreprocessor({
attributes: ['data-testid']
})
]
};| Option | Type | Default | Description |
|---|---|---|---|
attributes |
string[] |
[] |
Array of attribute names to remove |
envVar |
string |
'STRIP_ATTRIBUTES' |
Environment variable name to check |
expected |
string |
'1' |
Expected value for the environment variable |
// svelte.config.js
import { removeAttributesPreprocessor } from 'svelte-remove-attributes-preprocessor';
export default {
preprocess: [
removeAttributesPreprocessor({
attributes: ['data-testid', 'data-cy', 'data-test']
})
]
};// Only remove attributes when STRIP_ATTRIBUTES=1
removeAttributesPreprocessor({
envVar: 'STRIP_ATTRIBUTES',
expected: '1',
attributes: ['data-testid']
})