Skip to content

Instantly share code, notes, and snippets.

@steveholgado
Created January 16, 2019 20:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save steveholgado/e1c5b2a195cc0cd358e7085a626c54cf to your computer and use it in GitHub Desktop.
Save steveholgado/e1c5b2a195cc0cd358e7085a626c54cf to your computer and use it in GitHub Desktop.
Make environment variables available as Sass variables in Vue.js

Vue.js: Environment variables in Sass

Make environment variables available as Sass variables in Vue.js.

CONDITION="true"
COLOR="red"
<template>
<div class="example">
Example
</div>
</template>
<script>
export default {
name: 'Example'
}
</script>
<style lang="scss">
.example {
color: black;
@if $CONDITION == true {
color: $COLOR;
}
}
</style>
module.exports = {
css: {
loaderOptions: {
sass: {
data: `
$CONDITION: ${process.env.CONDITION || 'false'};
$COLOR: ${process.env.COLOR};
`
}
}
}
}
@joaoportela
Copy link

Thank you so much for this. Wanted some conditional css for development and couldn't find a way to do it.

@StaverDmitry
Copy link

That's perfect, thanks for posting it :)

@WvanDam
Copy link

WvanDam commented Sep 4, 2020

Thanks for this example. Note that with newer setups the config structure has changed slightly. To be precise: data has changed into prependData.

@vuchl
Copy link

vuchl commented Sep 10, 2020

Thanks man!

@lolik9l
Copy link

lolik9l commented Sep 24, 2020

For Nuxt js:

build: {
   loaders: {
     scss: {
       additionalData: `$MAIN_COLOR: ${process.env.NUXT_MAIN_COLOR};`,
     },
   },
 },

In nuxt.config.js

@badoubadou
Copy link

Hi man, did you made it work with stylus and nuxt ?
I tried :

build: {
    loaders: {
     stylus: {
       additionalData: `$myred: #000FFF`,
     },
   },

When I use it, it break my build. It add correctly the var, but just after it, I get an error :


1| $myred: #000FFF
   2| @import '~/assets/style/font.styl'
--------------^
   3| @import '~/assets/style/var.styl'
   4| @import '~/assets/style/transition.styl'
   5| @import '~/assets/style/layout.styl'

expected "indent", got "import"
But if I had the same var my self at the top the main stylus file like additionalData does, then it works fine.

Is it not the right way to do it ?

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment