Streamlining Vue.js Production Builds with a Custom Vite Plugin: Removing Unnecessary Attributes

Mustafa Dalga
heybooster
Published in
3 min readMay 20, 2023

Introduction:

In the dynamic world of Vue.js application development, testing is a cornerstone to ensure the quality and reliability of our applications. When testing our Vue components, we often use the Testing Library, which provides a simple and complete testing solution. One typical practice when using this library is adding ‘data-testid’ attributes to our HTML elements for easier selection. However, these attributes, while essential for testing, become superfluous in our production builds, potentially cluttering our HTML and making it less readable. The question then becomes, how do we efficiently remove these attributes for production, while still maintaining their usefulness during testing?

In this blog post, I am excited to share how I addressed this issue and crafted a solution that not only resolved the problem for my use case, but also provides flexibility for other similar scenarios.

The Problem:

As our Vue.js application grew, we found ourselves incorporating a vast number of ‘data-testid’ attributes across our components. These attributes allowed us to write comprehensive tests, ensuring our components function as expected. However, while these attributes are invaluable during the testing phase, they serve no purpose in the production environment and can lead to cluttered and bloated code.

Solution:

To tackle this challenge, I developed a custom Vite plugin that removes specified attributes before the Vue compiler runs, specifically during the production build process. This approach ensures our production code remains clean, efficient, and free of unnecessary ‘data-testid’ attributes.

Introducing the Vite Plugin — Remove Attributes

My new Vite plugin, “Remove Attributes” is designed specifically for Vue.js projects. Its primary function is to remove specified attributes during production builds. However, its capabilities extend beyond just ‘data-testid’ attributes. It can remove any attribute from any file type, be it ‘.vue’, ‘.ts’, or ‘.js’, providing users with comprehensive control over their production code. Additionally, the plugin can be configured to ignore certain files or folders, ensuring that only the necessary code is affected.

Using the Plugin:

Installing and integrating our plugin into your Vue.js project is straightforward. Here’s how you can do it:

Install the plugin via npm:

npm install --save-dev remove-attr

Import and use the plugin in your vite.config.js or vite.config.ts file.

Here’s an example configuration that will remove ‘data-testid’ attributes from all ‘.vue’ files during production builds:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import removeAttr from 'remove-attr'

export default defineConfig({
plugins: [
vue(),
removeAttr({
extensions: [ 'vue' ],
attributes: [ 'data-testid' ]
})
]
})

Moreover, the plugin allows for advanced configurations. Here’s an example of a configuration that removes ‘data-testid’ and ‘data-id’ attributes from ‘.vue’, ‘.ts’, and ‘.js’ files, except for those in specified folders or files:

export default defineConfig({
plugins: [
vue(),
removeAttr({
extensions: [ 'vue', 'ts', 'js' ],
attributes: [ 'data-testid', 'data-id' ],
ignoreFolders: [ 'src/tests', 'src/utilities' ],
ignoreFiles: [ 'Home.vue', 'src/components/Modal.vue', 'src/layouts/LayoutAuth.vue' ]
})
]
})

In Conclusion:

By developing a custom Vite plugin, we were able to address the problem of redundant ‘data-testid’ attributes cluttering our production code. The flexibility of this plugin allows it to be used for different purposes, further enhancing its value in the Vue.js development environment. This experience underscores the power of custom tools in improving our development workflow, while simultaneously highlighting the importance of clean, efficient production code.

With our new “Remove Attributes” Vite plugin, we’ve transformed a challenge into a tool that benefits not only our project but potentially any Vue.js project that faces a similar concern. Whether you want to remove ‘data-testid’ attributes or any other specified attributes from your code, our Vite plugin provides a flexible and efficient solution.

So, next time you find unnecessary attributes cluttering your production code, remember that a tailored solution is within your reach. Take it from my experience: with the right tool, you can ensure that your code remains clean, manageable, and efficient, no matter how complex your project becomes.

--

--

Mustafa Dalga
heybooster

Software Developer | Nature and Animal Photographer