-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
120 lines (100 loc) · 2.68 KB
/
index.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
117
118
119
120
/**
* Transport logs from webpack console to browser console
*
* Usage: in webpack.config.js:
*
* const ConsoleToBrowserPlugin = require('webpack-plugin-console-to-browser')
* plugins: [
* new ConsoleToBrowserPlugin()
* ]
*/
module.exports = class ConsoleToBrowserPlugin {
constructor() {
this.connPool = [];
}
buildScript() {
return [
'',
'',
'// webpack-plugin-console-to-browser bootstrap',
'(function() {',
' if (typeof window == "undefined") return;',
' var scriptDom = document.createElement("script");',
' scriptDom.setAttribute("type", "text/javascript");',
' scriptDom.setAttribute("src", "http://localhost:23233/assistant.js");',
' document.body.appendChild(scriptDom);',
'})()'
].join('\n');
}
initServer() {
const http = require('http'),
handler = http.createServer();
// start express for downloading script
const express = require('express'),
app = express();
app.get('/assistant.js', (req, res) => {
const fs = require('fs');
const script = fs.readFileSync(__dirname + '/client/assistant.js');
res.send(script);
res.end();
});
app.listen(23233); // default port of express is 23233
// start sockjs server
const sockjs = require('sockjs'),
sockServer = sockjs.createServer({
sockjs_url: 'http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js'
});
sockServer.on('connection', (conn) => {
if (!conn)
return;
this.connPool.push(conn);
// destroy
conn.on('close', () => {
const connIndex = this.connPool.indexOf(conn);
if (connIndex > 0) {
this.connPool.splice(connIndex, 1);
}
});
});
sockServer.installHandlers(handler, { prefix: '/sockjs-node' });
handler.listen(56867); // default port of sockjs is 56867
}
sockWrite(type, data) {
const items = this.connPool,
stripAnsi = require('strip-ansi');
for (const i in data)
{
data[i] = stripAnsi(data[i]);
}
items.forEach(item => {
item.write(JSON.stringify({
type: type,
data: data
}));
});
}
injectScript(compilation) {
compilation.mainTemplate.plugin('startup', (source) => {
console.log('\nInjecting console-to-browser script to bundle...');
return this.buildScript() + source;
});
}
sendWarnings(warnings) {
this.sockWrite('warnings', warnings);
}
sendErrors(errors) {
this.sockWrite('errors', errors);
}
onBuildCompleted(stats) {
const statsJson = stats.toJson({
errorDetails: false
});
this.sendWarnings(statsJson.warnings);
this.sendErrors(statsJson.errors);
}
apply(compiler) {
this.initServer();
compiler.plugin('compilation', this.injectScript.bind(this));
compiler.plugin('done', this.onBuildCompleted.bind(this));
}
};