Setting up Gulp
Last updated
Was this helpful?
Last updated
Was this helpful?
We're going to create a React starter repo that will allow you to setup full-stack React applications with Gulp, Browserify, and ES6.
Create a new repository in Github and clone it to your computer. You can call it react-starter-basic
. The link to the finished version is below, but we'll be building it from scratch.
Install the gulp
command line tool:
After cloning your repo to your local computer, setup npm
by running:
Then, install some dependencies we'll need for our workflow.
--save-dev
will save these dependencies, but only install them on development environments
gulp
will allow us to run tasks needed to copy and transpile code
browserify
will allow us to use require()
in front-end files.
babelify
and babel-preset-react
will allow us to transpile ES6/JSX to ES5
vinyl-source-stream
will allow us to convert the stream coming from browserify
so we can use it with gulp
react
and react-dom
are the dependencies needed to create React apps
Once these dependencies are set up, we'll also want to create a configuration file so Babel knows to use the preset for React.
Create a file called .babelrc
in the root of the project directory, and create the following preset in the file:
babel.rc
This preset will tell Babel to transpile using the React preset. Note that this is IMPORTANT when working with Babel, and the presets may vary depending on the type of application being developed.
In order to use gulp
, we need to create a file called a gulpfile. This file will contain tasks that can take care of importing modules, transpiling code, and copying files. Specifically, we'll be doing the following in our gulpfile.
Setting up browserify
to import modules
Transform code using babelify
Copying JS files to a different directory (separating our source React files from the transpiled file)
While beyond the scope of this lesson, Gulp can also be setup to minify code, lint code, and utilize additional preprocessors like SASS and LESS. See the resources for more information on setting up these tasks.
A task in gulp is essentially a named function that can be invoked via the gulp
command line tool. In the root of the project, let's make a gulpfile. Gulp will look for a file called gulpfile.js
, so we'll name the file gulpfile.js
.
In gulpfile.js:
This is a file with one task called build
. It can be run in the command line by running gulp build
.
By default, Gulp looks for a task called default
and runs it automatically if no task if specified. Let's change the file to run the build
task automatically.
Now we can type gulp
in the command line and Gulp will automatically run the build
task. Awesome!
Let's make a few more modifications to the gulpfile
in order to make it take care of our React files.
In gulpfile.js:
Yikes, what did we just do?! Let's break it down.
At the top of the file, we required the additional modules needed to process our React files
The build
task is setup to do the following
Incorporate browserify
so we can use require()
in front-end files
Everything needed should belong in src/app.jsx
Transform ES6/JSX into ES5 code
Bundle the files together and create a new file called app.js
Write the file to a new location (dist/app.js
)
In order to test this gulpfile.js
, let's make sure that we have a sample React app that exists. Create the following folder structure in the project:
Now, let's create a React application inside of src/app.jsx
In src/app.jsx:
Now, try running the gulpfile
by running in the command line:
If successful, the build
task should run and create a file called dist/app.js
in your project. This is the final file that can be included in a project. To test, try making a HTML file and include dist/app.js
on the page. See if your React project works.
Browserify allowed us to require react
and react-dom
into our src/app.jsx
file. Let's take this a step further by organizing all of our components into a separate file, then using require()
to use them.
Inside of the src
folder, create a new folder called components
. Also, create a file called header.jsx
inside the components
folder. Your folder structure should look like this:
Inside of src/components/header.jsx
, create a new React component. We'll make this Header component display name
from props
.
Now in order to use the Header component in src/app.jsx
, let's use require()
and render the component.
In src/app.jsx:
Try testing this file by running gulp
and opening the HTML created previously. Congrats, you've just used gulp
and browserify
to create a React app!