-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
98 lines (84 loc) · 2.7 KB
/
gulpfile.js
File metadata and controls
98 lines (84 loc) · 2.7 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
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
92
93
94
95
96
97
98
const gulp = require('gulp');
const babel = require('gulp-babel');
const bodyParser = require('body-parser');
const express = require('express');
const http = require('http');
const httpProxy = require('http-proxy');
const webpack = require('webpack');
const DevServer = require('webpack-dev-server');
const argv = require('yargs').argv;
const webpackConfig = require('./webpack.config');
const argHost = argv.host || 'localhost';
const argPort = argv.port || 4000;
const webpackPort = argPort + 1;
const proxyPort = argPort;
const hangerStore = { data: null };
gulp.task('build-webpack', callback => {
webpack(webpackConfig('production'), (err, stats) => {
if (err) {
throw Error('build-webpack', err);
}
if (stats.hasErrors()) {
throw Error('Compile errors have occurred.');
}
callback();
});
});
gulp.task('dev-webpack', () => {
const config = webpackConfig('development');
DevServer.addDevServerEntrypoints(config, {
...config.devServer,
host: 'localhost',
});
const compiler = webpack(config);
const server = new DevServer(compiler, config.devServer);
server.listen(webpackPort, 'localhost', err => {
if (err) {
throw err;
}
console.log('Dev server is running.');
});
});
gulp.task('run-proxy', () => {
const webpackProxy = httpProxy.createProxyServer({
target: {
host: 'localhost',
port: webpackPort,
},
});
webpackProxy.on('error', err => {
console.log('[run-proxy]', `Error on Webpack proxy.`, err);
});
const app = express();
// Run "Hanger" server for test.
app.use(bodyParser.text());
app.post('/hanger/open/', (req, res) => {
res.send('vuepythonpadrunner');
});
app.post('/hanger/vuepythonpadrunner/write/', (req, res) => {
hangerStore.data = req.body;
res.send('vuepythonpadrunner');
});
app.post('/hanger/vuepythonpadrunner/read/', (req, res) => {
const respond = () => {
if (hangerStore.data !== null) {
res.send(hangerStore.data);
hangerStore.data = null;
} else {
setTimeout(() => respond(), 1000);
}
};
respond();
});
app.get('/hanger/sleep/', (req, res) => {
const duration = req.query.duration;
setTimeout(() => res.send(duration), duration * 1000);
});
app.all('/*', (req, res) => {
webpackProxy.web(req, res);
});
const server = http.createServer(app);
server.listen(proxyPort);
});
gulp.task('dev', gulp.parallel('run-proxy', 'dev-webpack'));
gulp.task('build', gulp.parallel('build-webpack'));