Vue.js: Redundant CSS classes (CSS components) are created when importing the same CSS file

I am currently developing an App using Vue.js and CSS Components. Within my Vue components, I have noticed that some share similar styling.

In my Hello.vue component:

<template>
  <div :class="$style.title">Hello, World</div>
</template>
<script>
export default {}
</script>
<style module src="./common.css"></style>

In my GoodBye.vue component:

<template>
  <div :class="$style.title">Goodbye, cruel world.</div>
</template>
<script>
export default {}
</script>
<style module src="./common.css"></style>

The common.css file looks like this:

.title {
  font-weight: bold;
}

After compiling and running the code, I encountered a CSS class duplication issue (._2miFMUAEBLdLB9wHpgrYF2):

Screenshot showing duplicated CSS classes

Is there a way to resolve this duplication problem?

You can find the complete code here

Answer №1

Last night I delved into this topic! I am in the process of grasping the most effective strategies, and I'll share my findings with you thus far.

Your current setup may result in redundant CSS output during development mode, but in production, it gets deduplicated thanks to the configuration in the webpack.prod.conf.js file.

new OptimizeCSSPlugin({
  cssProcessorOptions: config.build.productionSourceMap
  ? { safe: true, map: { inline: false } }
  : { safe: true }
}),

Nevertheless, it still leads to duplicate hashes in your app.js, which can lead to rapid bloat, regardless of naming conventions.

At the moment, I'm inclined towards having a universal stylesheet in the root component with simple non-scoped styles, while each component has its own unique styles with module. Simply use the plain class names from the universal stylesheet.

Alternatively, I'm considering implementing this in my root component:

<style module="$commonStyles" src="./common.css"></style>

This way, I can access this.$commonStyles throughout the application.

I will keep you updated on my progress.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using regex in Javascript to find and match an ID within a string

JavaScript: var data='<div id="hai">this is div</div>'; I am looking to retrieve only the ID "hai" using a regular expression in JavaScript. The expected output should be, var id = regularexpression(data); The variable id should n ...

What is the best way to find a specific class within a CSS file using Visual Studio Code?

Currently, I am working with a bootstrap template on my webpage. I am interested in personalizing specific sections of the design, but unfortunately, I am having trouble identifying the relevant CSS rules within the extensive .css file. My research online ...

An error occurred: [object Object] does not contain the function 'bootstrapDatepicker'

After spending countless hours searching for a solution, I continue to encounter the dreaded 'Uncaught TypeError' without any successful resolutions. The issue seems to stem from a clash between tribe-events-ajax-calendar.js and foundation.min.j ...

Basic image slider featuring adjustable heights

I'm currently working on a basic content slider, but I've encountered an issue where the container doesn't expand as new content slides in. It seems to be related to the absolute positioning of the content items. If I don't use absolute ...

Expanding cards with Material-UI and React seems to be a challenge when using an expander

I've recently integrated a Card component into my project, sourced from material-ui's official website. However, I'm encountering an issue where the CardHeader does not expand upon clicking. This is the structure of my Component : import ...

Struggling with jquery's addClass method when trying to apply a class to

Below is a sample tr: <tr> <td><input type="text" class="ingredient required" name="ingredient"></td> <td><input type="number" class="amount required" name="amount" ></td> <td><input type="number" c ...

Retrieve the initial element of a JavaScript array

Is there a way to retrieve the first element of a JavaScript array without using array[0]? Interestingly, the array being passed into the method seems to have its first (and only) element stored at index 5 instead of 0. Update - I've attempted to cr ...

Would it be possible to use a script to convert the y-axis values of the chart into Arabic numerals?

Currently, I am working on creating line and pie charts, but I need the Y-axis to display Arabic language. $('button').on('click', function(e) { $('#pie-chart3').empty(); var type = $(this).d ...

Unable to retrieve property from Vuex store object

Firstly, I start up my store using the JSON file named config.json: { "branding": { "button": { "secondary": { "background_color": "#EC6B24", ... }, ... } } } ...

Insert the variable into the specified div ID

I am looking to implement an incremental div id system to ensure all my ids are unique. This way, I can make use of jQuery effects to customize them individually. Let me know if you need further clarification on my query. div id ="name_$id" Perhaps I sh ...

Encountering a problem with loading the stylesheet

I keep encountering a 404 error when trying to load the CSS in my web application. The HTML content looks like this: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PSL Inter App</title> <meta nam ...

Using Vue.js - Learn the process of importing a filter into a Vue component from an external file!

I am currently working on a vue-cli project that consists of a vue.js component called MyComponent.vue. My goal is to import a filter, named MyFilter.vue, from a separate file into the component. Surprisingly, I have successfully managed to export the fi ...

Is there a way to detect when a Bootstrap Alert is displayed?

I am using a flask app that flashes messages to an HTML file. When these flashed messages are caught, they are displayed as a bootstrap alert. Here is an example: {% with messages = get_flashed_messages() %} {% if messages %} {% for message in mes ...

Search for an element deep within a tree structure, and once found, retrieve the object along with the specific path leading to

I created a recursive function to search for a specific object and its path within a tree structure. However, when I changed the target ID (from 7 to 10) in the function, I encountered an error: "message": "Uncaught TypeError: Cannot read ...

Click-o-Meter: Tracking Button Presses

I’m looking to develop a button click counter that increases every time it is downloaded. I want to implement this functionality without using a database. Here's the code snippet: <?php $counterFile = 'path/to/counter.txt' ; ...

What is the best way to send a POST request with parameters to a PHP backend using AJAX?

This is an updated version of a previous question that was identified as a duplicate (How can I send an AJAX POST request to PHP with parameters?) I am currently sending an AJAX request to a PHP backend. Here are the JavaScript functions used to make and ...

Using CSS to place the h1 text on top of an image

I need some advice on how to place a heading above an image. I have tried using negative margin and adjusting z-index, but the image still covers the heading. If you have a better solution, please let me know! Thank you in advance for your help! PS: Anot ...

When setting the margin to auto, it perfectly centers elements on servers and IE, but on Chrome off the server, it appears to be aligned to the

There seems to be an issue with the display of my page in Chrome - it starts at the middle and ends on the right side. However, when I view it on my server or try it on Internet Explorer 11, it appears centered. Why is that? This problem only arose after ...

Having trouble with React testing-library: Why is the file upload empty when testing a PDF file?

While testing file upload with react-testing-library, I encountered an issue where the log indicated that the file was empty (even though it worked in the browser). After researching various docs and bugs, I discovered that since tests run on Node.js, the ...

What is causing my HTML to not recognize my Angular JS code?

Trying to dive into Angular JS, I wrote a small piece of code but for some reason, the HTML is not recognizing Angular JS. This is my index.html file: <!DOCTYPE HTML> <html ng-app="store"> <head> <link rel="stylesheet" type=" ...