You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add SASS support documentation facebook#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
Copy file name to clipboardexpand all lines: packages/react-scripts/template/README.md
+47
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
20
20
-[Importing a Component](#importing-a-component)
21
21
-[Adding a Stylesheet](#adding-a-stylesheet)
22
22
-[Post-Processing CSS](#post-processing-css)
23
+
-[Adding a CSS Preprocessor (Sass, Less etc.)](#adding-a-css-preprocessor-sass-less-etc)
23
24
-[Adding Images and Fonts](#adding-images-and-fonts)
24
25
-[Using the `public` Folder](#using-the-public-folder)
25
26
-[Changing the HTML](#changing-the-html)
@@ -353,6 +354,52 @@ becomes this:
353
354
354
355
There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
355
356
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`:
>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
+
356
403
## Adding Images and Fonts
357
404
358
405
With Webpack, using static assets like images and fonts works similarly to CSS.
0 commit comments