-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
225 lines (190 loc) · 5.49 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
var koa = require('koa');
var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');
var log = require('spm-log');
var join = path.join;
var readFile = fs.readFileSync;
var url = require('url');
var util = require('util');
var events = require('events');
function Server(compiler, opts) {
events.EventEmitter.call(this);
opts = opts || {};
this.headers = opts.headers;
compiler.plugin('done', function(stats) {
this.emit('done');
if (opts.livereload) {
try {
var items = Object.keys(stats.compilation.assets);
lrServer.changed({body: {files: items}});
log.warn('livereload', 'changed', items.join(', '));
} catch (e) {
console.log(e);
}
}
}.bind(this));
var ip = require('internal-ip')();
var app = this.app = koa();
// combo 拆分
app.use(require('./combo')({
hostname: ip,
port: opts.port
}));
app.use(function *(next) {
this.set({
'Access-Control-Allow-Origin': '*',
'Timing-Allow-Origin': '*'
});
this.oldUrl = this.url;
// 替换 server.family
if (opts.pkg.spm && opts.pkg.spm.server && opts.pkg.spm.server.family) {
var f = opts.pkg.spm.server.family;
if (f.charAt(0) !== '/') f = '/' + f;
this.url = this.url.replace(new RegExp('^' + f), '');
}
// 处理 livereload 和 weinre
this.htmlPrefix = this.htmlPostfix = '';
if (/\.html?$/.test(this.url.split('?')[0])) {
if (global.SPM_SERVER_HTTP_INJECT) {
this.htmlPrefix = global.SPM_SERVER_HTTP_INJECT;
}
var postFix = [];
if (opts.livereload) {
postFix.push('<script src="http://' + ip + ':35729/livereload.js"></script>');
}
if (opts.weinre) {
postFix.push('<script src="http://' + ip + ':8990/target/target-script-min.js#anonymous"></script>');
}
this.htmlPostfix = '\n' + postFix.join('\n');
}
// 替换 /name/version 为空
var prefix = [''];
if (opts.pkg.name) prefix.push(opts.pkg.name);
if (opts.pkg.version) prefix.push(opts.pkg.version);
prefix = prefix.join('/');
if (prefix && this.url.indexOf(prefix) === 0) {
this.url = this.url.replace(prefix, '');
}
// 替换 -hash.js 为 .js
var m = this.url.match(/^([^-]+)\-[a-z0-9]{20}(\..+)$/i);
if (m && m[1]) {
this.url = m[1] + m[2];
}
yield next;
});
// webpack 中间件
opts.noInfo = true;
try {
if (opts.pkg.spm.server.lazy) opts.lazy = true;
} catch(e) {}
app.use(require('./middleware')(compiler, opts));
// 恢复 url 属性
app.use(function *(next) {
if (this.oldUrl) {
this.url = this.oldUrl;
delete this.oldUrl;
}
yield next;
});
// 针对普通静态 html 文件的 fix
app.use(function *(next) {
var file = url.parse(this.url).pathname;
file = join(opts.cwd, file);
var isHTML = /\.html?$/.test(this.url);
if (isHTML && fs.existsSync(file)) {
var content = readFile(file, 'utf-8');
var docTypeReg = new RegExp('^\s*\<\!DOCTYPE\s*.+\>.*$', 'im');
var docType = content.match(docTypeReg);
if (docType) {
content = content.replace(docTypeReg, docType[0] + this.htmlPrefix);
} else {
content = this.htmlPrefix + content + this.htmlPostfix;
}
this.body = content;
} else {
yield next;
}
});
// 静态资源
app.use(require('./static')(opts.cwd, {
index: 'dont-have-index.xxxx'
}));
// 目录访问
app.use(require('koa-serve-index')(opts.cwd, {
hidden: true,
view: 'details'
}));
// cdn 反向代理
app.use(require('./cdn')());
// weinre 服务器
if (opts.weinre) {
require('coffee-script');
require('weinre').run({
httpPort: 8990,
boundHost: ip,
verbose: false,
debug: false,
readTimeout: 5,
open: false,
weinre: true,
deathTimeout: 15
});
}
// livereload 服务器
if (opts.livereload) {
var lrServer = require('tiny-lr')();
lrServer.listen(35729, function(err) {
if (err) {
log.error('livereload', err);
return;
}
log.info('livereload', 'listened on 35729');
});
}
// anyproxy 代理
if (opts.proxy) {
var anyproxy;
try {
anyproxy = require('anyproxy');
} catch(e) {
log.error('error', 'npm install anyproxy -g to enable proxy');
process.exit(1);
}
if (!anyproxy) {
log.error('proxy', '`anyproxy` is not install correctly');
process.exit(1);
}
!anyproxy.isRootCAFileExists() && anyproxy.generateRootCA();
new anyproxy.proxyServer({
type: 'http',
port: opts.anyproxyPort || 8989,
hostname: 'localhost',
rule: require('./rule')({
port: opts.port,
hostname: ip
})
});
}
// https 支持
this.listeningApp = opts.https ? https.createServer({
// using built-in self-signed certificate
key: opts.key || fs.readFileSync(path.join(__dirname, './ssl/server.key')),
cert: opts.cert || fs.readFileSync(path.join(__dirname, './ssl/server.crt')),
ca: opts.ca || fs.readFileSync(path.join(__dirname, './ssl/ca.crt')),
requestCert: true,
rejectUnauthorized: false
}, app.callback())
: http.createServer(app.callback());
}
Server.prototype.use = function(gen) {
this.app.use(gen);
return this;
};
Server.prototype.listen = function() {
this.listeningApp.listen.apply(this.listeningApp, arguments);
};
util.inherits(Server, events.EventEmitter);
module.exports = Server;