Learn how to import a stylus file globally in Vite build tool

Recently, I decided to use vite for building a new vue application. However, I encountered some difficulties when trying to globally import a stylus file in my vite.config.js.

I followed the code examples provided in the vite documentation:

export default defineConfig({
  css: {
    preprocessorOptions: {
      styl: {
        additionalData: `@import "./src/assets/styles/common.styl"`
      }
    }
  }
})

Unfortunately, this approach did not work as expected.

Luckily, I found a workaround on a GitHub repository that solved my issue:

export default defineConfig({
  css: {
    preprocessorOptions: {
      stylus: {
        globals: {
          '$highlight-color': 'red'
        }
      }
    }
  }
})

Although this solution is effective, I am still struggling to find a way to import a styl file globally. I attempted to add 'additionalData' to the working configuration but encountered multiple errors such as 'failed to locate file'. It seems like the system is trying to import the file into every .vue file but is having trouble locating it.

css: {
    preprocessorOptions: {
      stylus: {
        additionalData: `@import "./src/assets/styles/common.styl";`,
        globals: {
          '$color-g1': '#F3F4FC',
          '$color-white': '#FFFFFF',
        }
      }
    }
  }

Does anyone have any insights or ideas on how to tackle this issue?

Answer №1

I've found success with this code snippet:

  css: {
    preprocessorOptions: {
      stylus: {
        imports: [path.resolve(__dirname, 'src/assets/styles/common.styl')],
      },
    },
  },

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

Encoding and decoding a two-way stream in NodeJS

I am looking to encapsulate a socket within another object that has the following features: - transforms output, such as converting strings into Base64 format - transforms input, such as converting Base64 back into strings (Please note: while my specif ...

Creating a hover effect for a specific card while maintaining the rest of the cards unaffected

When hovering over a card, all icons are displayed instead of just the icons for that specific card. I want to show only the icons for the card being hovered on (example output: image). The cards are generated automatically using v-for and fetching data fr ...

Having trouble running classes using Maven test with the Testng.xml file in the terminal, however, it runs smoothly in Eclipse

While I have been successful in running my solution through the testng suit in the Eclipse console, I am facing difficulties executing the testng.xml file via Maven integrated with Sauce Labs in the terminal. Output received on the terminal: ------------ ...

Steps for sending data to a modal window

Consider this scenario: I have a function that retrieves some ids, and I utilize the following code to delete the corresponding posts: onClick={() => { Delete(idvalue[i]) }} Nevertheless, I am in need of a modal confirmation before proceeding with the ...

Tips for escaping an infinite loop within the componentDidUpdate function in reactjs

Currently, I am working on creating dashboards using reactjs. I have successfully implemented 4 tabs or buttons for charts, but I am facing an issue when clicking on different dashboards that have the same chart in the same panel. The chart is not updating ...

What is the best way to incorporate an "if" statement into my code?

I'm in the process of setting up a section on my website where users can get live price estimates based on a value they input. While I have most of the formula in place, there's one final element that I need to figure out: introducing a minimum f ...

Transfer an object for reusability in a different JavaScript file without utilizing the default operator

I have a scenario where I have two files organized in a tree structure that defines an object. The first file is called common.js. export default { table: { actions: { save: 'Save', delete: 'Delete', update: ...

Storing formatted user input in an array with VueJS: A step-by-step guide

Looking for assistance that relates to the following question Vue.js: Input formatting using computed property is not applying when typing quick I am facing a challenge in extracting formatted values from text inputs and storing them in an array. I intend ...

The issue of deleting the incorrect document ID in React Firebase

I'm currently facing an issue while trying to implement a delete operation on a Firebase database using Reactjs. The problem lies in my function that seems to be fetching the wrong id from Firebase. There's a button triggering the handleOpen fun ...

How can I modify this section of the code in Google Script to retrieve all columns?

My question is, how can I modify this code to fetch all columns from the array instead of just [0,1,2,3] Here is the line of code in question: const new_table = [0,1,2,3].map(x => make_row_from_col(this_table, x)).join('\n'); Using obje ...

Using Vuex: Implementing methods to modify component data or invoke component functions directly from the Store

If I were to operate a store: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { foo: bar }, mutations: { updateComponent (state) { // calculation and reasoning ...

Adjust input width based on content in VueJS

Is it possible to achieve the following using (Pug & CoffeeScript): input(placeholder="0", v-model.number="order[index]" v-on:change="adjustInput") ... adjustInput: -> event.target.style.width = event.target.value.length + 'ch' Even ...

The masonry plugin overlays images on top of each other

I have gone through similar questions but haven't found anything that applies to my specific case. Following an ajax call, I am adding li elements to a ul $.ajax({ url: 'do_load_gallery.php?load=all' }).done(function(result) { ...

JavaScript seems to have difficulty correctly parsing objects from JSON

Having trouble populating a Repeat Box with objects from a JSON document. Despite my efforts, it keeps duplicating the first item instead of adding each one individually. Any ideas on what might be causing this issue? Below is the code I am currently usin ...

What is the best way to send checkbox values to ActionResult in MVC5?

I am working on an MVC5 application and within my view, I have the following code snippet: <div class="form-group"> @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "control-label col-md-3" }) <div c ...

Checking dates in a JavaScript form

var myForm = document.getElementById("form"); document.bgColor="#FFFFCC"; //page styling myForm.style.color="blue"; myForm.style.fontSize="20px"; myForm.style.fontWeight="400"; myForm.style.fontFamily="arial"; function validateForm() { var firstname = d ...

Issue with Click event not working on dynamically added button in Angular 8

My goal is to dynamically add and remove product images when a user clicks the add or delete button on the screen. However, I am encountering an issue where the function is not being called when dynamically injecting HTML and binding the click event. Below ...

A guide on designing a personalized search bar for MUI-Datatables with a sleek outlined style

Check out the default UI from MUI-Datatables v4.3.0 here: https://i.stack.imgur.com/rbHgD.png I want to create a style similar to this: https://i.stack.imgur.com/AUHqC.png Just so you know, I am using the following packages: "@mui/material": &q ...

initiating a communication to a specific path with provided parameters

I am attempting to submit a request to the Laravel named route users.update with the parameter user: const saveUser = () => { router.put(route("users.update", user.value.id), user.value); submitted.value = true; createUserDialog.va ...

Repeating the process of running a function multiple times

export default function MyQuestions() { const router = useRouter(); const [auth, setAuth] = useState(false); const checkAuth = async () => { const loggedInUsername = await getUsername(); if (router.query.username === loggedInUsername) re ...