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

Commit e9e8f23

Browse files
authored
Hapi 17 (#33)
* update to Hapi 17 * fix imports
1 parent b274481 commit e9e8f23

7 files changed

+323
-335
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
"stylelint": "9.1.3",
8989
"stylelint-config-standard": "18.2.0",
9090
"stylelint-order": "0.8.1",
91-
"webpack-dev-middleware": "1.12.1",
92-
"webpack-hot-middleware": "2.20.0",
91+
"webpack-dev-middleware": "3.0.1",
92+
"webpack-hot-middleware": "2.21.2",
9393
"webpack-simple-progress-plugin": "0.0.4",
9494
"write-file-webpack-plugin": "4.2.0"
9595
},
@@ -100,10 +100,10 @@
100100
"fetch-everywhere": "1.0.5",
101101
"form2js": "1.0.0",
102102
"fs-extra": "5.0.0",
103-
"hapi": "16.6.2",
104-
"hapi-webpack-plugin": "2.0.0",
103+
"hapi": "17.2.2",
104+
"hapi-webpack-plugin": "3.0.0",
105105
"history": "4.7.2",
106-
"inert": "4.2.1",
106+
"inert": "5.1.0",
107107
"node-notifier": "5.2.1",
108108
"react": "16.2.0",
109109
"react-async-bootstrapper": "1.1.2",

src/server.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import 'fetch-everywhere';
22
import * as inert from 'inert';
3-
4-
import ServerManager from './server/ServerManager';
53
import AssetsController from './server/controllers/AssetsController';
6-
import ReactController from './server/controllers/ReactController';
74
import HapiWebpackHotPlugin from './server/plugin/HapiWebpackHotPlugin';
5+
import ReactController from './server/controllers/ReactController';
6+
import ServerManager from './server/ServerManager';
7+
8+
(async () => {
9+
10+
const manager = new ServerManager();
11+
12+
await manager.registerPlugin(inert);
13+
14+
if (manager.isDevelopment) {
15+
const hapiWebpackHotPlugin = new HapiWebpackHotPlugin();
816

9-
const manager = new ServerManager();
17+
await manager.registerPlugin(hapiWebpackHotPlugin.plugin);
18+
}
1019

11-
manager.registerPlugin(inert);
20+
manager.registerController(new AssetsController());
21+
manager.registerController(new ReactController());
1222

13-
if (manager.isDevelopment) {
14-
new HapiWebpackHotPlugin(manager.server); // eslint-disable-line no-new
15-
}
23+
await manager.startServer();
1624

17-
manager.registerController(new AssetsController());
18-
manager.registerController(new ReactController());
19-
manager.startServer();
25+
})();

src/server/ServerManager.js

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
import * as Hapi from 'hapi';
2-
3-
const PORT = process.env.PORT || 3000;
4-
const HOST = process.env.HOST || 'localhost';
5-
const NODE_ENV = process.env.NODE_ENV;
1+
import Hapi from 'hapi';
62

