Skip to content

Fix ICSS syntax in stylesheets - #10511

Merged
ianschmitz merged 1 commit into
react:masterfrom
thabemmz:master
Apr 12, 2021
Merged

Fix ICSS syntax in stylesheets#10511
ianschmitz merged 1 commit into
react:masterfrom
thabemmz:master

Conversation

@thabemmz

Copy link
Copy Markdown

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
Copy Markdown

bump

@vitalbone

Copy link
Copy Markdown

Bump.

@kintz09

Copy link
Copy Markdown

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

@richardoptibriumrichardoptibrium left a comment

Copy link
Copy Markdown

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?

@ianschmitzianschmitz added this to the 4.1 milestone Apr 12, 2021
@ianschmitzianschmitz changed the title Add explicit compileType for css-loader configuration in webpack.configFix ICSS syntax in stylesheetsApr 12, 2021
@ianschmitz
ianschmitz merged commit ea4002e into react:masterApr 12, 2021
@ianschmitz

Copy link
Copy Markdown
Contributor

Thanks @thabemmz!

@richardoptibrium

Copy link
Copy Markdown

Thanks guys.

@Bagnall

Copy link
Copy Markdown

When will this be released?

@TakanashiOuken

TakanashiOuken commented May 26, 2021

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

abhiisheek pushed a commit to abhiisheek/create-react-app that referenced this pull request May 24, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICSS export not working in CRA 4 CRA 4 failed to make SASS variables available to JavaScript (OK with CRA 3.x)

10 participants

@thabemmz@asherccohen@vitalbone@kintz09@ianschmitz@richardoptibrium@Bagnall@TakanashiOuken@sante@facebook-github-bot