Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use absolute path to import custom scss, when using react + webpack?

Inside a scss file, I'm trying to import custom, widely used chunk of scss (in a React/SASS/Webpack stack).

So that I can use a shared mixin.

Let's say I'm creating an MyAdminButton and I want to import scss file that concerns all the buttons of the project. (It's custom scss, not vendor/external one).

It would look like this :

//this actually works but it is a code smell : what if the current file moves ? @import "../../stylesheets/_common-btn-styles.scss";  .my-admin-btn {     // here I can use a shared mixin defined in _common-btn-styles.scss } 

This sounds not good since if my scss file move, then everything is broken.

Thanks for your help

like image 860
bdavidxyz Avatar asked Mar 11 '16 10:03

bdavidxyz


People also ask

How do I import main SCSS file into react?

To add SCSS styles to a React project, we can install the sass package if the React project is created with create-react-app. Then we import a SCSS file by writing: import '../scss/styles.

How do you use absolute path in react?

When initializing React Project with create-react-app, we can configure our React application to support importing modules with absolute paths. Note: We can create the jsconfig. json file if it doesn't exist. Now we have the working absolute imports setting with src folder as custom base directory.


1 Answers

Found. Actually you can configure sass-loader in webpack.config.json, as described here : https://github.com/jtangelder/sass-loader

Here is the relevant part :

sassLoader: {    includePaths: [path.resolve(__dirname, "./some-folder")] } 
like image 72
bdavidxyz Avatar answered Oct 02 '22 11:10

bdavidxyz