I'm trying to find my way in setting up an efficient web development workflow even though I am new to React and bundlers (and the combi of these two). My goal is to have each React component use its own SCSS file, which can't be accessed by other components' stylesheets. There needs to be some global files though, like a variables.scss
, which is where the problem lies.
This is how my file structure (from the src folder) looks at the moment:
├── assets
├── components
│ ├── App
│ │ ├── index.js
│ │ └── index.scss
│ ├── Taskbar
│ │ ├── index.js
│ │ └── index.scss
│ └── Window
│ ├── index.js
│ └── index.scss
├── index.js
└── sass
├── mixins.scss
└── variables.scss
I'm using Parcel with node-sass to import these SCSS files. As you can see, each component has its own folder with a SCSS file in it.
Each component loads its designated stylesheet like this:
import React from 'react';
import './index.scss';
import Taskbar from '../Taskbar';
export default class App extends React.Component {
render() {
return (
<div id="app">
<Taskbar />
</div>
)
}
}
I like to keep this approach, as the stylesheets are only used if the components themselves are imported.
The problem
If you take one more look at my file structure, there are two files in the sass folder (variables.scss and mixins.scss). I want all the files in that folder to be accessible to all components.
I've tried to just plain simple import the files in the App, so it can be accessed in all files, like so:
import '../../sass/variables.scss';
import '../../sass/mixins.scss';
These do load of course, but I can't use the variables and functions declared in these files in, for example, Taskbar/index.scss
.
So that would make my question: How do I use these global sass variables/functions in "protected" SCSS files?