The intention of this project is to provide a standard and modern starter kit for web application development using React within the Royal Netherlands Meteorological Institute (KNMI). It serves as a stepping stone with integrated development, deployment, testing.
- ESLint
- eslint-config-google
- eslint-plugin-react
- Make sure you have installed at least the following packages:
- PackageControl
- Babel
- EditorConfig
- SublimeLinter
- SublimeLinter-contrib-eslint
- Set syntax correctly, since linting is coupled with syntax selection
- For JavaScript: 1. Open a file with .js extension 1. Choose View > Syntax > Open all with current extension as... > Babel > Javascript (Babel)
- For JSX 1. Open a file with .jsx extension 1. Choose View > Syntax > Open all with current extension as... > Babel > Javascript (Babel)
- For JSON:
1. Open a file with .json extension
1. Choose View > Syntax > Open all with current extension as... > Babel > JSON
1. Map JSON syntax viewing to the right linting mechanism
- Choose Preferences > Package Settings > SublimeLinter > Settings - User
- Find the property `"syntax_map"
- Check if
"javascript (babel)": "javascript"
is present - Add
"json": "javascript"
$ git clone https://github.com/maartenlterpstra/KNMI-React-Redux-Starter-Kit.git <my-project-name>
$ cd <my-project-name>
$ npm install
While developing, you will probably rely mostly on npm start
; however, there are additional scripts at your disposal:
npm run <script> |
Description |
---|---|
start |
Serves your app at localhost:3000 . HMR will be enabled in development. |
compile |
Compiles the application to disk (~/dist by default). |
dev |
Same as npm start , but enables nodemon for the server as well. |
test |
Runs unit tests with Mocha/NYC and generates a coverage report. |
test:dev |
Runs Mocha/NYC and watches for changes to re-run tests; does not generate coverage reports. |
deploy |
Runs linter, tests, and then, on success, compiles your application to disk. |
deploy:dev |
Same as deploy but overrides NODE_ENV to "development". |
deploy:prod |
Same as deploy but overrides NODE_ENV to "production". |
lint |
Lint all .js files. |
lint:fix |
Lint and fix all .js files. Read more on this. |
The application structure presented in this boilerplate is fractal, where functionality is grouped primarily by feature rather than file type. Please note, however, that this structure is only meant to serve as a guide, it is by no means prescriptive. That said, it aims to represent generally accepted guidelines and patterns for building scalable applications. If you wish to read more about this pattern, please check out this awesome writeup by Justin Greenberg.
.
├── bin # Build/Start scripts
├── config # Project and build configurations
├── coverage # A detailed HTML unit test coverage report
├── public # Static public assets (not imported anywhere in source code)
├── server # Express application that provides webpack middleware
│ └── main.js # Server application entry point
├── src # Application source code
│ ├── index.html # Main HTML page container for app
│ ├── main.js # Application bootstrap and rendering
│ ├── actions # Action creators for different Redux reducers
│ ├── components # Global Reusable Presentational Components
│ ├── containers # Global Reusable Container Components
│ ├── layouts # Components that dictate major page structure
│ ├── reducers # Global reducers for Redux State
│ ├── routes # Main route definitions and async split points
│ │ ├── index.js # Bootstrap main application routes with store
│ │ ├── HomeRoute # Fractal route
│ │ │ ├── index.js # Route definitions and async split points
│ │ │ ├── assets # Assets required to render components
│ │ │ ├── components # Presentational React Components
│ │ │ └── routes ** # Fractal sub-routes (** optional)
│ │ └── CounterRoute # Fractal route
│ │ ├── index.js # Counter route definition
│ │ ├── container # Connect components to actions and store
│ │ ├── modules # Collections of reducers/constants/actions
│ │ └── routes ** # Fractal sub-routes (** optional)
│ ├── store # Redux-specific pieces
│ │ ├── createStore.js # Create and instrument redux store
│ │ └── reducers.js # Reducer registry and injection
│ └── styles # Application-wide styles (generally settings)
└── test # Access point for Mocha (test bundler and settings)
We recommend using the Redux DevTools Chrome Extension. Using the chrome extension allows your monitors to run on a separate thread and affords better performance and functionality. It comes with several of the most popular monitors, is easy to configure, filters actions, and doesn’t require installing any packages.
We use react-router
route definitions (<route>/index.js
) to define units of logic within our application. See the application structure section for more information.
To add a unit test, simply create a .spec.js
next to the file you want to test. For example, if you want to test MainComponent.jsx
, you create a test file called MainComponent.spec.js
. These files will be picked up automatically and will have Mocha/Chai/Enzyme available to 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 .nycrc
.
Out of the box, this starter kit is deployable by serving the ~/dist
folder generated by npm run deploy
(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. If you are unsure of how to do this, you might find this documentation helpful. 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.
Default project configuration can be found in ~/config/project.config.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 actual webpack build configuration.
If you need environment-specific overrides (useful for dynamically setting API endpoints, for example), you can edit ~/config/environments.config.js
and define overrides on a per-NODE_ENV basis. There are examples for both development
and production
, so use those as guidelines. Here are some common configuration options:
Key | Description |
---|---|
dir_src |
application source code base path |
dir_dist |
path to build compiled application to |
server_host |
hostname for the Express server |
server_port |
port for the Express server |
compiler_devtool |
what type of source-maps to generate (set to false /null to disable) |
compiler_vendor |
packages to separate into to the vendor bundle |
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/project.config.js
. When adding new globals, make sure you also add them to ~/.eslintrc
.
Variable | Description |
---|---|
process.env.NODE_ENV |
the active NODE_ENV when the build started |
__DEV__ |
True when process.env.NODE_ENV is development |
__PROD__ |
True when process.env.NODE_ENV is production |
__TEST__ |
True when process.env.NODE_ENV is test |
Both .scss
and .css
file extensions are supported out of the box. After being imported, styles will be processed with PostCSS for minification and autoprefixing, and will be extracted to a .css
file during production builds.
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 makes it easier to extend the starter kit to include functionality such as API's, universal rendering, and more -- all without bloating the base boilerplate.