westwick's avatar

globally include a settings.scss with vue-loader?

I find myself repeating this same pattern in every .vue file, in order to use variables, etc.:

    <style lang="scss">
      @import "../styles/settings.scss";
      
      .someclass { color: $some-variable; }
    </style>

or if it's nested in a folder I have to remember to be careful with the path:

    <style lang="scss">
      @import "../../styles/settings.scss";
    
    </style>

Is there a way to globally import that settings.scss file in every .vue file I create? I looked in the docs and didn't see it, but maybe I missed it, or maybe this is something I need to leverage webpack to do?

0 likes
7 replies
alenabdula's avatar

@westwick, @gsa

// webpack.mix.js

mix.webpackConfig({
    resolve: {
        alias: {
            'GlobalSass': path.resolve('resources/assets/sass/_bootstrap.scss')
        }
    },
});
<!-- Single Vue component file -->
<template>
  <div class="component"></div>
</template>

<script>
  export default {}
</script>

<style lang="scss">
  @import '~GlobalSass';
  .component {
    color: $red;
  }
</style>

You still have to write the code to import SCSS file, but at least you don't have to mess around with ./../../../ in your code.

Hope that helps. Best, Alen.

alenabdula's avatar

You can create a snippet, if your editor allows it, so you're not writing template structure each time.

alenabdula's avatar

Something like this, if using SublimeText

<snippet>
<content><![CDATA[
<template>
    <div>
        ${1:<!-- template -->}
    </div>
</template>
<script>
    export default {
        ${2://}
    }
</script>
<style lang="scss">
    @import '~GlobalSass';
    ${3://}
</style>
]]></content>
<tabTrigger>vue-template</tabTrigger>
</snippet>
westwick's avatar

You can view this thread on github to see how to load styles globally in each component without having to import each time: https://github.com/vuejs/vue-loader/issues/328

npm install sass-resources-loader --save-dev

then edit your webpack (build/util.js)

scss: generateLoaders('sass').concat(
  {
    loader: 'sass-resources-loader',
    options: {
      resources: path.resolve(__dirname, '../src/style/_variables.scss')
    }
  }
),
1 like
rowanxmas's avatar

I have been unable to get the new globalVueStyles working. When I did set globalVueStyles and extractVueStyles: true, my primary scss file loaded via the mix .sass loader did not compile correctly.

Please or to participate in this conversation.