Hello guys!
In my previous article, CRUD Opration with React and node,Mongodb, we discussed how to perform insert update delete and select data using ReactJS and Node,Express and Mongodb.
In this article, we’ll learn how to create react applications using Babel and Webpack. So let me first give you a brief indrocution about Babel and Webpack .
Webpack
A Webpack is a module bundler. Webpack takes modules with dependencies and generates static assets representing those modules..
Babel
Babel is a Javascript compiler, best know for its ability to turn it into ES6 (the next version of javascript) .
EcmaScript6
A relatively new Javasript standard, EcmaScript 6 is a deprecated name for the EcmaScript 2015 language specification. ES2015 is the 6th version of EcmaScript, thus why it was previously referred to as ES6. For reasons best known to themselves, those responsible for defining the language standard renamed it to ES2015 with the final version of the v6 spec.
Let us start to create application
The first step is to Create a new folder, ReactWithBabelWebpack, and right click on Folder and Git Bash here or navigate to folder and run npm init command. After that package.json file is created in our application.
The second step is we need to add react and react-dom dependencies in our application so we add dependencies using the below given command.
- npm install --save react
- npm install --save react-dom
The third step is add webpacks development dependencies. The main advantage of webpack is module bundler. All our js and jsx files is in one bundle of js files.
- npm install --save-dev webpack
- npm install webpack-dev-server
The forth step is to add babel dependencies for Javascript compiler.
- npm install --save-dev babel-loader
- npm install --save-dev babel-core
- npm install --save-dev babel-preset-es2015
- npm install --save-dev babel-preset-react
The next step is to create index.html file,
- <!doctype html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>React With Babel and Webpack</title>
-
- </head>
- <body>
- <div id="div"></div>
-
- <script src="bundle.js"></script>
- </body>
- </html>
main.js file
-
- import HelloWorld from './helloWorld.jsx';
helloWorld.jsx file
- import React from 'react';
- import ReactDOM from 'react-dom';
-
- class HelloWorld extends React.Component {
- render() {
- return (
- <div>
- <h1>Hello, world!</h1>
- <br/>
- <h2>Welcome to React App With Babel and Webpack </h2>
-
- </div>
- )
- }
- }
-
- ReactDOM.render(<HelloWorld/>, document.getElementById('div'));
webpack.config.js file
- var path = require('path');
- var webpack = require('webpack');
-
- module.exports = {
- entry: './main.js',
- output: { path: __dirname, filename: 'bundle.js' },
- module: {
- loaders: [
- {
- test: /.jsx?$/,
- loader: 'babel-loader',
- exclude: /node_modules/,
- query: {
- presets: ['es2015', 'react']
- }
- }
- ]
- },
- };
Task is almost finished. We run command webpack-dev-server for start application on server localhost:8080
We see the Ouput on brower like this .
Summary
This article describes how to create react application with webpack and Babel. I hope you enjoyed this article.