-
Notifications
You must be signed in to change notification settings - Fork 915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load a global settings.scss file in every vue component? #328
Comments
I have the same question,but also have no answer. |
I basically have the same question as well. |
@westwick have you tried - https://github.com/shakacode/sass-resources-loader |
I do not know the answer to the main question (global scss load). But regarding the additional pain point (adjust relative path), maybe you can mitigate it by configuring a Webpack alias. You would use {
resolve: {
alias: {
styles: 'path/to/your/styles'
}
}
} Docs: |
Neither of these solutions solves the fact that styles.scss will be repeated on your page for however many files you include it in. That just seems not thought out and very wasteful. |
@ericcirone We have to differenciate: Sharing Variables & SettingsThe OP asked about importing a Such a file usually does not contain any CSS, but only SCSS $variables, mixins etc. (as stated by the OP himself) - so there would be no repeated CSS in your final, compiled files. Granted, it's tedious to add this Sharing stylesYou wrote about importing For this scenario, you simply do an import of the shared style file in your @jbruni The alias is a good solution to relative paths, yep! 👍 But you should use an absolute path: {
resolve: {
alias: {
styles: path.resolve(__dirname, '../src/path/to/your/styles') // relative to the location of the webpack config file!
}
}
} |
@LinusBorg i got ya! I'm new to Sass and Vue. My problem was that i'm building a project with Foundation and tried importing in the Foundation main app.scss in all my |
@ericcirone under the current setup you have to go into the foundation scss folder and find the partials from the src folder that contain the variables and the mixins, then import those in every file. You likely do not need to import the entire main.scss file in every partial if that's what you are doing. |
Clsoing as this issue seems to be solved. |
I'd ask to leave this open as the real issue is being able to use scss variables, mixins in component definitions. Although css automatically cascades down, if you wanted to use a globally defined sass variable inside your component, you will not be able to use the $variable synthaxx.
this will fail and a solution to this would turn my world around. |
I also think this should remain open. I'd love to be able to use global scss variables throughout components without importing every time. |
Well, this is something that vue-loader is not suited to solve - it would have to be solved in sass-loader etc., because each pre processor might have a different syntax/ way of adding such a file. Stylus-loader offers such a functionality, by the way. |
@uptownhr well you can simply import your variables file in each component. It's a one-line copy&paste for each component, but it works. |
@LinusBorg wrote
Can you point to an example of this with Stylus? +1 to everyone asking for this issue to remain open, even if means waiting until a pull request eventually finds its way to sass-loader. This functionality should be a Vue product requirement, frankly. |
This is not very well documented, I only read about it in a docs example for using a plugin: https://github.com/shama/stylus-loader#using-nib-with-stylus The
We will not keep issues open for 3rd-party libs that we have no influence on, and likely won't require a change in vue-loader if they and in the 3rd-party lib (such changes would likely be implemented in loader configs, like the above You will be better of asking for this in the respective loader's repos. |
I too have the same problem. I really want to use a full Vue.JS Webpack stack on my next project. However.. not being able to import SASS variables is almost a deal breaker here. I guess for now I will compile global styles separately and use minimal styling in the components themselves. There has to be a solution to importing variables and mixins no? Or making them globally available on compile? |
I'm not sure if you missed it, so repeating myself again: you can of course simply @import your variables. This threads was about doing that automatically, which is not possible and frankly, (repeating myself again, see previous reply) cannot be the job of vue-loader to provide that for all the available pre-processors. |
I think you guys should seriously reconsider your position on this. At this point, SASS is the preprocessor of choice for at least 50% of devs (or a lot more, depending on which poll you're looking at). I really like keeping my components' css local to that component but it's a PITA when developing to have to import my settings.scss every time I start a new component, and having to take care to make the path right, and then it also makes the code slightly more verbose. Then if you ever move your component's path you have to update that import statement, etc. etc. Without this ability, I've resorted to the more traditional method of having all my scss files totally separate from the components, which defeats a good chunk of the the purpose of using vue-loader / .vue components IMO. Although to be totally fair I have not looked into the other options of getting this to work (such as the stylus one you linked). Either way, thanks for your work on this project. |
You easily reduce the pain about the paths with a webpack alias, reducing the statement to e.g. Considering the number of import statements we have in the JavaScript of most components, that should be manageable. Adding this automatically with vue-loader would be a hack. For starters, `lang="sass" can mean SCSS or SASS syntax, depending on the sass-loader config. So which do we choose when we prep end the @import string to the content of the script tag? We would have to interpret the webpqck settings somehow, etc. |
That's a fair point and I think a webpack alias can be a good "workaround". It's definitely more explicit to always use an import statement, which you could argue is better. |
Great :) |
quick question, when you |
Concerning filesize, this has no influence if the imported file only contains SCSS variables and no actual CSS markup. Concerning memory / compile speed, this should be so small as to be neglegable - I haven't seen any issues with it in dev. There are other parts of the build process that should be optimized if you experience longer (Re)build times in dev. (vendor chunks, DLL splitting ..) |
Is there anything more needed to get Webpack aliases working in I set a File structure:
// webpack.config.js
{
resolve: {
alias: {
'styles': path.resolve(__dirname, './src/style/')
}
}
} <!-- my-component.vue -->
<style lang="sass">
@import "styles/_vars.scss";
</style> I’ve tried all kinds of variations on the import directive with no success, and a direct alias to the variables file also produces an error. It does work using an absolute path (no Webpack alias), but I’d like to avoid the fragility that introduces: @import "./../style/vars"; Any help appreciated! |
@import "~styles/_vars.scss"; |
Looks like @kinoli and @Zver64 have the right idea. There's even an article here that covers how to do it for both The only gotchas I ran into were the age-old syntax slip-ups. 🤦 (Don't forget that semicolon following your @kayoderock I have a link to that in this comment already. |
I feel the best solution I have seen is this: |
@kayoderock I tried that solution it didn't work for me I am getting this error
|
Are you using Vue CLI 3 |
Yes @kayoderock, I am using Vue CLI 3 |
@mittalyashu Please, Do you have your vue.config.js file? and do you have the sass loader module? |
Yes. I have both Here's the module.exports = {
css: {
loaderOptions: {
sass: {
data: `
@import "@/assets/css/helpers/_helpers.sass";
`
}
}
}
} |
@mittalyashu Can I see the way you did your style in the index.vue file, Are you writing SCSS or SASS ? |
Here is my solution to this issue. Has worked perfectly for me ever since. Uses the newest MiniCssExtractPlugin compatible with Webpack 4.
Webpack Common// Webpack 4
...
module: {
rules: [
// CSS & SCSS Processing
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
// Load common SCSS [ Vars & Mixins ]
resources: './src/assets/styles/shared.scss',
},
}
],
},
... App.vue<template>
<main id="app">
<!-- Skip Navigatio Accessbility -->
<button href="#mainContent"
title="Skip to main content"
aria-label="Skip to main content"
v-on:click.stop.prevent="skipNav"
class="mdev-skipnav" tabindex="0">
Skip To Main Content
</button>
<main-navigation></main-navigation>
<transition name="fade">
<router-view></router-view>
</transition>
</main>
</template>
<script>
//Local Component registration
import MainNavigation from './components/shared/navigation.vue';
export default{
components: {
'main-navigation' : MainNavigation
},
methods: {
skipNav() {
var anchor = $("#mainContent").offset().top;
$('html,body').scrollTop(anchor);
}
}
};
</script>
<style lang="scss">
// Loads global stylesheets (Excludes mixins and Vars loaded via sass-resources-loader)
@import './assets/styles/global-main.scss';
// Can add styles here referencing mixins or variables and it works!
</style> |
@kayoderock A component file, looks something like this with SASS. <template>
...
</template>
<script>
...
</script>
<style lang="sass">
// Styles over here...
</style> |
@mittalyashu I suspect you are writing SCSS in your style , while you stated the lang="sass", change it to lang="scss". Let me know if this is the case else, I check out your error log again. |
well i'm new to vue but i fix it very simple like the example below ;) file structure
App.vue
./scss/global.scss
./scss/variables.scss
./scss/_sassfile.scss
for each one component or view you can override it ./views/About.vue
This way calling the scss var present in scss/variables.scss in the style of the component creating a vue.config.js in root project with this code
Now you can use the sass vars in the style tag
i will leave here a copy of my package.json { |
Don't want people to be able to use your components ? Just add sass in your .vue files... |
@golgote you are right. What I put in those files are general styles like application primary color etc |
That also combined some solutions in one post: |
If you create your project with you might solve this issues by change generateLoaders function on /build/utils.js like this /sass/vue-globals.scss
I'm quite new for vueJS or webpack stuffs, I'm not sure it's right solution. I hope this helps someone :) |
@ByungWookYe Alright, but did you try this => https://vueschool.io/articles/vuejs-tutorials/globally-load-sass-into-your-vue-js-applications |
@kayoderock I guess my solution seems similar with your link, but More specifically work for
I just wrote that comment for less skilled persons like me :) |
@ByungWookYe Alright, I get your point, I am just not sure if it's solving the problem here - having to load the sass at every needed point. Do you get me ? |
@kayoderock yeah, It add @import "@/sass/vue-globals.scss"; for every sass codes or files ,just same as your linked solution, and thus it solved the problem. |
Thats fine, @ByungWookYe it works too. Thanks. |
I am trying to create a Vue Component Library, where all of the components have a default scss variable in them, for example:
The intention is that the components will have default styling that could easily be configured/overridden when I later install my component library as an npm module in another project but be able to 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 in order to apply the "overrides". Or, to update the end projects vue config file to merge with the vue component's config file. I apologize if this should be a separate issue. I ultimately chose not to since it is mostly related to this issue. |
Thanks @nellysattari |
Nuxt Style Resources module helps me. |
* update docs for global scss loading per this comment thread: vuejs/vue-loader#328 (comment) * be more generic about webpack rule * update text
I also implemented the vueschool solution. Everything is working fine, but I moved from The thing is that I can use variables like |
For those using the option 'data' of sass-loader, note that since v9.0.0 the option to specify the @import is now named additionalData. |
I find myself repeating this same pattern in every component:
My settings.scss only contains variables. An additional pain point is that I have my components nested in several folders for better organization, so I always have to be careful to specify the path to my settings.scss properly. It'd be great if there was a way to globally include a settings.scss or similar file.
The text was updated successfully, but these errors were encountered: