-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
create-react-app 2.0, how to use absolute path import in sass/scss files? #4494
Comments
Start read from #4195 (comment) I think implementation is not that complex. @gaearon @Timer what do you think of re-using NODE_PATH as SCSS includePaths? |
I'm good with adding |
@Timer kinda weird. I have been using this option for years, works like a charm. |
See webpack-contrib/sass-loader#556 (comment) and webpack-contrib/sass-loader#558 (comment), I'm not sure if they're relevant or not. Does |
I was using NODE_PATH just fine with:
|
Is this issue related to #4651? |
I'm successfully using absolute imports with /.env:
given that /src/styles/variables.scss exists, in any .scss file:
works fine with in v2.0. |
@justo This is fine when you have full control over the scss files. It won't work for existing component libraries that don't support the ~ syntax for imports. |
@Timer This is an issue when using SCSS files from NPM packages where they also get SCSS files from other packages. A very good example is @import '~@material/button/mdc-button'; But inside their SCSS file they have: @import "@material/ripple/common";
@import "@material/ripple/mixins";
@import "./mixins";
@import "./variables"; So the build breaks as they do not have the For CRA, including this option fixes this problem (tested on my local): includePaths: ["node_modules"] |
I agree with @fbarbare. I have the same issue with I understand the philosophy of the project is to prefer convention over configuration. The default behavior works for most cases. However, there are situations where the default behavior does not work. Either, this is enabled by default or put behind a feature flag considering the issues mentioned above by @Timer. Thoughts? |
I am facing the same issue with @Material. Is there any way to solve this without ejecting? |
I would like to add that adding a For multiple sass paths, you should be able to use |
I confirm the solution @jayantbh provided, it worked flawlessly! |
@gudh Does this solution work for you? Can we close this issue now? |
@iansu I think it does, last week I also had this issue and the way I solved was following the tutorial https://github.com/material-components/material-components-web-react with some changes
// ./src/App.scss
@import "@material/react-button/index.scss";
...
I used ubuntu 18.04.1 LTS and CRA 2.1.1. |
@LuisAlbertoVasquezVargas I tried this solution but I still get the following error: I also tried setting the the SASS_PATH as an environment variable in my user account, with no success. |
@Vilvalas I think I forgot to add the tilde "~" before @Material in those instructions, my bad, https://github.com/LuisAlbertoVasquezVargas/material-testing/blob/4d45e75429db817813ffa92ceb91b7c9038400b6/src/App.scss#L55 |
@LuisAlbertoVasquezVargas If I use the tilde the initial import from App.scss works. |
@Vilvalas I see what happened, days before I tried different approaches to set up a simple app and some things were omitted and others taken for granted. Now this post will replace the previous one. Step 1: Create a blank react app
Then if everything goes fine press Step 2: Install React Button Component
Step 3: Using SassAdd environment variableexport SASS_PATH=./node_modules
Install SASSnpm install node-sass Rename your css fileRename your // ./src/App.scss
@import "@material/react-button/index.scss";
... Step 4: Replace codeOpen import React, {Component} from 'react';
import Button from '@material/react-button';
import './App.scss';
// add the appropriate line(s) in Step 3a if you are using compiled CSS instead.
class App extends Component {
render() {
return (
<div>
<Button
raised
onClick={() => console.log('clicked!')}
>
Click Me!
</Button>
</div>
);
}
}
export default App; Run your App
UPD: There is no need to add tildes anymore. |
Can confirm this works, thank you. I'm not really sure what the problem in my project was. I think the optimal solution would be to create a .env file with like @jayantbh commented earlier. |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue. |
Is this a bug report?
No
The problem
I am using creacte-react-app 2.0.0-next.66cc7a90, which already has support for Sass/Scss files
however with default relative import in Sass/Scss files, i have to use code like
@import "../../../../../styles/variables/_variables.scss";
instead I want to use absolute import so i can import with code
@import "styles/variables/_variables.scss";
I know one way to acheve so is to update options
but I want to do it without ejecting, how can i do it? using react-app-rewired?
The text was updated successfully, but these errors were encountered: