forked from apache/couchdb-fauxton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevserver.js
116 lines (95 loc) · 2.72 KB
/
devserver.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var spawn = require('child_process').spawn;
var fs = require("fs");
var webpack = require('webpack');
var WebpackDev = require('webpack-dev-server');
var config = require('./webpack.config.dev.js');
var httpProxy = require('http-proxy');
var loadSettings = function () {
var fileName = './settings.json.default.json';
if (fs.existsSync('./settings.json')) {
fileName = './settings.json';
}
return require(fileName).couchserver || {
port: process.env.FAUXTON_PORT || 8000,
contentSecurityPolicy: true,
proxy: {
target: 'http://127.0.0.1:5984',
changeOrigin: false
}
};
};
var settings = loadSettings();
var devSetup = function (cb) {
console.log('setup dev environment');
var cmd = 'devSetupWithClean';
if (settings.noClean) {
cmd = 'devSetup';
}
var grunt = spawn('grunt', [cmd]);
grunt.stdout.on('data', (data) => {
console.log(data.toString());
});
grunt.stderr.on('error', (data) => {
console.log('Setup error:', data.toString());
});
grunt.on('close', (code) => {
console.log('dev setup finished with code', code);
if (code === 0) {
cb();
}
});
};
const defaultHeaderValue = "default-src 'self'; img-src 'self' data:; font-src 'self'; " +
"script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline';";
function getCspHeaders () {
if (!settings.contentSecurityPolicy) {
return;
}
const cspHeader = settings.contentSecurityPolicyHeader || defaultHeaderValue;
return {
'Content-Security-Policy': cspHeader
};
};
var runWebpackServer = function () {
var options = {
contentBase: __dirname + '/dist/debug',
publicPath: '/',
outputPath: '/',
filename: 'bundle.js',
host: 'localhost',
port: process.env.FAUXTON_PORT || 8000,
hot: false,
historyApiFallback: true,
stats: {
colors: true,
},
headers: getCspHeaders(),
};
var compiler = webpack(config);
var server = new WebpackDev(compiler, options);
var proxy = httpProxy.createServer({
secure: false,
changeOrigin: true,
target: settings.proxy.target
});
proxy.on('proxyRes', function (proxyRes) {
if (proxyRes.headers['set-cookie']) {
proxyRes.headers['set-cookie'][0] = proxyRes.headers["set-cookie"][0].replace('Secure', '');
}
});
proxy.on('error', function () {
// don't explode on cancelled requests
});
server.app.all('*', function (req, res) {
proxy.web(req, res);
});
server.listen(options.port, '0.0.0.0', function (err) {
if (err) {
console.log(err);
return;
}
console.log('listening on', options.host, options.port);
console.log('Starting first compile. This will take about 10 seconds...');
});
};
devSetup(runWebpackServer);