app-dev.js
1.67 KB
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
const fs = require('fs');
const path = require('path');
const cluster = require('cluster');
const express = require('express');
const chokidar = require('chokidar');
const {createApp} = require('./create-app.js');
const {createBundleRenderer} = require('vue-server-renderer');
const {devServer, publish} = require('./build/dev-server.js');
const watcher = {
paths: ['./create-app.js', './doraemon', './config', './utils'],
options: {}
};
let renderer;
let realyPromise;
let template = fs.readFileSync(path.join(__dirname, './apps/index.html'), 'utf-8');
if (cluster.isMaster) {
const masterApp = express();
realyPromise = devServer(masterApp, params => {
renderer = createBundleRenderer(params.bundle, Object.assign(params.options, {
runInNewContext: false,
template
}));
});
let childWorker = cluster.fork();
cluster.on('message', (worker, msg) => {
if (msg.action === 'ssr_request') {
realyPromise.then(() => {
renderer.renderToString(msg.context, (err, html) => {
if (err) {
console.error(err);
}
worker.send({action: 'ssr_request', html, err: err && JSON.stringify(err)});
});
});
}
});
chokidar.watch(watcher.paths, watcher.options).on('change', pathStr => {
console.log(`${pathStr} changed`);
childWorker && childWorker.kill();
childWorker = cluster.fork().on('listening', address => {
console.log(`worker is restarted at ${address.port}`);
publish({action: 'reload'});
console.log('client is refresh');
});
});
masterApp.listen(6005, () => {
console.log('master is started');
});
} else {
const app = express();
createApp(app);
}