question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global SCSS Imports

See original GitHub issue

Is your feature request related to a problem? Please describe.

I have a scss file with variables I would like to use throughout my app without importing it in every <style> block.

Describe the solution you’d like

A configuration option in vite.config.js which directly or indirectly would allow users to prepend sass as is possible in vue.config.js using prependData or additionalData depending on your sass-loader version.

Describe alternatives you’ve considered

I noticed that vuejs/vue-next#2126 was cited in reference to closing #520, a similar request, but I’m not sure how that’s applicable to vite given that it doesn’t use webpack and as far as I can tell doesn’t use sass-loader.

Additional context

Another separate but related problem is that @import only seems to be working with relative paths; e.g. with the default project setup@import ./src/whatever.scss works fine but @import /src/whatever.scss does not. Supposing that is in fact the case and something like additionalData where possible to use in vite then relative paths would break for components in nested directories. Implementing #650 to resolve aliases in scss files might work though.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
tandv592082commented, Jan 26, 2021

It’s working perfectly for me

export default {
  css: {
    preprocessorOptions: {
      scss: { 
         // example : additionalData: `@import "./src/design/styles/variables";`
         // dont need include file extend .scss
         additionalData: `@import "./src/path-to-scss-variables";` 
     },
    },
  },
};
8reactions
scriptcodedcommented, Oct 19, 2020

This is working fine for me. My config looks like this:

import { SharedConfig } from 'vite'

const config: SharedConfig = {
  cssPreprocessOptions: {
    scss: {
      additionalData: '$primary: red;'
    }
  }
}

export default config

The biggest issue right now is #650, as there is no good way of importing files. For example the following wouldn’t work: additionalData: "@import '../assets/scss/_base';"

I’m using SCSS btw, but the same solution should work for plain CSS or anything else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass: @import
Sass imports have the same syntax as CSS imports, except that they allow multiple imports to be separated by commas rather than requiring...
Read more >
How to import Sass/SCSS mixins global? - DEV Community ‍ ‍
I import the two .scss files in the App.js so I can access the classes/mixins/variables global. But only the classes are accessible.
Read more >
Sass @import and Partials - W3Schools
Sass Importing Files ... Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of...
Read more >
How do I import a global rule in a sass file without it getting ...
I got this by running sass-migrator module --migrate-deps frontend/styles/App. sass . To have this build properly we must use the sass npm ...
Read more >
Basic Features: Built-in CSS Support - Next.js
Next.js allows you to import CSS files from a JavaScript file. This is possible because Next.js extends the concept of import beyond JavaScript....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found