question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility with create-react-app

See original GitHub issue

What?

Rename scss files that are designed to work as CSS Modules to _{moduleName}.module.scss

or (maybe, may need more thought) include a second file called _{moduleName}.module.scss that composes _{moduleName}.scss

Why?

Create React App 2 now supports Sass and CSS Modules:

More styling options: you can use Sass and CSS Modules out of the box. https://reactjs.org/blog/2018/10/01/create-react-app-v2.html

Create React App has the convention that if you do:

import styles from "./button.scss";

It will treat button.scss as a normal SCSS file and append the styles to your stylesheet.

However, if you do:

import styles from "./button.module.scss";

It will treat button.module.scss as a CSS module and return an object to JS for class name lookup.

Consider the following:

import React from "react";
import styles from "./button.module.scss"; // works
// import styles from "govuk-frontend/components/button/_button.scss"; // doesn't work
// import styles from "@penx/govuk-frontend/components/button/_button.module.scss"; // works

const Button = props => (
  <button className={styles["govuk-button"]} {...props} />
);

export default Button;

Where button.module.scss is:

@import "~govuk-frontend/components/button/button";

.govuk-button {
  composes: govuk-button;
}

At the moment I have to create a mostly redundant file to make sure that _button.scss is picked up as a CSS module.

Using:

import styles from "govuk-frontend/components/button/_button.scss"; 

Imports the CSS file but does not treat it as CSS module, so this does not work.

I have created a fork of govuk-frontend and renamed _button.scss to _button.module.scss, so if you do the following:

import styles from "@penx/govuk-frontend/components/button/_button.module.scss"; 

This makes govuk-frontend work with create-react-app out of the box.

You can try this out on the following sandbox: https://codesandbox.io/s/w0x8xk5lk

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NickColleycommented, Feb 6, 2019

We’ve spent time going over this with Alasdair and decided we won’t support this directly in GOV.UK Frontend for now, if you’d like to see the details we’ve put together a proposal you can read..

Thanks for all your work on this.

1reaction
kr8n3rcommented, Nov 12, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Supported Browsers and Features | Create React App
By default, the generated project supports all modern browsers. Support for Internet Explorer 9, 10, and 11 requires polyfills. For a set of ......
Read more >
Create a New React App
Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in...
Read more >
create-react-app - npm
This package includes the global command for Create React App. Please refer to its documentation: Getting Started – How to create a new...
Read more >
facebook/create-react-app: Set up a modern web app ... - GitHub
Create React App works on macOS, Windows, and Linux. If something doesn't work, please file an issue. If you have questions or need...
Read more >
What Does Create-React-App Actually Do? | by Andrew Mc
Next inside create-react-app (CRA), your command runs through a series of checks. It finds information about your system, looks if you have any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found