Reader small image

You're reading from  Getting Started with React

Product typeBook
Published inApr 2016
Reading LevelIntermediate
Publisher
ISBN-139781783550579
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Doel Sengupta
Doel Sengupta
author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

Manu Singhal
Manu Singhal
author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

Danillo Corvalan
Danillo Corvalan
author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan

View More author details
Right arrow

Chapter 9. Preparing Your Code for Deployment

Going through the ReactJS fundamentals and flux, we have almost approached the end of this book. After developing any application, we are left with the most crucial part of making the application available to the outside world, thus deploying your application. It's a good practice to keep the code in a source control repository such as GitHub or Bitbucket and to version control the code using Git. These help while working in a group and retrieval of any code as and when necessary. The explanation of how to set up the earlier-mentioned things is beyond the scope of this book, but there are a plenty of resources available for the same.

In this chapter, we will be exploring the following topics:

  • An introduction to Webpack

  • The ways of deploying a React application using Webpack and Gulp

  • The configuration options used for browserify

  • Installing a simple web server

An introduction to Webpack


Webpack is a module bundler, which is used to deploy JavaScript-based applications. It takes the input as modules with dependencies and then outputs these into static assets.

From the Webpack documentation site (https://webpack.github.io/docs/what-is-webpack.html#how-is-webpack-different), the following image explains the same.

Building a simple React application

As in the earlier chapters, let's build a simple React-based application with which we will be integrating the Webpack and deploy thereafter.

Install the packages vis npm from a terminal as:

sudo npm install babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-2
npm -g install httpster

Note

httpster: It is a simple http server to run the static content. In chrome browser, the index.html file sometimes doesn't render due to the X-origin error. Hence, running this webserver from your application directory will be easier to test your application in Chrome. Just run the command httpster.

By default...

Advantages of Webpack


Along the various advantages of using Webpack, as yet another bundler, these are the most important ones:

  1. Code splitting: Based on the code size, it helps modularize the code chunks of code and loads these modules as and when needed. You can define the split points on your code, based on which the code chunks will be used. Thus, it helps in faster page load and performance improvement.

  2. Loaders: As in the earlier-mentioned image, in the left-hand side, you can see that there are various other formats such as coffescripts/jsx instead of JavaScript and .less instead of .css. Thus, these loaders (npm packages) are used to convert these other formats into the accepted standardized formats, which makes the life of the developers much easy to code into any format they want. In React-based applications that we were seeing earlier, JSX formats are widely used. Hence, these loaders will come handy.

  3. Clever parsing: It helps to parse most of the third-party library and handles the...

Introduction to Gulp


Now that we have seen a module bundler, let's see what Gulp will do for us. Gulp is a build tool for compiling and compressing JS/assets, and it does live reload on the browsers. Gulp file is basically a file with the set of instructions, which Gulp should do. The file can have a default task or several other tasks to be called from one another.

Installing Gulp and creating Gulp file

Let's install gulp and configure it with our existing application:

npm install -g gulp (for globally installing gulp)
npm install gulp –save-dev (as a developer dependancy)

Next, create a simple gulpfile.js file at the root of your app directory:

var gulp = require('gulp');

gulp.task('default', function() {
  // tasks goes here
});

Let's execute the command from terminal:

A console screenshot, after the gulp command is executed

Then, we are installing some other packages for Gulp-related tasks. We are adding these in our package.json file and running npm install, in order to install these:

Package...

Summary


In this chapter, we came to know how we can deploy our React applications using Webpack and the way Gulp eases our life by automating tasks, minifying our assets (JS, JSX, CSS, SASS, images, and so on), watching any changes on these files and live-reload built in the browser. In Chapter 10, What's Next, we will be exploring some advanced concepts of ReactJS.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with React
Published in: Apr 2016Publisher: ISBN-13: 9781783550579
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan