Application starter template.
Intended to be used for implementing single page applications with possibility to have server side rendering.
Following libraries are used:
- ReactJS
- RxJS
- ExpressJS
- Router1 + Router1 React
- rx-react-container
And tools:
- Webpack - to bundle all the things
- BabelJS - for es2015 syntax
- Node SASS
- PostCSS with autoprefixer
- Nodemon
- Jest
Actually it is not much different from all other boilerplates - same tools and similar config.
But there is one thing that makes it different - there is helper script(dev-server.js
),
to keep server side and client side parts reloads in sync.
It a wrapper around webpack compiler, webpack-serve and a nodemon, it adds proxy into webpack-serve that ensures that all reloads from browser are loading data from recent server-side bundle - it will wait before request processing if new build is in progress or when server is not ready yet.
In result it removes some frustration, especially when changing code shared between server and client parts.
- Check your Node.js version - it should be version 8 or later
- Install dependencies using npm (or yarn)
- start server app, with automatic recompile and reload when something changes
npm run dev
- open this url in your browser:
http://localhost:8080/
To customize host and ports used by application - use environment variables:
DEV_SERVER_PORT
- port used by dev server,8080
by defaultDEV_SERVER_WS_PORT
- port used for webpack notifications websocket,8081
by defaultDEV_SERVER_HOST
- host where dev server is running,localhost
by defaultAPP_SERVER_PORT
- port user by application,8082
by default. Is passed asPORT
environment variable into application.
if you have different application host and port different than above - be sure to specify them in environment
Also you can enable some other things for dev-server by environment variables
HOT=1
to enable hot reload for client sideSSR=1
to enable server-side rendering in dev environment(disabled by default because typically you need test things on client side first)
For setting those variables - you can create .env
file at project root.
Configuration is not complete, and right now is not in usable state by default.
To fix it you need to add module.hot.accept
code. See https://github.com/gaearon/react-hot-loader#getting-started for details(step 4).
Build sources:
npm run build
Start server:
node server
Set env variable SSR=1
to enable server side rendering.
As for dev-server - you can create .env
file at project root.
build
build stats, serverpublic
static assetspublic/_assets
webpack build resultssrc
application sourcessrc/client
browser specific sourcessrc/server
server specific sourcestools
tooling and config scripts (webpack, dev server)
After a production build you may want to visualize your modules and chunks tree.
Use the webpack.github.io/analyse with the file at build/stats.json
.
Also I like using webpack-bundle-analyzer - recommend trying it too.
Many file types are preconfigured, but not every loader is installed. If you get an error like Cannot find module "xxx-loader"
, you'll need to install the loader with npm install xxx-loader --save
and restart the compilation.