Skip to content

Instantly share code, notes, and snippets.

@teksrc
Last active March 23, 2019 00:57
Show Gist options
  • Select an option

  • Save teksrc/2c82f8aeb4e844cdf7aea587eb9661b6 to your computer and use it in GitHub Desktop.

Select an option

Save teksrc/2c82f8aeb4e844cdf7aea587eb9661b6 to your computer and use it in GitHub Desktop.
React app from scratch with Babel & Webpack

mkdir ex-project cd ex-project npm init -y mkdir src touch webpack.config.js .babelrc

cd src touch index.html index.js cd .. npm i react react-dom --save npm i webpack webpack-cli @babel/core babel-loader @babel/preset-env @babel/preset-react html-webpack-plugin html-loader webpack-dev-server --save-dev

in package.json:

"scripts": {
  "start": "webpack-dev-server --open --mode development",
  "build": "webpack --mode production",
  "test": "echo \"Error: no test specified\" && exit 1"
},

in .babelrc:

{
    "presets": ["@babel/preset-env", "@babel/preset-react"]
}

in webpack.config.js:

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

make index.html have html code with a div in the body given an id name of "root" ensure package.json main is index.js

cd src touch App.js

in index.js is where we import our App component and where we inject it into our index.html through React DOM

in App.js create a App component that says HI in the render method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment