Skip to content

Commit d7bffca

Browse files
committed
Update Sass documentation
1 parent 21c7511 commit d7bffca

File tree

1 file changed

+27
-95
lines changed

1 file changed

+27
-95
lines changed

packages/react-scripts/template/README.md

Lines changed: 27 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ You can find the most recent version of this guide [here](https://github.com/fac
2525
- [Code Splitting](#code-splitting)
2626
- [Adding a Stylesheet](#adding-a-stylesheet)
2727
- [Adding a CSS Modules stylesheet](#adding-a-css-modules-stylesheet)
28+
- [Adding a Sass stylesheet](#adding-a-sass-stylesheet)
2829
- [Post-Processing CSS](#post-processing-css)
29-
- [Adding a CSS Preprocessor (Sass, Less etc.)](#adding-a-css-preprocessor-sass-less-etc)
3030
- [Adding Images, Fonts, and Files](#adding-images-fonts-and-files)
3131
- [Using the `public` Folder](#using-the-public-folder)
3232
- [Changing the HTML](#changing-the-html)
@@ -567,6 +567,32 @@ No clashes from other `.error` class names
567567

568568
**This is an optional feature.** Regular html stylesheets and js imported stylesheets are fully supported. CSS Modules are only added when explicitly named as a css module stylesheet using the extension `.module.css`.
569569

570+
## Adding a Sass stylesheet
571+
572+
Generally, we recommend that you don’t reuse the same CSS classes across different components. For example, instead of using a `.Button` CSS class in `<AcceptButton>` and `<RejectButton>` components, we recommend creating a `<Button>` component with its own `.Button` styles, that both `<AcceptButton>` and `<RejectButton>` can render (but [not inherit](https://facebook.github.io/react/docs/composition-vs-inheritance.html)).
573+
574+
Following this rule often makes CSS preprocessors less useful, as features like mixins and nesting are replaced by component composition. You can, however, integrate a CSS preprocessor if you find it valuable.
575+
576+
To use Sass, first install `node-sass`:
577+
578+
```bash
579+
$ npm install node-sass --save
580+
$ # or
581+
$ yarn add node-sass
582+
```
583+
584+
Now you can rename `src/App.css` to `src/App.scss` and update `src/App.js` to import `src/App.scss`.
585+
This file and any other file will be automatically compiled if imported with the extension `.scss` or `.sass`.
586+
587+
To share variables between Sass files, you can use Sass imports. For example, `src/App.scss` and other component style files could include `@import "./shared.scss";` with variable definitions.
588+
589+
This will allow you to do imports like
590+
591+
```scss
592+
@import 'styles/_colors.scss'; // assuming a styles directory under src/
593+
@import 'nprogress/nprogress'; // importing a css file from the nprogress node module
594+
```
595+
570596
## Post-Processing CSS
571597

572598
This project setup minifies your CSS and adds vendor prefixes to it automatically through [Autoprefixer](https://github.com/postcss/autoprefixer) so you don’t need to worry about it.
@@ -600,100 +626,6 @@ becomes this:
600626

601627
If you need to disable autoprefixing for some reason, [follow this section](https://github.com/postcss/autoprefixer#disabling).
602628

603-
## Adding a CSS Preprocessor (Sass, Less etc.)
604-
605-
Generally, we recommend that you don’t reuse the same CSS classes across different components. For example, instead of using a `.Button` CSS class in `<AcceptButton>` and `<RejectButton>` components, we recommend creating a `<Button>` component with its own `.Button` styles, that both `<AcceptButton>` and `<RejectButton>` can render (but [not inherit](https://facebook.github.io/react/docs/composition-vs-inheritance.html)).
606-
607-
Following this rule often makes CSS preprocessors less useful, as features like mixins and nesting are replaced by component composition. You can, however, integrate a CSS preprocessor if you find it valuable. In this walkthrough, we will be using Sass, but you can also use Less, or another alternative.
608-
609-
First, let’s install the command-line interface for Sass:
610-
611-
```sh
612-
npm install --save node-sass-chokidar
613-
```
614-
615-
Alternatively you may use `yarn`:
616-
617-
```sh
618-
yarn add node-sass-chokidar
619-
```
620-
621-
Then in `package.json`, add the following lines to `scripts`:
622-
623-
```diff
624-
"scripts": {
625-
+ "build-css": "node-sass-chokidar src/ -o src/",
626-
+ "watch-css": "node-sass-chokidar src/ -o src/ --watch",
627-
"start": "react-scripts start",
628-
"build": "react-scripts build",
629-
"test": "react-scripts test",
630-
```
631-
632-
> Note: To use a different preprocessor, replace `build-css` and `watch-css` commands according to your preprocessor’s documentation.
633-
634-
Now you can rename `src/App.css` to `src/App.scss` and run `npm run watch-css`. The watcher will find every Sass file in `src` subdirectories, and create a corresponding CSS file next to it, in our case overwriting `src/App.css`. Since `src/App.js` still imports `src/App.css`, the styles become a part of your application. You can now edit `src/App.scss`, and `src/App.css` will be regenerated.
635-
636-
To share variables between Sass files, you can use Sass imports. For example, `src/App.scss` and other component style files could include `@import "./shared.scss";` with variable definitions.
637-
638-
To enable importing files without using relative paths, you can add the `--include-path` option to the command in `package.json`.
639-
640-
```
641-
"build-css": "node-sass-chokidar --include-path ./node_modules src/ -o src/",
642-
"watch-css": "node-sass-chokidar --include-path ./node_modules src/ -o src/ --watch",
643-
```
644-
645-
This will allow you to do imports like
646-
647-
```scss
648-
@import 'styles/_colors.scss'; // assuming a styles directory under src/
649-
@import 'nprogress/nprogress'; // importing a css file from the nprogress node module
650-
```
651-
652-
At this point you might want to remove all CSS files from the source control, and add `src/**/*.css` to your `.gitignore` file. It is generally a good practice to keep the build products outside of the source control.
653-
654-
As a final step, you may find it convenient to run `watch-css` automatically with `npm start`, and run `build-css` as a part of `npm run build`. You can use the `&&` operator to execute two scripts sequentially. However, there is no cross-platform way to run two scripts in parallel, so we will install a package for this:
655-
656-
```sh
657-
npm install --save npm-run-all
658-
```
659-
660-
Alternatively you may use `yarn`:
661-
662-
```sh
663-
yarn add npm-run-all
664-
```
665-
666-
Then we can change `start` and `build` scripts to include the CSS preprocessor commands:
667-
668-
```diff
669-
"scripts": {
670-
"build-css": "node-sass-chokidar src/ -o src/",
671-
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
672-
- "start": "react-scripts start",
673-
- "build": "react-scripts build",
674-
+ "start-js": "react-scripts start",
675-
+ "start": "npm-run-all -p watch-css start-js",
676-
+ "build-js": "react-scripts build",
677-
+ "build": "npm-run-all build-css build-js",
678-
"test": "react-scripts test",
679-
"eject": "react-scripts eject"
680-
}
681-
```
682-
683-
Now running `npm start` and `npm run build` also builds Sass files.
684-
685-
**Why `node-sass-chokidar`?**
686-
687-
`node-sass` has been reported as having the following issues:
688-
689-
- `node-sass --watch` has been reported to have _performance issues_ in certain conditions when used in a virtual machine or with docker.
690-
691-
- Infinite styles compiling [#1939](https://github.com/facebook/create-react-app/issues/1939)
692-
693-
- `node-sass` has been reported as having issues with detecting new files in a directory [#1891](https://github.com/sass/node-sass/issues/1891)
694-
695-
`node-sass-chokidar` is used here as it addresses these issues.
696-
697629
## Adding Images, Fonts, and Files
698630

699631
With Webpack, using static assets like images and fonts works similarly to CSS.

0 commit comments

Comments
 (0)