Giter Site home page Giter Site logo

gaearon / react-redux-starter-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dvdzkwsk/react-redux-starter-kit

9.0 2.0 4.0 178 KB

Get started with React, Redux, React-Router, and Koa!

License: MIT License

JavaScript 97.52% CSS 1.04% HTML 1.44%

react-redux-starter-kit's Introduction

React Redux Starter Kit

Build Status dependencies

Starter kit to get you up and running with a bunch of awesome new technologies. This boilerplate provides server-side rendering of your routes (by way of Koa and react-router), and the sample application gives you a quick demo of Redux. All of this sits on top of a configurable, feature-rich Webpack build system that's already setup to provide unit testing, linting, hot-reloading, sass-loading with css-extraction, and a whole lot more. Check out the full feature list below!

Redux, React-Router, and React are constantly releasing new API changes. If you'd like to help keep this boilerplate up to date, please check out the current TODO list in ~/docs/todo or create a new issue if you think this repo is missing something!

Table of Contents

  1. Requirements
  2. Features
  3. Usage
  4. Webpack
  5. Styles
  6. Testing
  7. Utilities
  8. Deployment
  9. Troubleshooting

Requirements

Node ^0.12.0 or io.js ^2.0.0.

Features

  • React
  • react-router (1.0.0-beta)
  • Redux (1.0.0-beta)
    • redux-devtools (enabled with --debug flag)
    • react-redux
  • Koa
  • Immutable.js
  • Karma
    • Mocha w/ Chai
    • PhantomJS
  • Webpack
    • Separate server and client bundles
      • Client bundle splits app code from vendor dependencies
    • webpack-dev-server
    • react-hot-loader
    • sass-loader
      • CSS extraction in production mode
    • babel w/ babel-runtime
    • eslint-loader
      • Configured to fail production builds on error
    • Pre-configured aliases and globals
    • Easy per-environment configuration

NOTE: Bootstrap is loaded from its CDN for the sole purposes of making the example not look hideous. I didn't want to actually include it as an application dependency, so if you wish to remove it just delete its <link> tag in ~/src/index.html.

Usage

npm run compile

Runs the Webpack build system with your current NODE_ENV and compiles the application to disk (~/dist).

NOTE: I'm still searching for a good solution for when to run the server entry point bundler, since it doesn't make much sense to run webpack on it when you're just using the dev server. As a result (only for the time being) if you wish to run the Koa server you'll need to run compile with NODE_ENV=production first, since that's the only time the server bundle gets compiled.

npm run test

Runs all client-side tests for the application. In development mode this will run in watch mode and re-run individual test files when they change; in production mode a failing test will fail your build.

npm run test:unit

Similar to npm run test, but only runs unit tests.

npm run test:server

Runs the small test suite in ~/server/scripts/test.js. This will ideally be expanded in the future to instead act as an entry point similar to what exists for client-side tests.

npm run deploy

Helper script to run tests and then, on success, compile your application. Server tests that rely on the compiled server bundle will be run after compilation finishes.

npm run dev

Runs the webpack build system just like in compile but enables HMR and react hot-loader. The webpack dev server can be found at localhost:3000.

npm run dev:debug

Same as npm run dev but enables --debug flag automatically.

npm run dev:quiet

Same as npm run dev but disables verbose debugging information.

npm run server:start

Kicks off the Koa server (defaults to localhost:4000).

Configuration

Basic project configuration can be found in ~/config/index.js. Here you'll be able to redefine your src and dist directories, as well as tweak what ports Webpack and WebpackDevServer run on.

Webpack

Configuration

Webpack bundles are separated into sub-folders that define configurations for their respective bundle (e.g. ~/build/webpack/client and ~/build/webpack-server). A default webpack configuration is provided in ~/build/webpack/make-config, which exports a function that will merge a config object on top of that default configuration.

Bundle-specific configurations can further this customizability by implementing environment-specific configurations. Check out ~/build/webpack/client/_development for an example. You can configure which bundles are used and when in ~/webpack.config.js.

Vendor Bundle

You can redefine which packages to treat as vendor dependencies by editing VENDOR_DEPENDENCIES in ~/config/index.js. These default to

VENDOR_DEPENDENCIES : [
  'immutable',
  'react',
  'react-redux',
  'react-router',
  'redux'
]

Aliases

As mentioned in features, the default Webpack configuration provides some globals and aliases to make your life easier. These can be used as such:

import MyComponent from '../../components/my-component'; // without alias
import MyComponent from 'components/my-component'; // with alias

  // Available aliases:
  action-creators  => '~/src/action-creators'
  actions     => '~/src/actions'
  components  => '~/src/components'
  constants   => '~/src/constants'
  containers  => '~/src/containers'
  dispatchers => '~/src/dispatchers'
  layouts     => '~/src/layouts'
  models      => '~/src/models'
  reducers    => '~/src/reducers'
  routes      => '~/src/routes'
  services    => '~/src/services'
  stores      => '~/src/stores'
  styles      => '~/src/styles'
  utils       => '~/src/utils'
  views       => '~/src/views'

Globals

__DEV__

True when process.env.NODE_ENV is development

__PROD__

True when process.env.NODE_ENV is production

__DEBUG__

True when the compiler is run with --debug (any environment).

__CLIENT__

True when the client bundler is running.

__SERVER__

True when the server bundler is running.

Styles

All .scss imports will be run through the sass-loader, extracted during production builds, and ignored during server builds. If you're requiring styles from a base styles directory (useful for generic, app-wide styles) in your JS, you can make use of the styles alias, e.g.:

// ~/src/components/some/nested/component/index.jsx
import `styles/core.scss`;

Furthermore, this styles directory is aliased for sass imports, which further eliminates manual directory traversing. An example nested .scss file:

// current path: ~/src/styles/some/nested/style.scss
// what used to be this:
@import '../../base';

// can now be this:
@import 'base';

Testing

To add a unit test, simply create .spec.js file anywhere in ~/src. The entry point for Karma uses webpack's custom require to load all these files, and both Mocha and Chai will be available to you within your test without the need to import them.

Utilities

This boilerplate comes with two simple utilities (thanks to StevenLangbroek) to help speed up your Redux development process. In ~/src/utils you'll find exports for createConstants and createReducer. The former is pretty much an even lazier keyMirror, so if you really hate typing out those constants you may want to give it a shot. Check it out:

import { createConstants } from 'utils';

export default createConstants(
  'TODO_CREATE',
  'TODO_DESTROY',
  'TODO_TOGGLE_COMPLETE'
);

The other utility, create-reducer, is designed to expedite creating reducers when they're defined via an object map rather than switch statements. As an example, what once looked like this:

import { TODO_CREATE } from 'constants/todo';

const initialState = [];
const handlers = {
  [TODO_CREATE] : (state, payload) => { ... }
};

export default function todo (state = initialState, action) {
  const handler = handlers[action.type];

  return handler ? handler(state, action.payload) : state;
}

Can now look like this:

import { TODO_CREATE } from 'constants/todo';
import { createReducer } from 'utils';

const initialState = [];

export default createReducer(initialState, {
  [TODO_CREATE] : (state, payload) => { ... }
});

Deployment

Dokku Requirements

  • Add io.js as a buildpack:
    • In ~/ENV append: export BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-nodejs

Troubleshooting

--debug isn't working

If you're using one of the pre-configured npm scripts, make sure you follow npm's syntax:

npm run [command] [-- <args>]

As an example, npm run compile would look like this:

npm run compile -- --debug

react-redux-starter-kit's People

Contributors

stevenlangbroek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.