My SCSS setup within a Vue CLI 3 project

Lynne Finnigan on September 08, 2018

I never realised that I had lost some of my passion for development until Vue.js came along. I'm still learning, but it has made coding fun again... [Read Full]
markdown guide
 

Nice article :), however I was wondering if it could generate again and again all the styles called in global.scss each time a component has a <style scoped>. For exemple, the normalize.scss would be appended multiple time in the final file.

At least it seems to be the case when I replicate the exemple.

Here the compiled result:

/*! normalize.css v7.0.0 | MIT License html{line-height:1.15; [...]
/*! normalize.css v7.0.0 | MIT License html[data-v-abd815be]{line-height:1.15; [...]
/*! normalize.css v7.0.0 | MIT License html[data-v-jh6khva]{line-height:1.15; [...]

Or maybe this architecture should only be used with variables, mixins, functions ?

 

Hey Allan, great question!

My thinking around this was that inside the global.scss, it would only really import variables and mixins / placeholders - basically anything that the scoped components might need to reference or need access to.

You could probably also just list those files in the config, instead of creating a global.scss file - I just preferred it that way.

You bring up a good point, I think a good way of tackling this would be to have just variables and things that your components need to reference in the vue config file, and then any other 'global' styles such as basic html / body / normalize files could be imported into your layout / wrapper vue file.

What do you think?

 

Yes, I ended up with this solution so far :).

I created an import.scss for all my variables and mixins / placeholders used with the style-resources-loader plugin, and imported the global css part with a simple import in my main.js file.

I think it give the best of both worlds if you want to write style scoped and also use some general (unscoped) style like a reset, normalize, general UI, etc.

Great! Yeah that is always the case for me, I always have sort of base styles that apply globally, but I want to scope all my components. So doing it this way works perfectly :)

Hey Allan and Lynne, Sorry, kinda lost here :) Can you explain what you did to solve this? Especially with the import.scss file

Although, I think maybe importing the global.scss file in App.vue might also be a good solution?

Hi Emmanuel, the import.scss file should just have things like variables and mixins that the other files will need access to. If you don't use the plugin to serve the variables etc, you can't access them and use them within other components.

So basically, from your example. Everything imported in the global.scss file is available to use in all the components?

Awesome post Lynne. This helped me clean up way too many double imports.

Emmanuel, I was a little confused too.

For the stylesheet that you call into vue.config.js only import mixins and variables (no style selectors/rules).

Call other global.scss (declared rules and stuff) elsewhere, like main.js or a parent app like App.vue.

Any style rules imported to a stylesheet through vue.config.js will import again for each component that uses it.

 

Nice article :)!

I'm new to vue 3 and vue UI. I personally think that Vue 3 should add the required SCSS Loader plugin when you select it and create a standard style folder and some default files. I really appreciate your article! It filled in some gaps. I'm not a web developer, until the last year and then only really looking at all the new frameworks like; AngularJS, Angular, React, Preact, etc. I think Vue 3 has a lot going for it over vuejs. However, Seems like the Documentation for vue 3 needs to improve some. What a lot of articles lack are fine details that beginner would need. A lot of students and others moving over to web programming need every detail possible. I think it would be great if this article has some of your sample files contents to show what a basic scss file looks like and the structure inside your files global.scss. Thanks again! Great Job!!

 

Thanks! I never really thought about the official docs before but I guess it is lacking in terms of how to structure/set up styles. I might look into contributing to the docs :) I'm glad it helped you!

 

Hey Lynne, really nice job and quick response! I was tasked with creating a vue app for mobile like two weeks ago. So learning at a high rate of speed. :)

I'm really lacking in knowledge of css and scss. I have added bootstrap to my vue app. And now thanks to your article, I know i need to add my bootstrap scss files to the global.scss file as imports. However, it's blank right now.

One thing I'm trying to wrap my head around is css is cascading the css rules down but we are thing to sandbox them into each component (vue). And in the end we will combine them all into one file for deployment.

Does my assumption seem right about importing the scss file into the global.scss file? The bootstrap scss files are located under the node_modules folder in subfolders.

The way I use it, and think about it, is that anything in the global scss file applies across the whole site and this is generally things like variables, grid, layout, and basic styles. It should be styles that aren't very specific, sort of like your baseline.

For each component, when you write a scss file for it and the styles are nested inside the component class name, it shouldn't affect anything else other than that one component. This is what makes it modular.

In your component scss file, you might want to overwrite something that has been set in the global stylesheet, and that's completely fine. As long as it is nested inside your component class name then it should only affect that one component.

Does that make sense?

 

Really good article, I followed a similar pattern on a React project recently, the only difference is I used SCSS nesting to handle the encapsulation. I've been intrigued by the import style so thanks for the reminder/motivation to have a look.

 
 

wowww, amazing, I was precisely looking for this, to use variables and mixins inside my scoped style, I'm testing using atomic design with VueJS, and this import of variables is a must have part of the solution, thank you very much \o/

 
 

Dammit, this is what I have been searching for. Thank you so much for writing this.

Question: Can you give a brief description of each of the folders in the style directory?
Also, you mentioned that the example.scss file would have access to other files like typography, etc but you didn't show how? I want to assume it happens by default?

 

Thanks :)

The folders in the style directory are just the way I normally organise things and they are fairly self-explanatory from the names. The Views folder contains partials for styles relating to views. Basics contains things like basic rich text styles, links, buttons, etc. Setup is things like variables mixins, and functions. And components would be the styles for each component.

The point of the article is that you need to use the plugin in order to make things like the variables available in other scss files. So if you had a global.scss file setup in the config using the plugin, inside global.scss you would import any scss partials you need to be available across the whole site/app.

Then in the example component I created, example.scss would just have your styles for that component, but the point is that you can use variables within it because they are served globally.

 

Okay, what about access to other styles like the button or typography. Sorry, but I'm a little bit confused. since the way you are explaining it, you are only making reference to the variable styles.

 

"I never realised that I had lost some of my passion for development until Vue.js came along. I'm still learning, but it has made coding fun again for me."
Same here I think, I'm gonna read this properly tomorrow - as I wanted to do this also. Are you me?

 

Haha thanks! Well, I’ve never seen us both in the same room, so maybe I am you 😄

 

Like the article, but quick question. I am trying to create a Vue Component Library, where all of the components have a default scss variable in them, for example:

$color: #000 !default

The intention is that the component will have default styling that could easily be configured/overridden when I later install my component library but override the styles in a configurable variables scss file. I can't seem to figure out on how to have the vue library's config file to accept that scss file.

Any insights?

 

Hi Mark, do you have a code example of the config file and where you're trying to add the scss?

I'd imagine that you could probably override based on the order you're importing the scss files into the component, but I'm not sure I understand what you're looking to do.

 

Why import example.scss again in components when using global import already. I like to import all my general styles in vue.conf.js, then it is available inside every component without importing anything.

 

The idea is that your global styles are only variables and things you need access to globally. When you import example.scss it has the attribute ‘scoped’, which means it’ll only render if that component is rendered on the page. Saves you loading a larger css file with css that isn’t being used. Hope this helps!

 

I totally get how one could lose their passion for develop. I've been at it for decades and sometimes it gets old. You've inspired me give Vue a shot and I'm excited about getting started this weekend. Thanks!

BTW, I love that you wrote this article in Markdown.

 

Thanks Karl 😊

That’s great! Glad you’re going to give Vue a try, I’ve really enjoyed working with it so far. Let us know how you find it!

 

Just wanted to say a big thank-you! I was struggling a lot with this problem recently, and this article showed up on Google search. And it was the simplest, best explanation for the setup that I came across. 🤗

 

Thanks, I'm really glad this helped you! 😊

 

nice article, but i have a question.
I'm new with Vue.js. When I leave styles blank in the app.vue, and I only want to use my global.scss, my global.scss is not being compiled. Is this normal behavior?

 

Is there anywhere I could see your code? it's hard to tell what the issue could be without being able to see it

 

unfortunately thats not possible.
The problem is when my style tags in my app.vue are emtpy:

, the scss files are not compiling...

When I put anything between them, for instance:

body{line-height: 20px;}

my SCSS files are compiling...

 

Great article, and I feel like you should get bonus points for the META cover_image.

 
 

Nice read, I like how there are so many people reigniting there passion for programming with Vue. Ultimately it's this passion that will help Vue end up on top. ♥️ Vue

 

Thanks! I completely agree, many people have said this to me as well. Very nice to work with, great documentation and community.

 

Hi - a newbie question here - if anyone can please fill me in:

Following your guide I have successfully added vue-cli-plugin-scss-base (0.1.10) to my vue project.
And when using the "vue ui" (Vue Project Manager in your browser) approach - the scss folder with all the scss files get generated (as well as changes to vue.config.js and App.vue).

I suppose this happens just about the time the terminal which I engage "vue ui" from reports:
"🚀 Invoking generator for vue-cli-plugin-scss-base...
✔ Successfully invoked generator for plugin: vue-cli-plugin-scss-base
The following files have been updated / added:" (and the list of files: src/scss/animations/_animations.scss etc.......)

Now the question is: How would I go about doing this from the terminal??

I mean: "npm i vue-cli-plugin-scss-base" adds the statements to package.json and package.lock.json (in dependencies)

And "npm i vue-cli-plugin-scss-base --save-dev" adds to devDependencies (same as vue ui - from browser) when I add the plugin there..

but after that npm install / npm build / (rebuilding entire project in IntelliJ) or whatever I tried - it doesn't seem to have any effect. The scss file creation and file changes (mentioned above) never appear/happens.

Surely there is some essential command (knowledge) I am completely missing.

NEVER mind - the solution was as simple as: "vue add vue-cli-plugin-scss-base"

best regards,
H

 

Hi, I was just about to look into this, did you get it all sorted then?

 

Life saver,
actually this article it is better than every tutorial i watched in the same topic,
I have always been confused about setting up a style architecture for my vue projects.
good job

 

Thank you so much! I'm glad it has helped :)

 
 

I have in a project a huge components base. Some have their style tag in scss, and others in sass.

When I put scss in 'preProcessor', it works for scss styles but not sass ones. If I put sass, the same, it does not works foe scss components.

Is there a way to import my variables file in both componetns ?

 

Off the top of my head, it could be the import syntax. I think in scss it looks like this:

<style scoped lang="scss">
    @import 'styles.scss'
</style>

and in sass like this (without the quotes):

<style scoped lang="sass">
    @import styles.scss
</style>
 

Really good article. I've been struggling with this for a couple of days and finally made it work, thanks!

 

No problem, I'm glad it helped 😊 I was in the same situation, and it took a while to figure out!

 
 
 

Hey Lynne!

This article is so good. I love how you've organized these files. It's super clean and easy to see what styles are where. A novel approach to the problem. Thanks for writing it!

 
 

Well done ! Your article works perfectly even in France ! :) Next step for me, import a CSS+SASS grid micro-framework like KNACSS

Thanks Lynne !

 
 
code of conduct - report abuse