Make environment variables available as Sass variables in Vue.js.
Created
January 16, 2019 20:25
-
-
Save steveholgado/e1c5b2a195cc0cd358e7085a626c54cf to your computer and use it in GitHub Desktop.
Make environment variables available as Sass variables in Vue.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONDITION="true" | |
COLOR="red" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
css: { | |
loaderOptions: { | |
sass: { | |
data: ` | |
$CONDITION: ${process.env.CONDITION || 'false'}; | |
$COLOR: ${process.env.COLOR}; | |
` | |
} | |
} | |
} | |
} |
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
For Nuxt js:
In nuxt.config.js