Coder Social home page Coder Social logo

react-setup's Introduction

Setting up a React project

Initializing the NPM project

npm init --yes

Install React.

npm install react react-dom

Install webpack.

npm install -D webpack webpack-dev-server webpack-cli
mkdir -p public
touch public/index.html

Create an index.html file.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="main.js" type="text/javascript"></script>
  </body>
</html>

Add a NPM start script.

cat package.json | jq .scripts
{
  "start": "webpack-dev-server",
  "test": "echo \"Error: no test specified\" && exit 1"
}

Install babel.

npm install -D @babel/core @babel/preset-react

Configure webpack to use babel.

cat webpack.config.js
const path = require('path');

const config = {
  entry: ['./src/index.js'],

  module: {
    rules: [
      {
	test: /\.jsx?/,
	loaders: ['babel-loader'],
	exclude: /node_modules/,
      },
    ],
  },
};

module.exports = config;

Install the babel-loader webpack plugin.

npm install -D babel-loader

Add babel configuration to package.json.

cat package.json | jq .babel
{
  "presets": [
    "@babel/preset-react"
  ]
}

Load the index.html page.

npm install -D html-webpack-plugin

Add the plugin to the webpack.config.js file.

{
  //...
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: './index.html',
    }),
  ]
}

Handle CSS with post-css.

npm install -D \
    style-loader \
    css-loader \
    precss \
    postcss-loader

Add a rule in webpack.config.js for handling CSS files.

node -p "
  require('./webpack.config.js')
    .module
    .rules
    .find((r) => r.test.exec('is.css'))
"
{ test: /\.css$/,
  use:
   [ 'style-loader',
     { loader: 'css-loader', options: [Object] },
     'postcss-loader' ] }

And setup postcss in package.json.

cat package.json | jq .postcss
{
  "map": false,
  "plugins": {
    "postcss-import": {},
    "postcss-preset-env": {
      "stage": 3
    }
  }
}

Now we install the postcss plugins.

npm install -D postcss-import postcss-preset-env

Now we setup a eslint configuration.

npm install -D eslint
cat .eslintrc.json
{
  "env": {
    "browser": true,
    "es6": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "plugins": [
    "react"
  ],
  "rules": {
    "indent": [
      "error",
      2
    ],
    "linebreak-style": [
      "error",
      "unix"
    ],
    "quotes": [
      "error",
      "single"
    ],
    "semi": [
      "error",
      "always"
    ]
  }
}

And prettier.

npm install -D prettier
cat .prettierrc
{
  "tabWidth": 2,
  "singleQuote": true,
  "printWidth": 100,
  "trailingComma": "es5",
  "semi": true,
  "arrowParens": "always"
}

react-setup's People

Contributors

iensu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.