-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (75 loc) · 2.83 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const
express = require('express'),
app = express(),
path = require('path'),
webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
config = require('./config')
server = require('./server'),
chalk = require('chalk'),
chokidar = require('chokidar');
app.set('env', config.env);
//
// View engine
// ---------------------------------------------------------------------------------------
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//
// Include server logic as a middleware
// ---------------------------------------------------------------------------------------
app.use((req, res, next) => {
if (config.env === 'production')
return server.app(req, res, next);
// in development mode, this middleware doesn't use the chached 'server' variable
// but requires again the server entry every time, so that when require cache is flush with
// chokidar it requires the update server module
require('./server').app(req, res, next);
});
//
// Start webpack
// ---------------------------------------------------------------------------------------
const webpackConfig = require('./tools/webpack.config')(app.get('env'));
let compiler = webpack(webpackConfig);
// Production
if (config.env === 'production') {
compiler.apply(new webpack.ProgressPlugin());
return compiler.run((err, stats) => {
if (err) next(err);
// get the list of built assets
let assets = stats.toJson().assets;
//set the list globally
app.set('assets', assets);
// once the assets' name are resolved, start the app
server.start(app);
});
}
// Development
console.log(chalk.cyan('Starting development server...'));
let webpackDevMiddlewareInstance = webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
chunks: false, // this reduces the amount of stuff I see in my terminal; configure to your needs
'errors-only': true
}
});
app.use(webpackDevMiddlewareInstance);
app.use(webpackHotMiddleware(compiler, {
log: console.log
}));
webpackDevMiddlewareInstance.waitUntilValid(server.start.bind(server, app));
//
// Do 'hot-reloading' of express stuff on the server
// Throw away cached modules and re-require next time
// Ensure there's no important state in there!
// ---------------------------------------------------------------------------------------
const watcher = chokidar.watch('./server');
watcher.on('ready', () => {
watcher.on('all', () => {
console.log('Clearing /server/ module cache from server');
Object.keys(require.cache).forEach((id) => {
if (/[\/\\]server[\/\\]/.test(id)) delete require.cache[id];
});
});
});