Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit f00ebd3

Browse files
author
David Zukowski
committed
refactor(server): koa -> express, deprecate use of babel on server
1 parent 15f703f commit f00ebd3

17 files changed

+119
-310
lines changed

.babelrc

-10
This file was deleted.

README.md

+14-94
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![devDependency Status](https://david-dm.org/davezuko/react-redux-starter-kit/dev-status.svg)](https://david-dm.org/davezuko/react-redux-starter-kit#info=devDependencies)
77
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
88

9-
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.
9+
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 preprocessing with Sass, unit testing, code coverage reports, bundle splitting, and more.
1010

1111
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!
1212

@@ -40,7 +40,7 @@ Finally, This project wouldn't be possible without the help of our many contribu
4040
* [react-router-redux](https://github.com/rackt/react-router-redux)
4141
* [webpack](https://github.com/webpack/webpack)
4242
* [babel](https://github.com/babel/babel)
43-
* [koa](https://github.com/koajs/koa)
43+
* [express](https://github.com/expressjs/express)
4444
* [karma](https://github.com/karma-runner/karma)
4545
* [eslint](http://eslint.org)
4646

@@ -54,38 +54,14 @@ After confirming that your development environment meets the specified [requirem
5454

5555
### Install from source
5656

57-
First, clone or download:
57+
First, clone the project:
5858

5959
```bash
60-
$ git clone https://github.com/davezuko/react-redux-starter-kit.git
61-
// or
62-
$ wget -O react-redux-starter-kit.zip https://github.com/davezuko/react-redux-starter-kit/archive/master.zip
63-
$ unzip react-redux-starter-kit.zip
64-
```
65-
66-
Then, rename to your project name and change into the directory:
67-
68-
```bash
69-
$ mv react-redux-starter-kit <my-project-name>
70-
$ cd <my-project-name>
71-
```
72-
73-
### Alternatively, install via `redux-cli`
74-
75-
If not already installed (globally):
76-
77-
```bash
78-
$ npm i redux-cli -g
79-
```
80-
81-
Then, create a new project:
82-
83-
```bash
84-
$ redux new <my-project-name>
60+
$ git clone https://github.com/davezuko/react-redux-starter-kit.git <my-project-name>
8561
$ cd <my-project-name>
8662
```
8763

88-
### Install dependencies, and check to see it works
64+
Then install dependencies and check to see it works
8965

9066
```bash
9167
$ npm install # Install project dependencies
@@ -102,7 +78,6 @@ While developing, you will probably rely mostly on `npm start`; however, there a
10278
|`start`|Serves your app at `localhost:3000`. HMR will be enabled in development.|
10379
|`compile`|Compiles the application to disk (`~/dist` by default).|
10480
|`dev`|Same as `npm start`, but enables nodemon for the server as well.|
105-
|`dev:no-debug`|Same as `npm run dev` but disables devtool instrumentation.|
10681
|`test`|Runs unit tests with Karma and generates a coverage report.|
10782
|`test:dev`|Runs Karma and watches for changes to re-run tests; does not generate coverage reports.|
10883
|`deploy`|Runs linter, tests, and then, on success, compiles your application to disk.|
@@ -122,13 +97,13 @@ The application structure presented in this boilerplate is **fractal**, where fu
12297
├── build # All build-related configuration
12398
│ └── webpack # Environment-specific configuration files for webpack
12499
├── config # Project configuration settings
125-
├── server # Koa application (uses webpack middleware)
100+
├── server # Express application that provides webpack middleware
126101
│ └── main.js # Server application entry point
127102
├── src # Application source code
128103
│ ├── index.html # Main HTML page container for app
129104
│ ├── main.js # Application bootstrap and rendering
130-
│ ├── components # Reusable Presentational Components
131-
│ ├── containers # Reusable Container Components
105+
│ ├── components # Global Reusable Presentational Components
106+
│ ├── containers # Global Reusable Container Components
132107
│ ├── layouts # Components that dictate major page structure
133108
│ ├── redux # "Ducks" location...
134109
│ │ └── modules # reducer, action, creators not part of a route
@@ -164,70 +139,17 @@ npm i --save-dev redux-devtools redux-devtools-log-monitor redux-devtools-dock-m
164139

165140
Then follow the [manual integration walkthrough](https://github.com/gaearon/redux-devtools/blob/master/docs/Walkthrough.md).
166141

167-
#### `redux-cli`
168-
169-
```bash
170-
npm install redux-cli --save-dev
171-
```
172-
173142
### Routing
174143
We use `react-router` [route definitions](https://github.com/reactjs/react-router/blob/master/docs/API.md#plainroute) (`<route>/index.js`) to define units of logic within our application. See the [application structure](#application-structure) section for more information.
175144

176145
## Testing
177-
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. If you are using `redux-cli`, test files should automatically be generated when you create a component or redux module.
178-
179-
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/index.js`.
146+
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/index.js`.
180147

181148
## Deployment
182149
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:
183150

184151
### Static Deployments
185-
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](https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md#configuring-your-server) helpful. The Koa 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.
186-
187-
### Heroku
188-
189-
Heroku has `nodejs buildpack` script that does the following when you deploy your app to Heroku.
190-
1. Find `packages.json` in the root directory.
191-
2. Install `nodejs` and `npm` packages.
192-
3. Run `npm postinstall script`
193-
4. Run `npm start`
194-
195-
Therefore, you need to modify `package.json` before deploying to Heroku. Make the following changes in the `scripts` section of `package.json`.
196-
197-
```
198-
...
199-
"start": "better-npm-run start:prod",
200-
"serve": "better-npm-run start",
201-
"postinstall": "npm run deploy:prod",
202-
"betterScripts": {
203-
...
204-
"start:prod": {
205-
"command": "babel-node bin/server",
206-
"env": {
207-
"NODE_ENV": "production"
208-
}
209-
}
210-
...
211-
},
212-
```
213-
214-
It's also important to tell Heroku to install all `devDependencies` to successfully compile your app on Heroku's environment. Run the following in your terminal.
215-
216-
```bash
217-
$ heroku config:set NPM_CONFIG_PRODUCTION=false
218-
```
219-
220-
With this setup, you will install all the necessray packages, build your app, and start the webserver (e.g. koa) everytime you push your app to Heroku. Try to deploy to Heroku by running the following commands.
221-
222-
```bash
223-
$ git add .
224-
$ git commit -m 'My awesome commit'
225-
$ git push heroku master
226-
```
227-
228-
If you fail to deploy for an unknown reason, try [this helpful comment](https://github.com/davezuko/react-redux-starter-kit/issues/730#issuecomment-213997120) by [DonHansDampf](https://github.com/DonHansDampf) addressing Heroku deployments.
229-
230-
Have more questions? Feel free to submit an issue or join the Gitter chat!
152+
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](https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md#configuring-your-server) 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.
231153

232154
## Build System
233155

@@ -241,13 +163,11 @@ If you need environment-specific overrides (useful for dynamically setting API e
241163
|---|-----------|
242164
|`dir_src`|application source code base path|
243165
|`dir_dist`|path to build compiled application to|
244-
|`server_host`|hostname for the Koa server|
245-
|`server_port`|port for the Koa server|
246-
|`compiler_css_modules`|whether or not to enable CSS modules|
166+
|`server_host`|hostname for the Express server|
167+
|`server_port`|port for the Express server|
247168
|`compiler_devtool`|what type of source-maps to generate (set to `false`/`null` to disable)|
248169
|`compiler_vendor`|packages to separate into to the vendor bundle|
249170

250-
251171
### Root Resolve
252172
Webpack is configured to make use of [resolve.root](http://webpack.github.io/docs/configuration.html#resolve-root), which lets you import local packages as if you were traversing from the root of your `~/src` directory. Here's an example:
253173

@@ -275,11 +195,11 @@ These are global variables available to you anywhere in your source code. If you
275195

276196
### Styles
277197

278-
Both `.scss` and `.css` file extensions are supported out of the box and are configured to use [CSS Modules](https://github.com/css-modules/css-modules). After being imported, styles will be processed with [PostCSS](https://github.com/postcss/postcss) for minification and autoprefixing, and will be extracted to a `.css` file during production builds.
198+
Both `.scss` and `.css` file extensions are supported out of the box. After being imported, styles will be processed with [PostCSS](https://github.com/postcss/postcss) for minification and autoprefixing, and will be extracted to a `.css` file during production builds.
279199

280200
### Server
281201

282-
This starter kit comes packaged with an Koa 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 Koa app in place of [webpack-dev-server](https://github.com/webpack/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.
202+
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](https://github.com/webpack/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.
283203

284204
### Production Optimization
285205

bin/compile.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import fs from 'fs-extra'
2-
import _debug from 'debug'
3-
import webpackCompiler from '../build/webpack-compiler'
4-
import webpackConfig from '../build/webpack.config'
5-
import config from '../config'
1+
const fs = require('fs-extra')
2+
const debug = require('debug')('app:bin:compile')
3+
const webpackCompiler = require('../build/webpack-compiler')
4+
const webpackConfig = require('../build/webpack.config')
5+
const config = require('../config')
66

7-
const debug = _debug('app:bin:compile')
87
const paths = config.utils_paths
98

10-
;(async function () {
11-
try {
12-
debug('Run compiler')
13-
const stats = await webpackCompiler(webpackConfig)
14-
if (stats.warnings.length && config.compiler_fail_on_warning) {
15-
debug('Config set to fail on warning, exiting with status code "1".')
9+
const compile = () => {
10+
debug('Starting compiler.')
11+
return Promise.resolve()
12+
.then(() => webpackCompiler(webpackConfig))
13+
.then(stats => {
14+
if (stats.warnings.length && config.compiler_fail_on_warning) {
15+
throw new Error('Config set to fail on warning, exiting with status code "1".')
16+
}
17+
debug('Copying static assets to dist folder.')
18+
fs.copySync(paths.client('static'), paths.dist())
19+
})
20+
.then(() => {
21+
debug('Compilation completed successfully.')
22+
})
23+
.catch((err) => {
24+
debug('Compiler encountered an error.', err)
1625
process.exit(1)
17-
}
18-
debug('Copy static assets to dist folder.')
19-
fs.copySync(paths.client('static'), paths.dist())
20-
} catch (e) {
21-
debug('Compiler encountered an error.', e)
22-
process.exit(1)
23-
}
24-
})()
26+
})
27+
}
28+
29+
compile()

bin/server.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import config from '../config'
2-
import server from '../server/main'
3-
import _debug from 'debug'
1+
const config = require('../config')
2+
const server = require('../server/main')
3+
const debug = require('debug')('app:bin:server')
44

5-
const debug = _debug('app:bin:server')
65
const port = config.server_port
76
const host = config.server_host
87

98
server.listen(port)
109
debug(`Server is now running at http://${host}:${port}.`)
11-
debug(`Server accessible via localhost:${port} if you are using the project defaults.`)

build/karma.conf.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { argv } from 'yargs'
2-
import config from '../config'
3-
import webpackConfig from './webpack.config'
4-
import _debug from 'debug'
5-
6-
const debug = _debug('app:karma')
7-
debug('Create configuration.')
1+
const { argv } = require('yargs')
2+
const config = require('../config')
3+
const webpackConfig = require('./webpack.config')
4+
const debug = require('debug')('app:karma')
85

6+
debug('Creating configuration.')
97
const karmaConfig = {
108
basePath : '../', // project root in relation to bin/karma.js
119
files : [
@@ -25,13 +23,11 @@ const karmaConfig = {
2523
browsers : ['PhantomJS'],
2624
webpack : {
2725
devtool : 'cheap-module-source-map',
28-
resolve : {
29-
...webpackConfig.resolve,
30-
alias : {
31-
...webpackConfig.resolve.alias,
26+
resolve : Object.assign({}, webpackConfig.resolve, {
27+
alias : Object.assign({}, webpackConfig.resolve.alias, {
3228
sinon : 'sinon/pkg/sinon.js'
33-
}
34-
},
29+
})
30+
}),
3531
plugins : webpackConfig.plugins,
3632
module : {
3733
noParse : [
@@ -46,12 +42,11 @@ const karmaConfig = {
4642
},
4743
// Enzyme fix, see:
4844
// https://github.com/airbnb/enzyme/issues/47
49-
externals : {
50-
...webpackConfig.externals,
45+
externals : Object.assign({}, webpackConfig.externals, {
5146
'react/addons' : true,
5247
'react/lib/ExecutionEnvironment' : true,
5348
'react/lib/ReactContext' : 'window'
54-
},
49+
}),
5550
sassLoader : webpackConfig.sassLoader
5651
},
5752
webpackMiddleware : {
@@ -72,5 +67,4 @@ if (config.globals.__COVERAGE__) {
7267
}]
7368
}
7469

75-
// cannot use `export default` because of Karma.
7670
module.exports = (cfg) => cfg.set(karmaConfig)

build/webpack-compiler.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import webpack from 'webpack'
2-
import _debug from 'debug'
3-
import config from '../config'
1+
const webpack = require('webpack')
2+
const debug = require('debug')('app:build:webpack-compiler')
3+
const config = require('../config')
44

5-
const debug = _debug('app:build:webpack-compiler')
6-
const DEFAULT_STATS_FORMAT = config.compiler_stats
5+
function webpackCompiler (webpackConfig, statsFormat) {
6+
statsFormat = statsFormat || config.compiler_stats
77

8-
export default function webpackCompiler (webpackConfig, statsFormat = DEFAULT_STATS_FORMAT) {
98
return new Promise((resolve, reject) => {
109
const compiler = webpack(webpackConfig)
1110

@@ -33,3 +32,5 @@ export default function webpackCompiler (webpackConfig, statsFormat = DEFAULT_ST
3332
})
3433
})
3534
}
35+
36+
module.exports = webpackCompiler

0 commit comments

Comments
 (0)