73
class ServerManager {
84

9-
static log = () => console.info(`\n\nServer running in ${NODE_ENV} mode at: http://${HOST}:${PORT}\n`);
5+
static PORT = parseInt(process.env.PORT, 10) || 3000;
6+
static HOST = process.env.HOST || 'localhost';
7+
static NODE_ENV = process.env.NODE_ENV;
108

11-
_server = new Hapi.Server({debug: {request: ['error']}});
9+
isDevelopment = (ServerManager.NODE_ENV === 'development');
1210

13-
isDevelopment = (NODE_ENV === 'development');
11+
_server = null;
1412

1513
get server() {
1614
return this._server;
1715
}
1816

1917
constructor() {
20-
this._server.connection({
21-
host: HOST,
22-
port: PORT,
23-
});
18+
const options = {
19+
host: ServerManager.HOST,
20+
port: ServerManager.PORT,
21+
};
22+
23+
this._server = new Hapi.Server(options);
24+
}
25+
26+
static log() {
27+
console.info(`\n\nServer running in ${ServerManager.NODE_ENV} mode at: http://${ServerManager.HOST}:${ServerManager.PORT}\n`);
2428
}
2529

2630
async registerPlugin(pluginConfig) {
@@ -31,16 +35,16 @@ class ServerManager {
3135
controller.mapRoutes(this._server);
3236
}
3337

34-
startServer() {
35-
this._server.start((error) => {
36-
if (error) {
37-
throw error;
38-
}
38+
async startServer() {
39+
try {
40+
await this._server.start();
3941

4042
if (!this.isDevelopment) {
4143
ServerManager.log();
4244
}
43-
});
45+
} catch (err) {
46+
console.error(err);
47+
}
4448
}
4549

4650
}

src/server/controllers/AssetsController.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ class AssetsController {
66
server.route({
77
method: 'GET',
88
path: '/assets/{file*}',
9-
handler: (request, reply) => {
10-
reply.file(path.resolve(__dirname, `../../public${request.path}`));
9+
handler: (request, h) => {
10+
const file = path.resolve(__dirname, `../../public${request.path}`);
11+
12+
return h.file(file);
1113
},
1214
});
1315
}

src/server/controllers/ReactController.jsx

+19-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {AsyncComponentProvider, createAsyncContext} from 'react-async-component'
33
import asyncBootstrapper from 'react-async-bootstrapper';
44
import serialize from 'serialize-javascript';
55
import path from 'path';
6-
import * as fse from 'fs-extra';
7-
import * as React from 'react';
6+
import fse from 'fs-extra';
7+
import React from 'react';
88
import RouterWrapper from '../../RouterWrapper';
99
import ProviderService from '../../services/ProviderService';
1010
import rootSaga from '../../stores/rootSaga';
@@ -17,7 +17,7 @@ class ReactController {
1717
server.route({
1818
method: 'GET',
1919
path: '/{route*}',
20-
handler: async (request, reply) => {
20+
handler: async (request, h) => {
2121
const store = ProviderService.createProviderStore({}, null, true);
2222
const asyncContext = createAsyncContext();
2323
const routeContext = {};
@@ -36,11 +36,19 @@ class ReactController {
3636

3737
await asyncBootstrapper(app);
3838

39-
store.runSaga(rootSaga).done.then(() => {
40-
if (routeContext.url) {
41-
return reply().redirect(routeContext.url).permanent().rewritable();
42-
}
39+
const sagaDone = store.runSaga(rootSaga).done;
4340

41+
renderToString(app);
42+
43+
store.endSaga();
44+
45+
await sagaDone;
46+
47+
if (routeContext.url) {
48+
return h.redirect(routeContext.url);
49+
}
50+
51+
try {
4452
const renderedHtml = renderToString(app);
4553
const asyncComponentsState = asyncContext.getState();
4654
const state = store.getState();
@@ -60,14 +68,10 @@ class ReactController {
6068
.replace('{state}', JSON.stringify(initialState))
6169
.replace('{asyncComponentsState}', serialize(asyncComponentsState));
6270

63-
return reply(html);
64-
}).catch((error) => {
65-
reply(error.toString());
66-
});
67-
68-
renderToString(app);
69-
70-
store.endSaga();
71+
return h.response(html);
72+
} catch (error) {
73+
return error.toString();
74+
}
7175
},
7276
});
7377
}

src/server/plugin/HapiWebpackHotPlugin.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
import Webpack from 'webpack';
2-
import ServerManager from '../ServerManager';
31
import HapiWebpackPlugin from 'hapi-webpack-plugin';
42
import notifier from 'node-notifier';
3+
import Webpack from 'webpack';
4+
import ServerManager from '../ServerManager';
55

66
class HapiWebpackHotPlugin {
77

8-
constructor(server) {
8+
get plugin() {
99
const config = require('../../../webpack.config.js'); // eslint-disable-line global-require
1010
const compiler = Webpack(config);
1111

1212
compiler.plugin('done', (stats) => this._onDone(stats));
1313

14-
const options = {
15-
assets: {
16-
// webpack-dev-middleware options - https://github.com/webpack/webpack-dev-middleware
17-
index: '/public/index.html',
18-
},
19-
hot: {
20-
// webpack-hot-middleware options - https://github.com/glenjamin/webpack-hot-middleware
21-
},
22-
compiler,
14+
const assets = {
15+
// webpack-dev-middleware options - See https://github.com/webpack/webpack-dev-middleware
16+
index: '/public/index.html',
2317
};
2418

25-
server.register({
26-
register: HapiWebpackPlugin,
27-
options,
28-
}, (error) => {
29-
if (error) {
30-
console.error(error);
31-
}
32-
});
19+
const hot = {
20+
// webpack-hot-middleware options - See https://github.com/glenjamin/webpack-hot-middleware
21+
};
22+
23+
return {
24+
plugin: HapiWebpackPlugin,
25+
options: {compiler, assets, hot},
26+
};
3327
}
3428

3529
_onDone(stats) {

0 commit comments

Comments
 (0)