Repository navigation
Expand file tree
/
Copy pathdevserver.js
More file actions
66 lines (53 loc) · 1.72 KB
/
Copy pathdevserver.js
File metadata and controls
66 lines (53 loc) · 1.72 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
59
60
61
62
63
64
65
66
const { spawn } = require('child_process');
const express = require('express');
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const app = express();
const [nodeConfig, browserConfig] = require('./webpack.prod.js');
/**
* Node
*/
const nodeCompiler = webpack(nodeConfig);
// Start middleware. Use writeToDisk because bin/browserglue.js requires the bundle on dist/
middleware(nodeCompiler, { writeToDisk: true });
const spawnBrowserglueBinary = () => {
console.log('* Spawn Browserglue child process')
const bin = spawn('./bin/browserglue.js');
bin.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
bin.stderr.on('data', (data) => {
process.stderr.write(data.toString());
});
bin.on('close', (code) => {
console.log(`* Browserglue child process exited with code ${code}`);
});
return bin;
}
let serverProcess;
// Whenever compilation finishes, (re)spawn browserglue child process
nodeCompiler.hooks.done.tap('RestartBrowserGlue', (stats) => {
// return true to emit the output, otherwise false
// console.log(stats);
console.log('* Restart Browserglue server');
if (serverProcess) {
serverProcess.kill();
}
serverProcess = spawnBrowserglueBinary();
return true;
});
/**
* Browser
*/
const browserCompiler = webpack(browserConfig);
const browserMiddleware = middleware(browserCompiler, {
publicPath: browserConfig.output.publicPath,
});
// Tell express to use the webpack-dev-middleware and use the webpack dev
// configuration file as a base.
app.use(browserMiddleware);
app.use(express.static('dist'));
// Serve the files on port 3000.
app.listen(3000, () => {
console.log('Serving files at dist/ on http://localhost:3000');
});