Skip to content

Commit 645ba66

Browse files
tsironisTimer
authored andcommitted
Add SASS support documentation #1007 (#1008)
* Add SASS support documentation #1007 * Change SASS section title to more generic label * Fix link in Table of Contents * Chain build-css with watch-css script, fix typos * Update Sass and Less naming style * Fix wording, remove offensive words * Slightly rewite
1 parent 12e8cea commit 645ba66

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/react-scripts/template/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
2020
- [Importing a Component](#importing-a-component)
2121
- [Adding a Stylesheet](#adding-a-stylesheet)
2222
- [Post-Processing CSS](#post-processing-css)
23+
- [Adding a CSS Preprocessor (Sass, Less etc.)](#adding-a-css-preprocessor-sass-less-etc)
2324
- [Adding Images and Fonts](#adding-images-and-fonts)
2425
- [Using the `public` Folder](#using-the-public-folder)
2526
- [Changing the HTML](#changing-the-html)
@@ -353,6 +354,52 @@ becomes this:
353354

354355
There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
355356

357+
## Adding a CSS Preprocessor (Sass, Less etc.)
358+
359+
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)).
360+
361+
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.
362+
363+
First, let’s install the command-line interface for Sass:
364+
365+
```
366+
npm install node-sass --save-dev
367+
```
368+
369+
Then in `package.json`, add the following lines to `scripts`:
370+
371+
```diff
372+
"scripts": {
373+
+ "build-css": "node-sass src/ -o src/",
374+
+ "watch-css": "npm run build-css && node-sass src/ -o src/ --watch",
375+
"start": "react-scripts start",
376+
"build": "react-scripts build",
377+
"test": "react-scripts test --env=jsdom",
378+
```
379+
380+
>Note: To use a different preprocessor, replace `build-css` and `watch-css` commands according to your preprocessor’s documentation.
381+
382+
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.
383+
384+
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.
385+
386+
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 `&` operator for executing scripts in parallel, and `&&` for executing them sequentially:
387+
388+
```diff
389+
"scripts": {
390+
"build-css": "node-sass src/ -o src/",
391+
"watch-css": "npm run build-css && node-sass src/ -o src/ --watch",
392+
- "start": "react-scripts start",
393+
- "build": "react-scripts build",
394+
+ "start": "react-scripts start & npm run watch-css",
395+
+ "build": "react-scripts build && npm run build-css",
396+
"test": "react-scripts test --env=jsdom",
397+
"eject": "react-scripts eject"
398+
}
399+
```
400+
401+
Now running `npm start` and `npm run build` also builds Sass files. Note that `node-sass` seems to have an [issue recognizing newly created files on some systems](https://github.com/sass/node-sass/issues/1891) so you might need to restart the watcher when you create a file until it’s resolved.
402+
356403
## Adding Images and Fonts
357404

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

0 commit comments

Comments
 (0)