To show how to create a React standardized project
- Initial
package.json
setup and scripts. - Webpack and
.babelrc
configurations. - How to think about React components.
- The app is currently running here on Heroku.
$ npm install
to install all dependencies$ npm run build:semantic
to install the semantic bundle- You will need your own The Movie DB api key
- Set the environment variable
MOVIE_DB_API_KEY=***YOUR_API_KEY***
$ npm run dev
to run webpack and the node server
- Setup dev vs prod webpacks
- Once you've created and entered your project root folder, run
$ npm init
and follow the instructions to create apackage.json
file. - Install express
$ npm install express --save
which adds express to yourpackage.json
as a dependency and creates anode_modules
directory. Addnode_modules
to your.gitignore
. Read more here to understand npm and how to setup yourpackage.json
. - As you add more packages, make sure to run
$ npm install
if you are not adding them through the npm cli.
- Here is a decent writeup on how to configure webpack for React. webpack is an intricate module bundler and there is a great course on Pluralsight that I recommend for the basics.
$ npm install babel-core babel-loader babel-preset-es2015 babel-preset-react --save
$ npm install webpack -D
save webpack as devDependency instead of a global install so you don't run into any weird version issues.- Create both a
webpack.config.js
and awebpack.prod.config.js
, for local and prod configurations respectively. - Create a
.babelrc
file with the same config. Learn more about babel here - Comments in each of the webpack file should help explain the configuration.
- Create a
server.js
file at the root of your project. - You are serving the
index.html
file on each route, because it will hold the entry point of you application. It contains a basic HTML markup along with a div with the id App, and a script tag for yourbundle.js
file. - Given you have webpack bundling all your sources into a dist folder, you should statically serve that folder.
- I highly recommend the Code School course if you've never used Express before.
- Fantastic tutorial on React router, it explains the intricacies of routing with React far better than I can.
$ npm install -D react-router
- Create an
index.js
file in the src folder. This file will contain all the routes for you application. - Comments in the
index.js
file should help explain the routing configuration.
semantic-ui is a great development framework to create responsive websites that are cross browser compatible.
I'm using semantic-ui-react also known as Stardust, which is the reactified version of semantic. They're not completely finished with converting all of semantic, but it honestly has most of everything you could need.
$ npm install --save semantic-ui-react
using mocha as a testing framework. A
mocha.opts
file under the test folder contains command line arguments as options for mocha tests.
enzyme is test utility for React. It's very expressive and allows for both shallow and full rendering.
$ npm install -D jsdom
is a necessary dependency for testing within a DOM.$ npm install -D mocha sinon expect react-addons-test-utils enzyme
Create the test folder with themocha.opts
file. You can choose whatever reporter you want. I like nyan because it's easy to see when there are warnings.- Create a
dom.js
file which is required in themocha.opts
file which means that it will be available for all tests.
this line says to include this file across all tests.
--require test/utils/dom.js
this line runs the jsx compiler across all test files
--compilers jsx?:babel-core/register
this includes all sub directories for the test
--recursive
this chooses the reporter
--reporter nyan
- Setup a test npm script in the
package.json
file. Currently run tests with$ npm run test
. - I recommend to mirror your test folder structure with your src folder structure, so it's easy to go back and forth between your test and source code.
Using Airbnb lint configuration follow the instructions here to download the correct packages.
Read more here to learn about .eslintrc config files.
If you use Atom, download the following packages in order to get real-time linting of JavaScript, ES6 and JSX code in the editor. In the root folder, run the following commands to get the package:
$ apm install linter
Once that finishes successfully, download the following package:
$ apm install linter-eslint
You can also run the linting task through the command line. However, it is best to get real-time feedback as you're developing. The lint:broswer
script is in the package.json
file. To run the linting task from the command line, run the following command:
$ npm run lint:browser
You should run the lint command whenever you make changes to the .eslintrc file and then restart atom for the changes to take effect.