Skip to content
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

How can I import global scss file in Vue? #1725

Open
Jancat opened this issue Jul 13, 2018 · 13 comments
Open

How can I import global scss file in Vue? #1725

Jancat opened this issue Jul 13, 2018 · 13 comments

Comments

@Jancat
Copy link

Jancat commented Jul 13, 2018

❔ Question

I'm building a Vue.js application and installed node-sass devDependency already.

I want to import global scss file(main.scss) in main.ts and all .vue component can use the global style directly. But it not works in the below code.

The main.ts file:

import Vue from 'vue'
import './styles/main.scss'   // import global app style file
import router from '@/router'
import App from '@/App.vue'

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')

App.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<style lang="scss">
// I had to import main scss file in every .vue component. Otherwise the `$vue-green` is undefined when bunddling.
@import './styles/main';

// the #app class hadn't apply to the root div element?
#app {
  font-family: '-apple-system', 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  background-color: $vue-green;
}
</style>

/styles/main.scss

@import 'variables';   // ./variables.scss defined the $vue-green

problem:

  1. The main.scss hadn't been injected to the .vue component.
  2. The #app style hadn't been applied to <div id="app"> root element.

🔦 Context

How to import scss into Vue application properly?

🌍 Your Environment

Software Version(s)
Parcel 1.9.4
Node 10.5.0
npm/Yarn Yarn
Operating System Mac OS
@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jul 13, 2018

That will not inject it, it’ll create a new css bundle that would be added to an index.html if thats the entrypoint

Sent with GitHawk

@Jancat
Copy link
Author

Jancat commented Jul 13, 2018

Is there a way to import global scss style like webpack sass-resources-loader?

And do you have any ideas for problem 2 above? Is it may be a bug? @DeMoorJasper

Thanks.

@DeMoorJasper
Copy link
Member

@Jancat If I understand correctly than the sass-ressources-loader is basically just sass-node but for webpack and we support every sass feature so I guess we support it...

Not sure why that isn't working probably a good idea to dive into the output folder and see for yourself why it isn't working, some debugging from your side would definitely help. I can't guess what's going wrong

@Jancat
Copy link
Author

Jancat commented Jul 14, 2018

@DeMoorJasper Sorry, the problem 2 is the reason that I forgot to add postcss-modules dependency.

For problem 1:

Since Parcel base on node-sass to support sass feature, why the App.vue can't use style that import from main.ts? Now I had to @import './styles/main'; in my every vue component that need to use main scss style.

@caiotarifa
Copy link

A few months passed. Is there a solution?

I bring an example of how it is done in the Webpack.

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        data: `
          @import "@/scss/_variables.scss";
          @import "@/scss/_mixins.scss";
        `
      }
    }
  }
};

@gregorybolkenstijn
Copy link

+1

@mjoct
Copy link

mjoct commented Feb 21, 2019

Place it inside your main.js

@Dagge1
Copy link

Dagge1 commented Mar 12, 2019

it still doesn't work. I want to include Bootstrap scss and it simply doesn't work, I have tried countless combinations. You guys should stop announcing that parcel is a zero-config thing, because it is not. I'm going back to webpack

@Neefay
Copy link

Neefay commented Apr 30, 2019

This one was a bit of a blow to my initial Parcel excitement, but I'm sure we'll see an implementation soon.

EDIT: After looking around for a bit, this solution has worked wonders for me.

Just add .sassrc.js to your project root folder and link it with your entry point. Parcel Magic® takes care of the rest.

.sassrc.js

module.exports = { data: '@import "./src/styles";' };

@danieltian
Copy link

danieltian commented Aug 11, 2019

Is there any way of doing the same with Stylus? This is the last thing I need to get working in Parcel before I can switch to it wholesale, because I need the ability to globally import my mixins and variables into every file. This is the webpack loader config I'm trying to reproduce:

{
  test: /\.styl(us)?$/,
  use: 'vue-style-loader', 'css-loader', {
    loader: 'stylus-loader',
    options: {
      import: [
        path.resolve(__dirname, 'src', 'mixins.styl'),
        path.resolve(__dirname, 'src', 'colors.styl'),
        path.resolve(__dirname, 'src', 'variables.styl')
      ]
    }
  }
}

@SonyaOrlova
Copy link

SonyaOrlova commented Oct 22, 2019

.sassrc.js to your project root folder and link it with your entry point

@Neefay hi! what do you mean "link with your entry point"?

@zuavra
Copy link

zuavra commented Dec 2, 2019

@Neefay .sassrc.js did not work for me, but I got it to work by loading the main SCSS file in index.html:

<head>
    <link rel="stylesheet" href="./css/index.scss">
</head>

From there you can @import './global/variables.scss and @import './components/app.scss and so on.

It's a pity that you can't load .scss files from .vue files since I would have liked to make them scoped, but at least this is working.

Edit: I should also mention I had to use node-sass, since with sass it was not hot-reloading the .scss files when modified.

@cjones26
Copy link

cjones26 commented Mar 5, 2020

This one was a bit of a blow to my initial Parcel excitement, but I'm sure we'll see an implementation soon.

EDIT: After looking around for a bit, this solution has worked wonders for me.

Just add .sassrc.js to your project root folder and link it with your entry point. Parcel Magic® takes care of the rest.

.sassrc.js

module.exports = { data: '@import "./src/styles";' };

I have tried a multitude of things, but this was something which actually worked. Thank you, @Neefay!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

13 participants