forked from xdan/jodit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
154 lines (123 loc) · 3.03 KB
/
server.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2022 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
const webpack = require('webpack');
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
const reg = /--([a-zA-Z-]+)\s*=\s*(.*)/;
const args = {};
process.argv
.filter(a => reg.test(a))
.forEach(c => {
const res = reg.exec(c);
args[res[1]] = res[2];
if (args[res[1]] === 'true') {
args[res[1]] = true;
}
if (args[res[1]] === 'false') {
args[res[1]] = false;
}
});
const config = require(path.resolve(cwd, './webpack.config'))(
[],
{
es: 'es5',
isTest: true,
...args
},
cwd
);
const compression = require('compression');
const open = require('open');
const app = new express();
app.use(compression());
const port = args.port || 2000;
const compiler = webpack(config);
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));
const compile = new Promise((resolve, reject) => {
compiler.hooks.failed.tap('CompilePlugin', () => {
reject();
});
compiler.hooks.done.tap('CompilePlugin', () => {
resolve();
});
});
app.get('/', (req, res) => {
if (fs.existsSync(cwd + '/test.html')) {
res.sendFile(cwd + '/test.html');
} else {
res.sendFile(cwd + '/index.html');
}
});
app.get('/*.js', (req, res) => {
res.sendFile(cwd + '/' + req.url);
});
app.get('/icons.html', (req, res) => {
res.sendFile(cwd + '/icons.html');
});
app.get('/test.html', (req, res) => {
res.sendFile(cwd + '/test.html');
});
app.get('/fake.html', (req, res) => {
res.sendFile(cwd + '/fake.html');
});
app.use(express.static('./'));
app.get('/build/*.*', (req, res) => {
const filename = cwd + '/' + req.url;
if (fs.existsSync(filename)) {
res.sendFile(filename);
} else {
res.status(404).send('Not Found');
}
});
app.use('/node_modules', require('express').static(cwd + '/node_modules'));
app.use('/test', require('express').static(cwd + '/test'));
app.use('/app.css', require('express').static(cwd + '/app.css'));
app.use(
'/examples/download.jpg',
require('express').static(cwd + '/examples/download.jpg')
);
const host = `http://localhost:${port}/`;
let resolveServer;
const run = new Promise(resolve => {
resolveServer = resolve;
});
const listen = app.listen(port, error => {
if (error) {
console.error(error);
reject(error);
} else {
console.info(
'==> Listening on port %s. Open up http://localhost:%s/ in your browser.',
port,
port
);
compile
.then(() => resolveServer())
.then(() => {
if (!args['no-open']) {
const open = require('open');
open(host, {
newInstance: false
});
}
})
.then(() => console.log('Build done'));
}
});
module.exports = {
host,
port,
run,
close: () => {
listen.close();
compiler.close(() => {});
}
};