This repository was archived by the owner on May 16, 2024. It is now read-only.
-
Couldn't load subscription status.
- Fork 4
Style options
Dima Vyshniakov edited this page Oct 8, 2022
·
3 revisions
Template uses vanilla CSS with autoprefixer enabled. To avoid class name collisions and reduce nesting, we are using css-modules. To make css-modules work, the stylesheet file name should have .module suffix.
import React from 'react';
import classes from './Component.module.css';
const Component = () => (
<div className={classes.wrapper}>Component</div>
)CRA doesn't support style pre-processors, except SASS. But this doesn't mean, that we shouldn't use them. In order to add support for custom style processor without ejecting, we can use file watchers. File watchers will track changes in style files and compile them to vanilla CSS, consumed by CRA.
SASS/SCSS support comes “out of the box” in CRA. To enable it:
- Install
node-sass
yarn add node-sass --dev- Import SASS/SCSS files straight into Component.
import React from 'react';
import classes from './Component.module.scss'; // note the changed extension
const Component = () => (
<div className={classes.wrapper}>Component</div>
)- Change
.lintstagedrcto lint SCSS files instead of CSS.
{
"*.js": [
"eslint --fix"
],
"*.scss": [
"stylelint --fix"
]
}- Install
postcss-cliand related plugins:
yarn add --dev postcss-nested postcss-cli postcss-preset-env npm-run-all- Modify package scripts:
{
"build:style": "postcss src/**/*.pcss --dir src --base src --ext css",
"watch:style": "yarn build:style -w",
"start": "npm-run-all -p watch:style start:js",
"start:js": "react-scripts start",
"build:js": "react-scripts build",
"build": "npm-run-all build:style build:js"
}- Add
postcss.config.jsfile in the root folder. With following configuration:
const pkg = require('./package.json');
module.exports = {
plugins: [
require('postcss-nested'), // handle nested selectors, like LESS or SASS
require('postcss-preset-env')({
browsers: pkg.browserslist.production, // use browsers list from production mode
stage: 1,
}),
],
};- Add rule to
.gitignoreand.stylelintrcto ignore all CSS files, since we are generating them.
.gitignore
# css
*.css
.stylelintrc
{
"ignoreFiles": ["**/*.snap", "**/*.css"]
}- Change
.lintstagedrcto lint files with.pcssextension instead of.css.
{
"*.js": [
"eslint --fix"
],
"*.pcss": [
"stylelint --fix"
]
}- Install less and related plugins:
yarn add --dev less less-watch-compiler npm-run-all- Modify package scripts:
{
"build:style": "yarn watch:style --run-once",
"watch:style": "less-watch-compiler src src",
"start": "npm-run-all -p watch:style start:js",
"start:js": "react-scripts start",
"build:js": "react-scripts build",
"build": "npm-run-all build:style build:js"
}- Add rule to
.gitignoreand.stylelintrcto ignore all CSS files, since we are generating them.
.gitignore
# css
*.css
.stylelintrc
{
"ignoreFiles": ["**/*.snap", "**/*.css"]
}- Change
.lintstagedrcto lint.lessfiles instead of.css.
{
"*.js": [
"eslint --fix"
],
"*.less": [
"stylelint --fix"
]
}