text
stringlengths
0
445
<button class="submit">Submit</button>
<template>
<style lang="scss" scoped>
.submit {
background-color: #20ae96;
}
</style>
…becomes this:
<button class="submit" data-v-2929>Submit</button>
<style>
.submit[data-v-2929] {
background-color: #20ae96;
}
</style>
That dynamic data tag gets added to every child element in the component as well. So every element and every style for this component will have a data-v-2929 on them at runtime.
If you import a Sass file into your component that has actual styles in it, Vue (via webpack) will pull in those styles and “namespace” them with that dynamic data- attribute. The result is that is you include Bulma in your app 13 damn times with a bunch of data-v weirdness in front of it.
But this begs the question: if webpack renders the CSS in every single component, why would you ever want to use the vue.config.js approach? In a word: variables.
The variable sharing problem
You can’t define a Sass variable in one component and reference it from another. That would also be kind of hard to manage since you would be defining and using variables all over the place. Only I would write code like that.
You, on the other hand, would probably put all your variables in a variables.scss file. Each component would then reference that central store of variables. Importing a variables file into every single component is redundant. It’s also excessive. And unnecessary. And long-winded.
This is precisely the problem that Sarah’s article is solving: importing a Sass file into every component in your project.
It’s OK to import something like variables into every component because variables aren’t rendered. If you import 200 variables and only reference one of them, who cares? Those variables don’t exist in the rendered CSS anyway.
For example, this:
<style lang="scss" scoped>
$primary: #20ae96;
$secondary: #336699;
.submit {
background-color: $primary
}
</style>
…becomes this:
<style>
.submit[data-v-2929] {
background-color: #20ae96;
}
</style>
So, there are really two problems here:
Bulma needs to be global.
Bulma’s variables should be accessible from the components.
What we need is a clever combination of Sarah’s technique, along with a little proprietary knowledge about how Bulma is structured.
Using Bulma with the Vue
We’re going to accomplish this with the least amount of duplication by having three files in the src/styles directory:
variables.scss: This file will be where you pull in Bulma’s variables and override/define your own. Note that you have to include the following three files to get all of Bulma’s variables. And they have to be in this order…
// Your variables customizations go up here
// Include Bulma's variables
@import "bulma/sass/utilities/initial-variables.sass";
@import "bulma/sass/utilities/functions.sass";
@import "bulma/sass/utilities/derived-variables.sass";
bulma-custom.scss: This file is where you pull in the pieces of Bulma that you want. It should reference the variables.scss file.
@import "./variables.scss";
/* UTILTIES */
@import "bulma/sass/utilities/animations.sass";
@import "bulma/sass/utilities/controls.sass";
@import "bulma/sass/utilities/mixins.sass";
// etc...
site.scss: This pulls in the bulma-custom.scss file and is also where you define global styles that are used across the whole project.
@import url("https://use.fontawesome.com/releases/v5.6.3/css/all.css");
@import "./bulma-custom.scss";
html,
body {
height: 100%;
background-color: #f9fafc;
}
// etc...
Import the site.scss file into your main.js file. Or in my case, main.ts. Does it make me better than you that I use TypeScript? Yes. Yes it does.
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
// import styles
import "@/styles/site.scss";
This makes all of the Bulma pieces we are using available in every component. They are global, but only included once.
Per Sarah’s article, add the variables.scss file to the vue.config.js file.