-
Added a local configuration for the client application in src/config
- Copy src/config/_local.sample.js to src/config/_local.js and override any configuration options that you see in the src/config/_base.js
-
Due to the TSheets API not supporting CORS, added CORS support via creating a CORS proxy with nginx
- Installed nginx on Mac OS X using homebrew:
brew tap homebrew/nginx
brew install nginx-full --with-headers-more-module
- Most of the instructions for this method were adapted from nginx as a CORS-enabled HTTPS proxy
- Edit
/usr/local/etc/nginx/nginx.conf
to add the proxy, roughly:
- Installed nginx on Mac OS X using homebrew:
# Make sure you specify all the methods and Headers
# you send with any request!
more_set_headers 'Access-Control-Allow-Origin: *';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization';
location / {
# Handle a CORS preflight OPTIONS request
# without passing it through to the proxied server
if ($request_method = OPTIONS ) {
add_header Content-Length 0;
add_header Content-Type text/plain;
return 204;
}
proxy_pass https://rest.tsheets.com/;
}
- Then whatever port your server is on, go into your
src/config/_local.js
and override thetsheets_apiroot
to be your localhost, something like:http://localhost:8080/api/v1
. - Start nginx by doing
nginx
(sudo if necessary) - Stop nginx doing
nginx -s stop
Woohoo! If you'd like to try it out, you're welcome to build directly from the master branch. However, if troubleshooting issues with Babel isn't quite your thing, just pull the stable v0.18.0 release and continue on your way with Babel 5.
After installing npm dependencies, open
.eslintrc
, change thesemi
rule fromnever
toalways
, and then runnpm run lint:fix
-- Easy as that! Alternatively, use the same npm script after installing and extending your preferred ESLint configuration; it's easy to customize the project's code style to suit your team's needs. See, we can coexist peacefully.
This starter kit is designed to get you up and running with a bunch of awesome new front-end technologies, all on top of a configurable, feature-rich webpack build system that's already setup to provide hot reloading, CSS modules with Sass support, unit testing, code coverage reports, bundle splitting, and a whole lot more.
The primary goal of this project is to remain as unopinionated as possible. Its purpose is not to dictate your project structure or to demonstrate a complete sample application, but to provide a set of tools intended to make front-end development robust, easy, and, most importantly, fun. Check out the full feature list below!
- Requirements
- Features
- Getting Started
- Usage
- Structure
- Webpack
- Server
- Styles
- Testing
- Deployment
- Troubleshooting
Node ^5.0.0
- React (
^0.14.0
)- Includes react-addons-test-utils (
^0.14.0
)
- Includes react-addons-test-utils (
- Redux (
^3.0.0
)- react-redux (
^4.0.0
) - redux-devtools
- use
npm run dev:nw
to display them in a separate window.
- use
- redux-thunk middleware
- react-redux (
- react-router (
^1.0.0
) - redux-simple-router (
^1.0.0
) - Webpack
- CSS modules!
- sass-loader
- postcss-loader with cssnano for style autoprefixing and minification
- Bundle splitting for app and vendor dependencies
- CSS extraction during production builds
- Loaders for fonts and images
- Express
- webpack-dev-middleware
- webpack-hot-middleware
- Karma
- Mocha w/ chai, sinon-chai, and chai-as-promised
- PhantomJS
- Code coverage reports
- Babel (
^6.3.0
)- babel-plugin-transform-runtime so transforms aren't inlined
- babel-preset-react-hmre for:
- react-transform-hmr (HMR for React components)
- redbox-react (visible error reporting for React components)
- ESLint
- Uses Standard Style by default, but you're welcome to change this!
- Includes separate test-specific
.eslintrc
to work with Mocha and Chai
Just clone the repo and install the necessary node modules:
$ git clone https://github.com/davezuko/react-redux-starter-kit.git
$ cd react-redux-starter-kit
$ npm install # Install Node modules listed in ./package.json (may take a while the first time)
$ npm start # Compile and launch
Before delving into the descriptions of each available npm script, here's a brief summary of the three which will most likely be your bread and butter:
- Doing live development? Use
npm start
to spin up the dev server. - Compiling the application to disk? Use
npm run compile
. - Deploying to an environment?
npm run deploy
can help with that.
NOTE: This package makes use of debug to improve your debugging experience. For convenience, all of messages are prefixed with app:*
. If you'd like to to change what debug statements are displayed, you can override the DEBUG
environment variable to app:*
via the CLI (e.g. DEBUG=app:* npm start
) or update the ~/.env
file.
Great, now that introductions have been made here's everything in full detail:
npm start
- Spins up express server to serve your app atlocalhost:3000
. HMR will be enabled in development.npm run compile
- Compiles the application to disk (~/dist
by default).npm run dev:nw
- Same asnpm start
, but opens the redux devtools in a new window.npm run dev:no-debug
- Same asnpm start
but disables redux devtools.npm run test
- Runs unit tests with Karma and generates a coverage report.npm run test:dev
- Runs Karma and watches for changes to re-run tests; does not generate coverage reports.npm run deploy
- Runs linter, tests, and then, on success, compiles your application to disk.npm run lint
- Lint all.js
files.npm run lint:fix
- Lint and fix all.js
files. Read more on this.
NOTE: Deploying to a specific environment? Make sure to specify your target NODE_ENV
so webpack will use the correct configuration. For example: NODE_ENV=production npm run compile
will compile your application with ~/build/webpack/_production.js
.
Basic project configuration can be found in ~/config/_base.js
. Here you'll be able to redefine your src
and dist
directories, adjust compilation settings, tweak your vendor dependencies, and more. For the most part, you should be able to make changes in here without ever having to touch the webpack build configuration. If you need environment-specific overrides, create a file with the name of target NODE_ENV
prefixed by an _
in ~/config
(see ~/config/_production.js
for an example).
Common configuration options:
dir_src
- application source code base pathdir_dist
- path to build compiled application toserver_host
- hostname for the express serverserver_port
- port for the express servercompiler_css_modules
- whether or not to enable CSS modulescompiler_source_maps
- whether or not to generate source mapscompiler_vendor
- packages to separate into to the vendor bundle
The folder structure provided is only meant to serve as a guide, it is by no means prescriptive. It is something that has worked very well for me and my team, but use only what makes sense to you.
.
├── bin # Build/Start scripts
├── build # All build-related configuration
│ └── webpack # Environment-specific configuration files for webpack
├── config # Project configuration settings
├── server # Express application (uses webpack middleware)
│ └── main.js # Server application entry point
├── src # Application source code
│ ├── components # Generic React Components (generally Dumb components)
│ ├── containers # Components that provide context (e.g. Redux Provider)
│ ├── layouts # Components that dictate major page structure
│ ├── redux # Redux-specific pieces
│ │ ├── modules # Collections of reducers/constants/actions
│ │ └── utils # Redux-specific helpers
│ ├── routes # Application route definitions
│ ├── static # Static assets (not imported anywhere in source code)
│ ├── styles # Application-wide styles (generally settings)
│ ├── views # Components that live at a route
│ └── main.js # Application bootstrap and rendering
└── tests # Unit tests
TL;DR: They're all components.
This distinction may not be important for you, but as an explanation: A Layout is something that describes an entire page structure, such as a fixed navigation, viewport, sidebar, and footer. Most applications will probably only have one layout, but keeping these components separate makes their intent clear. Views are components that live at routes, and are generally rendered within a Layout. What this ends up meaning is that, with this structure, nearly everything inside of Components ends up being a dumb component.
The webpack compiler configuration is located in ~/build/webpack
. Here you'll find configurations for each environment; development
and production
exist out of the box.
Note: There has been a conscious decision to keep development-specific configuration (such as hot-reloading) out of .babelrc
. By doing this, it's possible to create cleaner development builds (such as for teams that have a dev
-> stage
-> production
workflow) that don't, for example, constantly poll for HMR updates.
So why not just disable HMR? Well, as a further explanation, enabling react-transform-hmr
in .babelrc
but building the project without HMR enabled (think of running tests with NODE_ENV=development
but without a dev server) causes errors to be thrown, so this decision also alleviates that issue.
You can redefine which packages to bundle separately by modifying compiler_vendor
in ~/config/_base.js
. These default to:
[
'history',
'react',
'react-redux',
'react-router',
'redux-simple-router',
'redux'
]
Webpack is configured to make use of resolve.root, which lets you import local packages as if you were traversing from the root of your ~/src
directory. Here's an example:
// current file: ~/src/views/some/nested/View.js
// What used to be this:
import SomeComponent from '../../../components/SomeComponent'
// Can now be this:
import SomeComponent from 'components/SomeComponent' // Hooray!
These are global variables available to you anywhere in your source code. If you wish to modify them, they can be found as the globals
key in ~/config/index.js
.
process.env.NODE_ENV
- the activeNODE_ENV
when the build started__DEV__
- True whenprocess.env.NODE_ENV
isdevelopment
__PROD__
- True whenprocess.env.NODE_ENV
isproduction
Additionally, the following variables are globally available by automatic imports (see section "Globally available imports" further below):
React
(imported from'react'
)ReactDOM
(imported from'react-dom'
)
Webpack is configured to use ProvidePlugin, which lets you use commonly used imports without explicitly writing an import statement, reducing boilerplate. To add more automatic imports, add them to compiler_globals
in ~/config/_base
. Additionally, add them to the globals object in your .eslintrc
so you don't encounter misleading linter errors.
This starter kit comes packaged with an Express server. It's important to note that the sole purpose of this server is to provide webpack-dev-middleware
and webpack-hot-middleware
for hot module replacement. Using a custom Express app in place of webpack-dev-server will hopefully make it easier for users to extend the starter kit to include functionality such as back-end API's, isomorphic/universal rendering, and more -- all without bloating the base boilerplate. Because of this, it should be noted that the provided server is not production-ready. If you're deploying to production, take a look at the deployment section.
Both .scss
and .css
file extensions are supported out of the box and are configured to use CSS Modules. After being imported, styles will be processed with PostCSS for minification and autoprefixing, and will be extracted to a .css
file during production builds.
NOTE: If you're importing styles from a base styles directory (useful for generic, app-wide styles), you can make use of the styles
alias, e.g.:
// current file: ~/src/components/some/nested/component/index.jsx
import 'styles/core.scss' // this imports ~/src/styles/core.scss
Furthermore, this styles
directory is aliased for sass imports, which further eliminates manual directory traversing; this is especially useful for importing variables/mixins.
Here's an example:
// current file: ~/src/styles/some/nested/style.scss
// what used to be this (where base is ~/src/styles/_base.scss):
@import '../../base';
// can now be this:
@import 'base';
To add a unit test, simply create a .spec.js
file anywhere in ~/tests
. Karma will pick up on these files automatically, and Mocha and Chai will be available within your test without the need to import them.
Coverage reports will be compiled to ~/coverage
by default. If you wish to change what reporters are used and where reports are compiled, you can do so by modifying coverage_reporters
in ~/config/_base.js
.
Out of the box, this starter kit is deployable by serving the ~/dist
folder generated by npm run compile
(make sure to specify your target NODE_ENV
as well). This project does not concern itself with the details of server-side rendering or API structure, since that demands an opinionated structure that makes it difficult to extend the starter kit. However, if you do need help with more advanced deployment strategies, here are a few tips:
If you are serving the application via a web server such as nginx, make sure to direct incoming routes to the root ~/dist/index.html
file and let react-router take care of the rest. The Express server that comes with the starter kit is able to be extended to serve as an API or whatever else you need, but that's entirely up to you.
Have more questions? Feel free to submit an issue or join the Gitter chat!
This is most likely because the new window has been blocked by your popup blocker, so make sure it's disabled before trying again.
Reference: issue 110
While this is common to any sizable application, it's worth noting for those who may not know: if you happen to notice higher CPU usage in your editor after compiling the application, you may need to tell your editor not to process the dist folder. For example, in Sublime you can add:
"folder_exclude_patterns": [".svn", ".git", ".hg", "CVS", "node_modules", "dist"]