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.

Sass Variables not available in pages / layouts / components

See original GitHub issue

Environment

Nuxt project info: 22:39:51


  • Operating System: Linux
  • Node Version: v16.13.0
  • Nuxt Version: 3.0.0-27375427.d07d572
  • Package Manager: npm@8.3.1
  • Bundler: Vite
  • User Config: components, meta, css
  • Runtime Modules: -
  • Build Modules: -

Reproduction

I am trying to load sass from assets/sass/global.sass. Add the file and fill it with:

@forward "colors"
@use "colors" as c

body
  background-color: c.$secondary

Add a partical sass file _colors.sass and fill it with:

$primary: #264653
$secondary: #e76f51
$black: #171717
$white: #e7e7e7

Add the sass file into the nuxt.config.ts config.

import { defineNuxtConfig } from 'nuxt3'

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
    components: true,
    meta: {
        script: [
            { src: "https://unpkg.com/phosphor-icons" }
        ],
    },
    css: [
        "@/assets/sass/global.sass",
    ]
})

See that the variable is valid and is applied as the background-color for the body. Now try to access any of the color variables inside a component, page or layout. Both with namespacing (@use) and without (@import) didn`t work for me.

Describe the bug

The Sass variables should be loaded into every vue file to be globally available. They aren`t as far as I can see.

Additional context

The Nuxt 3 docs are yet sparsely filled so there might be something missing I could not read up, that is necessary to make the variables load.

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
danielroecommented, Apr 11, 2022

We may implement via https://github.com/nuxt-community/style-resources-module or directly in Nuxt.

There is also the following workaround:

export default defineNuxtConfig({
  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/scss/global.scss";',
        },
      },
    },
  },
})
2reactions
jdhillencommented, Jun 15, 2022

Using 3.0.0-rc.4 and this work around doesn’t seem to work anymore. Still getting ‘Undefined variable’ in components that use global scss variables. Any suggestions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't access global SASS variables from my component
My guess is that Webpack isn't compiling all the SCSS declarations together and therefore can't find the variable.
Read more >
Structuring your Sass Projects - ITNEXT
Let's take a look at how we can structure our Sass projects. As projects grow and expand, the need to modularize our directory...
Read more >
Sass Basics
Think of variables as a way to store information that you want to reuse throughout your stylesheet. You can store things like colors,...
Read more >
SASS variables - Vuetify
A list of available variables is located within each component's API section and in the Variable API of this page.
Read more >
2 Smartest Ways to Structure Sass - Webdesigner Depot
Simple Structure · _core.sass — contains variables, resets, mixins, and other similar styles · _layout.sass — there are the styles for the header, ......
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