Goal: add react and css modules
npm install --save-dev autoprefixer babel-preset-react css-loader node-sass postcss-loader prop-types react react-dom sass-loader style-loader stylelint stylelint-config-standard
react
andreact-dom
: to use reactsass-loader
andnode-sass
: for webpack to load SCSS files and convert them into CSScss-loader
: for webpack to load CSS files and, in our case, to parse them into CSS modulesstyle-loader
: for webpack to bundle CSS into our javascript, and inline it into the page on demand (to extract all CSS into an external CSS file, you'd need to use ExtractTextPlugin)stylelint
andstylelint-config-mailonline
: to lint our CSSbabel-preset-react
: to add some plugins to our babel config, especially to transpile JSX syntaxautoprefixer
andpostcss-loader
: to post-process CSS, adding vendor prefixes to our rules
- Add
babel-preset-react
to our babel config in.babelrc
:{ "presets": ["es2015", "react"] }
- Create
.stylelintrc.json
:{ "extends": "stylelint-config-standard" }
- Modify
.eslintrc.json
to addmailonline/react
:{ "extends": ["mailonline", "mailonline/react"], "root": true }
- Create
postcss.config.js
(don't worry too much about it right now):/* eslint-disable import/no-commonjs, import/unambiguous, filenames/match-regex */ const autoprefixer = require('autoprefixer'); module.exports = { plugins: [ autoprefixer({ browsers: [ '> 1%', 'last 3 versions', 'iOS > 8', 'not ie < 10' ] }) ] };
- Modify your
module -> rules
array inwebpack.config.babel.js
to add a loader for.scss
files:... module: { rules: [ { loader: 'babel-loader', test: /\.js$/ }, { test: /\.scss$/, use: [ 'style-loader?sourceMap', { loader: 'css-loader', options: { importLoaders: 3, localIdentName: '[local]__[hash:base64:5]', modules: true, sourceMap: true } }, 'postcss-loader?sourceMap', 'sass-loader?sourceMap' ] } ] }, ...
- Replace your
lint
script inpackage.json
with this:"lint-scripts": "eslint --ignore-path .gitignore '**/*.js'", "lint-styles": "stylelint --ignore-path .gitignore '**/*.{scss,sass,css}'", "lint": "npm run lint-scripts && npm run lint-styles && npm run lint-json",
- Create
src/styles.scss
:.container { padding: 16px; background: #eee; border: 1px solid black; }
- Modify
src/index.js
:import React from 'react'; import {render} from 'react-dom'; import styles from './styles.scss'; render( <div className={styles.container}>Hello world!</div>, document.querySelector('[data-react-workshop]') );
- Modify
demo/index.html
, add this before thescript
element, in the body:<div data-react-workshop></div>
- Run
npm run start
and openlocalhost:8080
(or whichever port applies), you should see a grey box with a black border and "Hello world!" inside
Now you're ready for Step 3