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

Fix ICSS syntax in stylesheets #10511

Merged
merged 1 commit into from
Apr 12, 2021
Merged

Fix ICSS syntax in stylesheets #10511

merged 1 commit into from
Apr 12, 2021

Conversation

thabemmz
Copy link
Contributor

@thabemmz thabemmz commented Feb 5, 2021

Context

After upgrading Create React App to v4, the exported SASS variables did not work anymore in my project. This was a known issue for CRA@v4.

Cause of the issue

In CRA@v4, css-loader was upgraded from v3 to v4. In css-loader v4 there was a bug that did not handle ICSS (the syntax within CSS modules used to :import and :export) imports properly. This has been resolved by adding the compileType property in v4.2.0.

In the README of css-loader, there is a whole section about how to configure your setup to support ICSS properly when used together with CSS modules (see here).

What has been done?

  • Pass a compileType to all style rules in Webpack config of CRA. Use icss for non-modules and module for all modules

How to test?

I tested this in my own project, but in the Create-React-App project, I verified this works by doing the following:

  • Ran the project using yarn start
  • Verify the interface of the App template is shown properly
  • Now, within packages/cra-template/template/src, rename App.css to App.module.css, in the class-names replace all kebab-cased classnames to camelcase (for testing purposes)
  • In App.js, change the import to import Styles from './App.module.css'; and replace all classnames with Styles.App-like variant
  • Verify the interface of the App template is shown properly
  • Add a file foo.css with the following content: :export { black: #000000; }
  • In App.js, add the following import: import vars from './foo.css'
  • In App.js, change the <p> on line 10 to <p style={{ color: vars.black }}>
  • Verify the interface of the App template displays the text in black. This confirms the :export syntax is working properly.
  • Now, in your terminal, stop the running project. Then run (cd packages/cra-template && yarn add node-sass) and run yarn start again
  • Rename all .css files within packages/cra-template/template/src to .scss
  • Verify the interface of the App template displays the text in black. This confirms the :export syntax is working properly, even in SCSS 🎉

@asherccohen
Copy link

bump

@vitalbone
Copy link

Bump.

@kintz09
Copy link

kintz09 commented Mar 26, 2021

Is this merge still being reviewed? I'm hoping this issue gets resolved soon so I can remove the craco workaround from my project.

Copy link

@richardoptibrium richardoptibrium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are straightforward. Surely this can be merged?

@ianschmitz ianschmitz added this to the 4.1 milestone Apr 12, 2021
@ianschmitz ianschmitz changed the title Add explicit compileType for css-loader configuration in webpack.config Fix ICSS syntax in stylesheets Apr 12, 2021
@ianschmitz ianschmitz merged commit ea4002e into facebook:master Apr 12, 2021
@ianschmitz
Copy link
Contributor

Thanks @thabemmz!

@richardoptibrium
Copy link

Thanks guys.

@Bagnall
Copy link

Bagnall commented May 7, 2021

When will this be released?

@TakanashiOuken
Copy link

TakanashiOuken commented May 26, 2021

Is this fix included in the 4.0.3 version?

wombleton pushed a commit to AurorNZ/create-react-app that referenced this pull request Jun 1, 2021
@richardoptibrium
Copy link

This does not appear to be fixed in 4.0.3. Could someone release the fix please?

sumanthratna pushed a commit to sumanthratna/create-react-app that referenced this pull request Aug 4, 2021
@sante
Copy link

sante commented Sep 16, 2021

When will a new version be released? So this fix can be available

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