-
-
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
CRA 4 failed to make SASS variables available to JavaScript (OK with CRA 3.x) #10047
Comments
I've got some updates... I have just discovered, that CRA 4 is able to read the SASS variables if the SASS file is named as In
Not ideal (these SASS files are not CSS modules in this context) but it works. |
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. |
The issue is not resolved yet. |
yeah this solution works fine, but still issue is not resolved :( |
With the new release of CRA (4.0.2), I looked into this again. Unfortunately, it is still not working. However, after seeing this issue at css-loader, I decided to look into this again and found a more elegant solution than the one described above. The issue is caused by the fact that in the Webpack configuration offered by Create React App. In there, the non-module This can be configured in the This means the Webpack configuration offered by Create React App needs to be modified. Since we use craco in our project, we can do the following. In
The above configuration will make exporting SASS variables work again (without renaming files to Of course, this is very hacky and fully depends on the current setup of the CRA webpack configuration. I'll issue a PR ASAP at CRA to fix this over there. |
PR can be found here: #10511 |
That's great news! Thank your for the PR @thabemmz. Hope it will be merged soon. |
@thabemmz Thanks for looking into this, much appreciated. |
Just bumping this issue, came across it today |
Bump |
I ran into this issue today while migrating a project into a new CRA4 app. Hoping this gets fixed soon. Implementing the craco config did temporarily fit it for me. |
Is this fix merged in 4.0.3? |
@TakanashiOuken The issue is not resolved yet with 4.0.3 |
Getting same issues in latest version 4.0.3 and Also having vulnerabilities due to css-loader dependancies |
Bump. Please could you merge/release the fix?! |
The .module solution above worked for me, but in case anybody run into this issue: I could not access a sass variable in JavaScript because i exported a variable name starting with $. Works: Doesn't work: |
…dency upgrades -- see facebook/create-react-app#10047 for details
Looks like the fix was released with CRA v5 but it didn't resolve the issue for me. I still get an error on
|
Will this fix be backported as a patch in CRA4? Or will we be required to upgrade to CRA5 to resolve this issue without using CRACO? |
How to read SASS variables in JavaScript?
With Create React App 3.x it is possible to make SASS variables available to JavaScript. To make variables available, we simply need to export them with the
:export
syntax. For example:We can then import the SCSS file into JavaScript to access the variables defined in the file:
It can be really useful to export SASS variables. For example, in our projects, we often create custom themes for Material UI to change the colors, the typography, the components... and will also use SASS. Therefore, we can directly import SASS variables in our Material UI theme:
As far as I know,
react-scripts
usecss-loader
as a dependency to configure webpack to enable this feature.More info about
:export
here and here.Describe the bug
Create Rect App 4.0.0 cannot read the exported variables. The variables are undefined.
Did you try recovering your dependencies?
Yes.
Which terms did you search for in User Guide?
N/A
Environment
Output of
npx create-react-app --info
:Steps to reproduce
N.B: You can clone my demo repositories (see below) if you don't want to manually reproduce the bug.
Requirements: you need to setup Sass in Create React App. Please follow the documentation here.
:export
keyword (see example above)Expected behavior
The value of the SASS variable should be available in JavaScript.
Actual behavior
The variable is undefined.
Reproducible demo
You can close the following repositories and just run
yarn start
.With CRA 3:
https://github.com/guicara/create-react-app-3-bug-scss-variable-export
With CRA 4:
https://github.com/guicara/create-react-app-4-bug-scss-variable-export
Anyone have some ideas or suggestions?
Thank you for your help!
The text was updated successfully, but these errors were encountered: